Setting up Pi-hole and WireGuard VPN via PiVPN for your personal ad-blocker that works everywhere
This guide will teach you how to setup a personal adblocker that works anywhere, even blocking ads on your phone and within apps!
Contents
- Why do you want to do this?
- Pre-Requisites
- Part 1: Installing the OS for your Raspberry Pi
- Part 2: Setting up your Raspberry Pi for Remote Access (optional)
- Part 3: Installing Pi-hole
- Part 4: Configuring your network to use Pi-hole
- Part 5: Sign up with NoIP for Dynamic DNS
- Part 6: Installing PiVPN with WireGuard to enable you to block ads everywhere
- Part 7: Setting up NoIP dynamic update client
- Part 8: Connecting your mobile device to your WireGuard VPN
Why do you want to do this?
You might be thinking: “I already have adblock / ublock / extension of choice installed on my browser and it blocks ads, so why should I do this?”. Well, for most people, an ad-blocking extension is indeed enough. However, you must have realised that the extension is limited to only working in your desktop browser. Your mobile browser probably still loads ads, and your apps certainly do. Given that more than 50% of web traffic are coming from mobile devices[1], a number that has been increasing, you are likely spending more time surfing the web on your phone than your desktop/laptop. This guide will teach you how to set up an adblocker that will work on all your devices at all times, even when you are out and about. I will also guide you on how to set up your Pi via remote access if you do not have a spare screen / mouse / keyboard lying around.
Pre-Requisites
- Raspberry Pi — I’m using the Raspberry Pi 4 Model B with 2GB of ram, but anything with more than 512MB of ram will work (e.g. Raspberry Pi Zero)
- 8GB microSD card recommended — you could go with 4GB if you do not want to have the desktop environment of Raspberry Pi’s OS
- Accessories to power your Pi and connect it to your home network
Part 1: Installing the OS for your Raspberry Pi
Head over to https://www.raspberrypi.org/software/ and grab the Raspberry Pi Imager.
The above screen will greet you. Now just choose the default OS and choose your SD card and proceed to write the image to the card. Some screenshots below to guide you:
Part 2: Setting up your Raspberry Pi for Remote Access (optional)
You would want to do this step if you do not have a spare mouse / keyboard / screen to access your Raspberry Pi with. Go here and come back to this guide after.
Part 3: Installing Pi-hole
Now let’s start transforming your Raspberry Pi into an ad-blocker. If you have not followed step 2 and are accessing your Pi directly via a connected monitor and keyboard, login to your Pi with the following default username and password:
username: pi
password: rasbperry
To install Pi-hole, enter the below command into the command line after you have logged in.
curl -sSL https://install.pi-hole.net | bash
You can select the default options for the next few screens until the below one.
When installing, make sure to choose the interface as above accordingly. eth0
if you are using an ethernet cable and wlan0
if you are using WiFi.
Now you will be prompted to choose your DNS provider, as above. Google is fine, but I prefer Cloudflare.
For now, selecting these 2 default lists as above is fine. You can change it later if you wish to. A good set of adblock lists can be found here: https://firebog.net/. For a simple low maintenance set of lists, simply use those listed here: https://v.firebog.net/hosts/lists.php?type=tick.
You may select the default settings for the next few pages.
Now, the final screen will show you your default password for the web interface page. However, do not worry about copying it down. Just dismiss that page.
You should now be at the page above. We will now reset the password of Pi-hole’s web interface to one of your liking. Enter the following command pihole -a -p
and reset your password.
Congrats, you have now installed Pi-hole!
Part 4: Configuring your network to use Pi-hole
Once installed, you will need to make sure you are using Pi as the DNS server for your network. You can either:
- Set your router to use Pi as the only DNS server in its settings; OR
- Use your Pi as the DHCP server and disable the router’s DHCP server
Lets go through 1 first:
Use the command hostname -I
to find the IP address of your Pi, as shown above.
Now you will need to find out the default gateway IP address in order to access your router’s configuration screen. You can either use ipconfig via Windows’ command prompt or the route command via Raspberry Pi. See the two screenshots above.
You can now access your router configuration page by typing the default gateway IP address in your browser’s address bar.
The above shows my linksys router’s settings set to use Pi as the DNS server. Look for something similar on your router’s settings page. You want to set the Static DNS to your Pi’s ip address and leave the rest as they are.
Now, for option 2:
There are some advantages to using Pi as your DHCP server as it may have more features than your router’s built in DHCP server.
First, you have to find out your Pi’s IP address via the command hostname -I
, as above.
Now, you can go into the admin panel of your Pi-hole by typing the Pi’s ip address followed by admin in your browser, like so: 192.168.10.150/admin/
Please replace the IP address with your own value.
Go to the settings page and check the “DHCP server enabled” box. Scroll down and click save at the bottom of the page. Notice the red message asking you to disable your router’s DHCP server. We will do that now.
The above shows how to disable the DHCP server on my Linksys router. Look for something similar on your router settings page.
Now you are all set! You can stop here if you only want to block ads on your local network, but that’s not why we are here right?
Part 5: Sign up with NoIP for Dynamic DNS
We would need to find your public IP address prior to setting up the WireGuard VPN to allow you to access your Pi from anywhere and use it to block ads. To find your current public IP address, simply google “what is my ip”. However, this would not work for most of us as your internet service provider (ISP) will likely assign dynamic IP addresses for you and they change every now and then. Hence, we shall sign up for a dynamic DNS provider to resolve our changing IP address.
Go to noip.com and sign up for an account. Make sure to choose ddns.net as the hostname domain because we are using the free option. Note down the hostname you created. I’m recommending noip as it has an application which can be run on Raspberry Pi which automatically updates your dynamic DNS every few minutes, keeping it up to date. We will set that up after installation of PiVPN.
Part 6: Installing PiVPN with WireGuard to enable you to block ads everywhere
Let’s install PiVPN by running the following command:
curl -L https://install.pivpn.io | bash
Be sure to select the WireGuard option when prompted. The rest can be left as the defaults.
There will be a page which asks you to select the port to be used. WireGuard’s default port is 51820. You can keep it or change it. Note it down as you will need to port forward this port via UDP in your router settings.
After the page allowing you to set the port, there will be a prompt which asks if you wish to use PiVPN with your Pi-hole installation. Select “yes”.
Now you should arrive at this screen above. Select the second option “DNS Entry”. Enter the noip hostname you created in part 5. Follow through the rest of the prompts with the default options.
After rebooting, your have now installed PiVPN with WireGuard.
Part 7: Setting up NoIP dynamic update client
You can follow the guide here to install the noip dynamic update client on your Raspberry Pi. This will allow your public IP address to be updated regularly via the NoIP service.
Now, we have to ensure that the NoIP service is started whenever your Pi boots. To do so, we will use the rc.local
file. Edit the file by typing the following command:
sudo nano /etc/rc.local
You should now see the above screen. Simply add noip2
in between the lines fi
and exit 0
. You can leave empty lines between them to keep the file easy to read.
Part 8: Connecting your mobile device to your WireGuard VPN
This part is quite easy. Simply type the below command to add a WireGuard user account:
pivpn add
Now, download the WireGuard app on your phone and use it to scan the qr code generated by typing the following command:
pivpn -qr
Ok you are all set! Go forth and enjoy your new ad-free life.