Ads can be really annoying on some websites, and while there are browser extensions like Adblock to block them, those solutions are only local to your browser, and not always very effective. A better way to block ads is to use a Pi-Hole on your local network. Pi-Hole is a software for the Raspberry Pi, that acts as your DNS server to block queries for known advertiser domains.
Let’s see how to install Pi-Hole on a Raspberry Pi, and how to configure your network to use it!
First, note that Pi-Hole is compatible with several operating systems. The full list is available on Pi-Hole’s documentation page. Here we’ll only talk about the Raspberry Pi. I’ll be using Raspbian Stretch Lite, but the Desktop version would work the same. The installation on other operating systems would be very similar to what I’ll show here.
Pi-Hole can be installed via downloading a script from the developer’s website and running it. You can either pipe the downloaded script to bash, or download the script then execute it. As a general rule, piping things to bash is considered bad practice if you don’t know what is being piped:
curl -sSL https://install.pi-hole.net | bash
Since Pi-Hole can be considered a trusted source, it is safe here to do that. For more safety, download the script to a file, read it to make sure it’s a legit script, and then execute it:
curl -sSL -o install.sh https://install.pi-hole.net
# Read the script, then execute it:
sudo chmod +x install.sh # Make script executable
Once the installation starts, wait a few minutes until it finishes.
- Since Pi-Hole will make your Raspberry Pi act as a DNS server, it will need to be assigned a static IP address. The installer will give you the option to do so. Or if you already gave a static IP address to your Raspberry Pi, then it will skip this step.
- Pi-Hole will need an upstream DNS provider. Devices on your network will use the Raspberry Pi as a DNS server, which will ignore requests for known ad-serving domains, and use a real DNS server for other requests. I’ll use Google, but you can use any DNS provider offered by the installer.
- Keep the list of DNS trackers all selected.
- For the protocols, in most cases you’ll only need IPv4 on your local network, but you can keep both IPv4 and IPv6 checked.
- The installer will warn you about possible IP conflicts if your router tries to assign the IP address you gave to your Raspberry Pi to another device. In most home networks this won’t be an issue, and most routers are smart enough not to do that.
- You can chose to enable or disable the web interface. I recommend to enable it, since it’ll give you some statistics on how many requests were blocked, the domain names that were blocked, etc… You can always disable it later if you change your mind.
- If you enabled the web interface, you’ll also need to install the web server lighttpd, unless it’s already installed on your Raspberry Pi.
- If you enabled the web interface, you’ll also need to log queries.
Finish the installation
After you finished selecting the options described above, the installation will resume and install the remaining components.
Once it is complete, you’ll be shown the admin password for the web interface. Instead of remembering it, you can change this password using:
pi-hole -a -p
-a is for admin (web interface options), and -p is for password.
You can now check the web interface by going to: http://<IP_of_raspberry_pi>/admin/. It won’t show any blocked request, since no device on your network uses it yet.
Configure your devices
There are two ways to configure your network to use your Raspberry Pi as DNS server.
The first one is to change the DNS server in the DHCP settings of your router. Not every router will allow you to do that, but if your router does, then you only need to change this setting and then every device on your local network will automatically use the Raspberry Pi as DNS server. In case your router doesn’t expose this setting, you can also set your Raspberry Pi to be the DHCP server on your network. In this case, like the article says, be sure to disable DHCP on your router.
The second option is to change the DNS server on each device. I’ll chose this options because my router doesn’t expose the DNS settings. I don’t want to use my Raspberry Pi as DHCP server, because I’ve been burnt several times in the past with SD card corruptions, and if it happens, then I won’t be able to connect to my LAN with any device since it will not have a DHCP server.
Setting the DNS server on your device is very easy. On Mac OS X, simply go to System Preferences / Network / Advanced / DNS. Under the left pane (DNS servers), click the + icon and enter the IP address of your Raspberry Pi. Then click OK and then Apply.
To make sure that Pi-Hole was successfully installed and that your device is correctly configured, you can use this page from Pi-Hole’s developers website, where you should see no ad.
Additionally, you can browse any website that you know displays ads, and see if the ads are hidden or not.
You can then go back to Pi-Hole’s web interface and see that the number of blocked queries has increased. From there if you login on the left menu, you’ll have access to much more information such as the IP addresses of the devices that made queries, the domain names requested and blocked and much more.