Part 3: Setting up Pi-Hole on your Raspberry Pi
This is the third article in my ongoing series of posts, called Baking a pie to block ads, focused on using a Raspberry Pi to block ads on your home network (and even outside of it, which I will cover here).
Part 1 covers the introduction (to servers, DNS based ad blocking and Raspberry Pis) and part 2 walks you through setting up your R-Pi. If you have read those and/or are already familiar with these, read on.
Why are there ads all over the internet?
Whenever you open a website or an app, you might see ads on there which are sometimes super annoying, intrusive and loud. Some ads might even download/inject some malicious code on your device(s) which could harm you or the device in multiple ways.
Not all ads are bad though, sometimes these ads are helpful because they help the free websites make money as they might not have any other revenue streams and this is the only way they can monetize their free content. So consider allowing ads on such websites.
But there are still other reasons to block ads:
- Publishers which make you pay/subscribe but still show ads
- Publishers which pop up an ad every time you click something
- Publishers which try to download some malware on to your device
- Publishers which will their pages with loud, intrusive and annoying ads
- Protecting kids from accidentally seeing some bad/traumatizing ad
- Avoid being tracked by the trackers on these ads and 3P cookies
- …the list goes on and on.
Take your pick. Chances are, if you’ve made it this far into the article, you’ve already decided to block ads and the reasons don’t matter to you.
As it is with everything in existence, advertising is a tool, and when weaponized, it can do great damage. So the purpose of blocking these ads is to minimize the damage it can do.
If you have questions like “How does 1 small computer block ads on my whole network?” or “How do I block ads outside my home network?” or “Can I still analyze and control what is being blocked?”, please be patient because all of these will be answered in either this or the next article.
What is a Pi-Hole?
Simply speaking, a Pi-Hole is like a black hole for all ad-requests from your network (if you want to know more about how ads are requested, please read the introduction of the series here)
It is a great tool which has the ability to block traditional website advertisements as well as advertisements in unconventional places, such as smart TVs and mobile operating system advertisements. You can read more about the Pi-Hole here.
It is completely open source and free (FOSS) and although it was designed to be used on a Raspberry Pi, it can be installed on pretty much any Linux based machine (you can follow these same steps to install Pi-Hole on any other old computer that you can leave powered on 24x7).
How do I install Pi-Hole on my Raspberry Pi?
I’m assuming you have either a Raspberry Pi with Raspbian OS on it (or any other distro) or any other Linux computer.
Follow the steps below to install and configure Pi-Hole on your Raspberry Pi :
- SSH into your Raspberry Pi (or if you have it hooked up to a display, open up the terminal application) and enter the password
ssh pi@<IP address of your pi>:
Note: You can read part 2 of this series here for more details about SSH, how to install and login to your pi
- Download and install Pi-Hole by using one of the following 3 methods:
- Alternative 1: One step automated install — Recommended
sudo curl -sSL https://install.pi-hole.net | bash
“sudo” is basically the admin mode as the Pi-Hole requires root/admin privileges to install.
- Alternative 2: Install by cloning the directory
git clone --depth 1 https://github.com/pi-hole/pi-hole.git Pi-hole
cd "Pi-hole/automated install/"
sudo bash basic-install.sh
Here the first command clones the github repository, the second line enters that directory and the third one runs the installer
- Alternative 3: Manually download the installer and run it
wget -O basic-install.sh https://install.pi-hole.net
sudo bash basic-install.sh
Here, the “wget” command downloads it and the next line runs it.
I’ve used the first method and all the following steps will be based on that but even if you use any of the other 2 methods, everything will be the same. Once it starts installing, you should see the following screen on your terminal
- Once the initial config is done (as shown in the previous step), you will be greeted with the following screen:
- Click OK to proceed and you will see a screen as follows:
- Your router generally assigns IP addresses to devices connected to it and it might sometimes assign different IP addresses to the same device but we need our Pi-Hole device to get the same IP address from the router every time so that it functions properly even if it gets disconnected and reconnects to the router. This is basically what the “Static IP Needed” screen is warning you about.
Click OK to proceed and it will take you to the following config screen:
- Use your arrow keys to navigate to “wlan0” and press the spacebar to select it. This is basically telling what network interface to use. Since I’m using my Pi wirelessly (Wi-Fi), I selected wlan0. If you have your Pi hooked up to your router directly, use “eth0”. Once you’ve selected it press enter and you’ll be taken to this screen:
- I can write another series of articles about what is DNS and how it works, but for now it’s basically a phonebook for the internet. You can read more about DNS in part 1 here or watch this video. In simple words, we basically need an upstream DNS provider here so that if some new domain is requested and Pi-Hole doesn’t have it in it’s cache or blocklist, it will go to that upstream server to resolve it.
You can do more research about what provider to use, but I went with OpenDNS (you can also go with Google or Cloudfare or any other custom DNS). Use the arrow keys to select and press enter and then you’ll see this screen:
- This is basically the default blocklist which the Pi-Hole uses. It comes with ~58k domains which are blocked. You can add more lists and domains once you have installed Pi-Hole. Press enter to continue and you’ll see the protocols screen:
- No need to change anything here as we need to block ads over both IPv4 and IPv6 addresses. Read more about these here. Press enter to continue.
- Press enter to use this IP address as a static IP address or select No and enter the address you want to use, if you know what you’re doing. Press enter and you’ll see a warning about IP conflicts which you can ignore for now if you don’t have a complicated home network setup (if you do, you probably don’t need this tutorial :P). Press Enter on the warning screen
- Now you’ll see the IPv6 address that will be used to block ads over IPv6. Press Enter again and you’ll see the following screen:
- With “On” selected, press Enter as we want a web admin interface because it makes it easier to visualize and analyze all the traffic to and from our Pi-Hole server. The next screen you see is this:
- Press Enter as we want these modules for proper functioning of our web server.
- Press Enter as we want to log all the queries (all the internet requests) for analyzing them later and you get this screen:
- Now if you are the only admin who will be seeing the web admin interface AND this is only for your home network, you can select the “Show everything” option. Otherwise, choose the appropriate option and press enter (depending on the privacy policies of the org where you are using this)
- Now the all the configurations are done and Pi-Hole is installing, it will take a couple of minutes and you’ll see something like this:
- And you’re done! Pi-Hole is up and running.
Testing the Pi-Hole:
Open any browser on your home network and open the web interface by typing the web interface address given once the installation is complete. Also make sure you note down the web interface password (you can always change it later) because you’ll need it to login to the admin interface.
This is the the non-admin interface:
- Click on “Login” and enter the admin credentials and login and then you’ll see the admin interface (I’ve removed the top permitted domains section)
This web interface deserves its own series of articles (which I will definitely be posting later). But it basically has everything you can imagine for analyzing your network traffic. You can see all the connected clients, the domains requested, blocked and permitted domains, client information, queryable database etc. Feel free to explore it!
Setting up your router/device to use Pi-Hole as your DNS
There are 2 ways to do use the Pi-Hole as a DNS server:
- Configure the router to use Pi-Hole as the default DNS server for all DHCP clients(recommended)
- Configure each device manually to use Pi-Hole as the DNS server
I would recommend using the first method as it’s a “set it and forget it” solution. You configure your router once, and then any device which connects to it will have all the ads blocked.
For the second method, you will have to manually configure DNS settings on each device and it’s slightly different for each device. So I’ll be not using this method, but if you do need to use this, leave a comment and I’ll explain.
Steps to configure your router:
- Login to your router’s configuration page (usually 192.168.1.1) and login using the admin credentials (usually given in the documentation provided by your ISP or at the back of your router)
- Navigate to the DHCP/DNS settings section (usually under the LAN section, you might need to click “Advanced” settings for this)
- If you’re having trouble, you can just try Googling it based on your router’s model number. Ex “TP Link archer DNS setting”
- There will be a field (or multiple) for specifying DNS servers. Enter the Pi-Hole DNS server there and click save (no need to change anything else for now)
The setup is finally done! Now any device which connects to your router, will be able to take advantage of the Pi-Hole. Login to the dashboard again and you’ll start seeing more data based on the clients connected and their activities. EXPLORE!
Now you can analyze all your network traffic and get all kinds of insights and you can keep tweaking your blocklist to improve the ad blocking. I’ll be write about the insights and findings I have gathered by analyzing my Pi-Hole and that’s the super interesting part!
All these steps are to block ads on your home network. In the next article, I will show you how to use this outside your home network so that you can use it on your phone, on the go even on a 4/5G network, so basically you’ll never see ads again (most of the time)!
Stay tuned, leave a comment if you have any questions and as always, constructive feedback is welcome :)