In this in-depth Pi Day project spotlight, I’ll walk you through the steps to turn a Raspberry Pi into an ad-blocking DNS server for your network. Many people use ad-blocking browser add-ons to reduce the amount of distracting and unnecessary information displayed on the pages visited, but this method goes further. Unlike a browser-based ad-blocker, this method will prevent ad domains from ever being contacted in the first place, saving you bandwidth and page load time. It will affect all devices that use your network, requiring no installation or configuration on those devices. You will, however, have to reconfigure the DNS settings in your router, so you will need access to it. Familiarity with basic networking concepts is assumed, but no prior knowledge of Linux is necessary. These instructions have been written with the Pi 3 model B and it’s standard “Raspbian/Raspberry Pi OS” in mind, but the process should be similar for most models and configurations.
You will need:
– Raspberry Pi model B board
– Raspberry Pi case (optional)
– Micro USB cable
– 5V 2.4A USB DC power adapter
– 8GB or larger microSD card (standard size SD card if you are using a first-generation Pi)
– SD card reader (most laptops have this built in)
– Ethernet cable, cat 5 or cat 6
– Router with spare ethernet port
– Ability to log in to the router and change settings
– HDMI cable, HDMI-capable monitor or TV (for initial setup only)
– USB keyboard and mouse (for initial setup only)
Install the operating system:
Insert the SD card into your computer’s slot and make sure it is formatted to FAT32 or exFAT. Go to this page and download the SD card imager that is appropriate for your PC OS. Run the imager and choose which model of Pi you have, and it should automatically recommend a Pi OS that is appropriate for your model. You may also need to specify which SD card (drive letter) you are writing to, but in most cases the default will be fine.
Once this is done, set up the Pi peripherals, insert the card in the slot on the underside of the Pi board, and connect the Pi to your router via ethernet. Plug in the Pi. Go through the OS setup options once it boots and it will restart.
Now that the OS installation is finished, it would be a very good idea to change the password to something other than the default, particularly since it will be used as an always-on network attached device. Go to Raspbian’s “start button” (at the upper left by default) then “Preferences” then “Raspberry Pi Configuration.” Click the “Change Password” button at the upper right of the first tab and enter a new password.
Install and configure the ad-blocker:
You will need to use the command line for this step. This can sometimes be intimidating or off-putting for many people, but it’s not a big deal here as you will only need to enter one command and then follow the automated process afterwards. Start by clicking the icon for the terminal on Raspbian’s taskbar:
It will open a black window with a prompt. Copy the following command and paste it into the window:
curl -sSL https://install.pi-hole.net | bash
You can also find additional information and installation options for the ad-blocking software at: https://pi-hole.net/
This command sends the install script at https://install.pi-hole.net to “bash,” which is the name of the command line utility.
- Once the command appears in the window, hit enter. It will display some lines of text and ASCII art, then it will load into a simple graphical interface, which will guide you through the remainder of the installation process.
- The first three boxes it will show you contain some general information, read them and hit enter for “Ok.” One of the boxes will mention that the Pi will need to be given a static IP address, we will discuss this process a bit later in the guide. You can hit “Continue” here, or you can skip to the “Configure your router section” of this guide and return here when you’re done. The installation should work fine even if you assign the IP after the installation, as long as the IP values assigned are consistent.
- The next box asks which network adapter to use. I recommend using the wired ethernet adapter (eth0) as opposed to wifi (wlan0).
- The following box confirms IP settings. My personal situation is such that I leave the gateway as the default, but if your network is configured differently, then you will need to change these values. For example, if your router’s IP is 192.168.0.1 instead of 192.168.1.1, you will need to change both values to reflect this. If you have a particular IP you want to use for the Pi, you can specify it now; just make sure whatever IP is used is also assigned to the Pi in your router’s DHCP settings, which is elaborated on in the “Configure your router” section. Make a note of the values you have assigned before continuing, as you will need them later. The confirmation box after this step will provide a bit more information about this as well.
- The following box will ask you which upstream DNS provider you will use, which will provide DNS translation for all domains that aren’t blocked. My personal preference is OpenDNS.
- Another box after that will ask if you want to include a particular third-party block list. I haven’t encountered any problems with allowing this. You can also customize block lists after the installation is complete if you want, but it hasn’t been necessary in my experience.
- It will then ask if you want to install the web admin interface. I recommend doing this, but if you plan to also use the Pi as a web server, this will interfere with that.
- The following dialog will ask if you want to log queries. The default of yes should be fine for most.
- The next box asks about FTL privacy levels. If you are extra paranoid you may want to change this, but the default selection should be fine for most.
- Finally, it will then return to an automated section of the installation process.
- Once that is done, there is one last dialog box: this one is important, so don’t skip it. Make note of the device’s IP if you haven’t already, the URL for the web admin page, and the web admin password.
This completes the Pi-Hole installation, reboot the Pi.
Configure your router:
This step is very important, but unfortunately, I will only be able to offer limited guidance, because the exact details will vary depending on your network configuration and router firmware.
Open your web browser, go to the URL bar, and enter your router’s IP address, this will typically be 192.168.1.1 or 192.168.0.1 for most home networks.
If you entered the right address it should present you with a login page. Enter your credentials. If you don’t know them, you will likely need to reset and reconfigure your whole router again to regain access, which is beyond the scope of this tutorial.
Once you have logged in to your router it will likely show you a series of tabbed configuration panels.
You will need to change your DHCP reservations and your DNS server settings. On most routers that I have owned, these have both been under the section for DHCP.
When you have found the DHCP reservation table, it will show you which MAC addresses (permanent network hardware identifiers) have been assigned to which IP addresses by your router. Most modern routers will show a device name as well. Make sure the Pi is assigned the IP address it was told to use during the installation process, and that no other devices are assigned to that IP address.
- If you are having trouble identifying which device is your Pi in the list and don’t know its MAC address, go back to the Pi’s command line interface and enter the following:
ifconfig eth0
- On the fourth line of the output, it should say something similar to:
ether c7:35:ce:fd:8e:a1
- The part with colons will be the Pi’s MAC address.
Once the DHCP reservations have been adjusted, find the DNS server setting in your router’s interface. It will likely be under the same or similar section as DHCP. Change it to the IP address you assigned to the Pi. Make sure the settings you have changed have been applied/saved. You should not need to reboot your router.
At this point, your Pi-based ad blocker should be functional. The best way to check on it is to bookmark the admin web page mentioned earlier. It will show you some statistics as well as a useful graph showing total queries vs blocked queries. If you login, it will show you more info, as well as allow you to temporarily disable the ad blocker if it is interfering with something you are trying to do.
If it does not appear to be blocking anything initially, you may need to wait awhile for the DHCP changes to take effect, and requesting more pages will give it more information to display on the graph if it is working. It is also important to note that if you are using a third-party VPN connection on your device, it will bypass your local network’s Pi DNS and use the VPN’s DNS instead, and so the ad-blocking functionality provided by the Pi will not work on that device while it is connected to the VPN.
In my experience so far, it has needed almost no maintenance (though occasional updates to the OS, admin interface, and blocklists are recommended) to continue doing its job reliably, and any issues it had were fixed by simply restarting the Pi.
Hopefully this guide provides you with a practical, day-to-day use case for your Raspberry Pi as well as an interesting tech project.
More resources about the Pi-Hole ad-blocking software can be found at: https://pi-hole.net/ and more resources about the Raspberry Pi itself can be found at https://www.raspberrypi.com