How To Set Up Pi-hole on Raspberry Pi Zero To Block Ads on Your Network
By: Yash Wate
Tags: Raspberry Pi Zero, Pihole, Networking, Raspberry Pi, DIY

How To Set Up Pi-hole on Raspberry Pi Zero To Block Ads on Your Network Yash Wate Follow Mar 17 · 8 min read

Protect your network from ads and tracking!

Annoyed with ad-services tracking your activities online and serving eerily relevant ads anywhere you go on the internet? You need Pi-hole.

Pi-hole

Pi-hole is a DNS sinkhole that blocks ads while you surf the web. It is essentially an advertisement and internet tracker blocking application, which you can install on any Linux system, be it an SBC (Raspberry Pi) or a Router.

In this guide, we will walk you through setting up a Pi-hole on a Raspberry Pi. The Raspberry Pi model we will be using for this project is the Raspberry Pi Zero. But before we proceed, let’s address a few questions.

What is Pi-hole, and what does it do?

Pi-hole is a network-level application that blocks trackers and advertisements on the internet, so you can browse the internet without being tracked or bothered by advertisements.

By using Pi-hole as a private DNS server for all of your devices, you turn it into a DNS sinkhole. A DNS sinkhole, for the uninitiated, is a DNS server that returns false DNS results for a query.

Once Pi-hole is set up, you can point your devices to use its IP address such that all traffic on these devices is routed through it. That way, when a website (that serves ads) requests its ad servers’ domain to retrieve an ad, Pi-hole checks their domain name with its record of blacklisted ad-services.

If there is a match, Pi-hole refuses to resolve the domain and returns the requesting device with a dummy address. And, in the process, blocks out ads on the website. On the other hand, if no match is found, the website receives the requested ad from the domain and displays it up on the website.

What you’ll need to set up Pi-hole?

Raspberry Pi Zero Raspberry Pi Zero case (or some other box) Power adapter USB hub RJ45 to USB adapter Mini-HDMI to HDMI adapter MicroSD card (preferably at least 16GB) Keyboard

Pi-hole project prerequisites

How to set up Pi-hole on Raspberry Pi Zero?

After you have all the pre-requisites at your disposal, you are ready to start the installation process. To do this, we first need to set up our Raspberry Pi Zero and then move on to installing Pi-hole on it.

Step 1 – Install Raspberry Pi OS (erstwhile Raspbian)

Raspberry Pi Zero, or any other Raspberry Pi model for that matter, needs an operating system before it can run any software or application. While there are different ways to go about this, we recommend using Raspberry Pi Imager. Imager is an easy-to-use utility that lets you conveniently flash an image onto a microSD card. You can download Imager from here. And once downloaded and installed, run it.

Plug-in your microSD card — either using an adapter or a card reader — to your computer. Now, in Imager, click on CHOOSE OS and choose Raspberry Pi OS Lite. Similarly, click on CHOOSE SD CARD and select your card.

Raspberry Pi Imager

Finally, hit WRITE to start flashing Raspberry Pi OS on your card.

Step 2 – Boot up Raspberry Pi

As soon as the flashing process finishes, remove the microSD card and plug it into your Raspberry Pi Zero. Subsequently, connect the monitor, power supply, keyboard, and ethernet to the device as well.

Your Raspberry Pi Zero should now boot up. While it does that, you can see some status codes on your monitor — this is normal. After the initial boot sequence finishes, you get a login prompt.

Raspberry Pi login screen

Enter the following (default) Raspberry Pi credentials in the terminal window:

username: pi

password: raspberry

And hit Enter. If all goes well, you should be logged in to your Raspberry Pi.

Step 3 – Configure Raspberry Pi

While you are logged in to your Pi, you need to configure some of its settings before you can use it. Fortunately, the system has a handly configuration utility called “raspi-config”, which makes this process fairly simple.

The Raspberry Pi configurations you need to change include those for the keyboard layout, the time zone, and the region. In addition, you also need to enable SSH (Secure Shell) for accessing your Raspberry Pi remotely.

To change these settings, make sure you are logged-in and enter the following command in the terminal window:

sudo raspi-config

You should now see a graphical configuration window with a host of settings. To navigate through these settings, use the navigation keys to move the cursor around and hit the Enter/return key to select.

To change the hostname, in the raspi-config window, select System Options and choose Hostname. Here, enter your preferred hostname for your Raspberry Pi and press Enter/return to save. Likewise, select Password from System Options and enter your password twice.

Raspberry Pi configuration (raspi-config) utility screen

Next, you need to go to Localisation Options in the primary raspi-config menu, and from there, change the settings for Locale, Timezone, and Keyboard one-by-one.

Finally, to enable SSH, head over to the Interface Options and choose SSH. When prompted, select Yes to enable SSH on your Raspberry Pi.

With all those configurations done, choose Finish. And on the next screen, select Yes to reboot Pi and apply your changes. When the Pi boots up, log-in with your username and password.

Lastly, we need to assign a static IP (Internet Protocol) address to the Raspberry Pi to prevent your router from assigning a new IP address to the Pi every time it connects to the network. To do this, first, get the static IP address currently assigned to your Raspberry Pi Zero. The easiest way to do this is to type in arp -a in the terminal window, which presents you with the interface, the IP address, and the MAC address of your Pi. Alternatively, you can head to your router’s configuration page to view these details.

Once you have the IP address, enter the following command in the terminal:

sudo nano /etc/dhcpcd.conf

and enter/modify the following details:

[hostname] interface eth0

static ip_address=[IP address with subnet mask]

static routers=[router gateway address]

static domain_name_servers=[router DNS or other preferred DNS server]

Note: this goes without saying that the static IP address you plan to assign to your Pi should not be allocated to any other device on your network.

Setting up static IP for Raspberry Pi OS

With the configuration file populated with the above settings, hit CTRL+o or control+o to write them. Finally, type in reboot in the terminal to restart your Raspberry Pi.

Step 4 – Install Pi-hole on Raspberry Pi

We are now on to the final step in the process, which is to install Pi-hole on the Raspberry Pi. For this, enter the following command in the terminal window:

curl -sSL <https://install.pi-hole.net> | bash

And hit Enter\return. The above command may seem odd to some — piping curl to bash — if you are doing it over HTTPS and are aware of the source you are installing the software from, you should be fine. Using that command makes the installation procedure efficient and quick.

Pi-hole installation screen

The Pi-hole installer should start now, and all you got to do from hereon is to follow the on-screen instructions. A few screens you need to particularly pay attention to include those for static IP, upstream DNS provider, and ad-services blacklist. For static IP, make sure it matches the one you added to the configuration file, and for upstream DNS, select the DNS service you prefer (Google, Cloudflare, etc.).

In addition to the above screens, you must also carefully attend to the screens that ask for web admin interface installation and web server installation. Likewise, on the screens that follow, you can select to log queries and set privacy mode depending on your preference. If you are setting Pi-hole on your home network, you can enable both options. However, if you are in an office setting, you will need to select an option sensibly.

Finally, once the installation is complete, you are presented with a screen that contains all the necessary settings for your Pi-hole. Make sure you note-down these settings or take a snapshot as you will need them later.

How to use Pi-hole?

With Pi-hole running on your local network, all you have to do to use it and block ads is configure the DNS. There are two ways to do this: you can either make Pi-hole’s IP the default DNS for your entire network, or you can point all your devices to this IP address to route their traffic through Pi-hole.

The former approach requires you to change the DNS client for your network from the router’s settings, while the latter approach only requires you to change the DNS server on each of your devices from their Wi-Fi settings.

Pi-hole dashboard

Blocking ads with Pi-hole on Raspberry Pi Zero

If you followed the instructions up until now, you should have Pi-hole installed and running on your Pi Zero. And it should be able to block ads across different websites. You can check if Pi-hole is working fine or not by going to the Pi-hole web dashboard.

The Pi-hole dashboard offers insights into your Raspberry Pi’s status, along with blocked queries and blacklisted domains. At any time, if you find the need to blacklist or whitelist a domain, you can make the change directly from this dashboard once you log in. [Dashboard login password is one that you were given after installation was complete].

In the event you are still getting ads on some websites, like YouTube, it is because blocking ads with Pi-hole can be hit or miss with some websites, especially those that serve ads from their own domain. And therefore, Pi-hole can occasionally fail to block the domain from displaying ads in such scenarios.