Installing Pi-Hole Ad Blocker on a Raspberry Pi Zero

Ads are annoying and they slow down your internet browsing experience.  Here’s how to block them for your whole network using Pi-Hole and a Raspberry Pi Zero.

No-one likes ads. Ads interrupt videos.  Ads crowd up web pages while you’re trying to read. Ads pop up at the worst moment.

There are probably ads competing for your attention while you try to read this right now.

Pi-Hole to the Rescue

Pi-Hole is an ad blocking solution that blocks ads for your whole network – including devices that don’t support ad-blocking browser add-ons like your gaming console or smart TV.

It does this by providing a custom DNS server.  DNS servers tell your computer where to find resources on the internet.  By providing a DNS server, Pi-Hole can block all requests for resources from advertisers before they even reach your device.

What You’ll Need

Here’s what you’ll need to get a Pi-Hole set up.

  • A Raspberry Pi (I’m using a Raspberry Pi zero)
  • SD Card
  • USB Ethernet adapter (recommended if your Pi doesn’t have an Ethernet port)
  • Keyboard/mouse/etc
Everything you need to run your own Pi-Hole.
Everything you need to run your own Pi-Hole.

Download and Install Pi-Hole

The installation instructions for P-Hole can be found at:

https://pi-hole.net

 

The Pi-Hole website - click the blue 'Install' button for installation instructions.
The Pi-Hole website – click the blue ‘Install’ button for installation instructions

 

Pi-Hole installation instructions. We'll be following the second method, which is the easiest.
Pi-Hole installation instructions. We’ll be following the second method, which is the easiest.

But First, Installing Raspberry Pi OS

You’ll notice that Pi-Hole isn’t a stand-alone Linux distribution that can just be flashed onto an SD card for use in your Pi.  It requires that Rasberry Pi OS be installed first.

Here’s how that’s done.  Head over to:

https://www.raspberrypi.com/software/

…and download the Raspberry Pi Imager for your OS.

Download the Raspberry Pi Imager
Download the Raspberry Pi Imager
Run the Raspberry Pi Imager - select the Raspberry Pi OS Lite option, your storage device, and click 'WRITE' when you're ready.
Run the Raspberry Pi Imager – select the Raspberry Pi OS Lite option, your storage device, and click ‘WRITE’ when you’re ready.

pihole 46

When the image has been written, you can remove the SD card and place it in your Raspberry Pi.
When the image has been written, you can remove the SD card and place it in your Raspberry Pi.

Setting up Raspberry Pi OS

Raspberry Pi OS up and running.
Raspberry Pi OS up and running.

Now that you have a fresh install of Raspberry Pi OS, you can go ahead and power up your Raspberry Pi.  There are a couple of steps left to get it configured.

First, run:

sudo raspi-config

You’ll see the Raspberry Pi configuration menu.

Navigate to 'Advanced Options' with the arrow keys and hit ENTER
Navigate to ‘Advanced Options’ with the arrow keys and hit ENTER
Select 'Expand Filesystem' and hit ENTER to ensure the system is using the full capacity of your SD card.
Select ‘Expand Filesystem’ and hit ENTER to ensure the system is using the full capacity of your SD card.
Go back to the main menu and select 'Interface Options'.
Go back to the main menu and select ‘Interface Options’.
Ensure SSH is enabled so that the Raspberry Pi can be connected to remotely.
Ensure SSH is enabled so that the Raspberry Pi can be connected to remotely.

pihole 9

With SSH enabled, you can exit the Raspberry Pi configuration tool and reboot if prompted.

One last thing – make sure you change the default password for the system by running:

passwd

 

Set a Static IP Address on Raspberry Pi

Before set up Pi-Hole and unplug the monitor and keyboard from the Raspberry Pi, a static IP address needs to be configured.

On most networks, by default, when a device connects to your network it will ask your router for an IP address and be assigned one automatically.

This is usually fine, but that address can change over time.  As the Pi-Hole will be acting as a DNS server, and we will want to be able to connect to it via SSH periodically for maintenance, if the IP address changes, we won’t know where to find it.

So, a static IP address needs to be set, so that it wont’t change.

This is done on Raspberry Pi OS by editing the /etc/dhcpcd.conf file.  Below, the file is opened with the nano text editor:

sudo nano /etc/dhcpcd.conf

You’ll see the following screen – it’s the nano text editor.  Navigate with the arrow keys, delete text with backspace, and type as normal:

The nano text editor.
The nano text editor.
Editing the static IP configuration.
Editing the static IP configuration.

Scroll down through the configuration file until you see the line

# Example static IP configuration:

By default this configuration is commented out – the lines beginning with a # are ignored by the system.  Uncoment the lines so that this section looks like the below:

interface eth0
static ip_address=192.168.1.202/24
static routers=192.168.1.254
static domain_name_servers=1.1.1.1

Here’s what those values mean:

  • interface eth0 – this will be the configuration for the interface called eth0 (usually the first/default wired Ethernet connection on a Linux system)
  • static ip_address – this sets the static IP address in CIDR notation
    • Make sure you use an IP address that won’t be automatically assigned by your router
  • static routers – sets the router for your network
    • Set this to the IP address of your existing router
  • static domain_name_servers – your Pi-Hole will be acting as a DNS server, but the underlying Raspberry PI OS will need a server for connecting to the internet to perform software updates, etc
    • I’ve entered 1.1.1.1 for CloudFlare’s DNS server.  You could also use 8.8.8.8 which is the address for Google’s DNS service

Notice I’ve left out the IPV6 configuration to keep things simple – most home networks do not use IPv6.

Press the CTRL + X keys to quit the nano text editor.  Press Y to save and confirm the changes.

Reboot to apply the changes:

sudo reboot

 

Log back in and run the following commands to make sure your Raspberry Pi OS software is up to date:

sudo apt update

sudo apt upgrade

Installing Pi-Hole on Raspberry Pi OS

Pi-Hole can now be installed on top of Raspberry Pi OS!

Run the following command to download the installation script:

wget -O basic-install.sh https://install.pi-hole.net

Execute the installation script by running:

sudo bash basic-install.sh

If all goes well, you’ll see the following screen:

Pi-Hole installation script.
Pi-Hole installation script.
Press 'OK' when you're ready to continue the installation.
Press ‘OK’ when you’re ready to continue the installation.
We've already configured the static IP address, so we're all good.
We’ve already configured the static IP address, so we’re all good.
When selecting which network interface Pi-Hole will run on, make sure you select the same one you configured your static IP address on.
When selecting which network interface Pi-Hole will run on, make sure you select the same one you configured your static IP address on.
Select an upstram DNS provider. This is the provider Pi-Hole will use to look up addresses before filtering them for ads.
Select an upstream DNS provider. This is the provider Pi-Hole will use to look up addresses before filtering them for ads.
Pi-Hole requires a block list (which lists all known sources of ads) to work. One is suggested, so select it and continue.
Pi-Hole requires a block list (which lists all known sources of ads) to work. One is suggested, so select it and continue.
This will configure Pi-Hole to use the static IP previously set.
This will configure Pi-Hole to use the static IP previously set.
You may receive this warning. It can be ignored, provided you have set the static IP to be outside of the range automatically assigned by your router.
You may receive this warning. It can be ignored, provided you have set the static IP to be outside of the range automatically assigned by your router.
Enable the option to install the web interface, it's useful.
Enable the option to install the web interface, it’s useful.

pihole 35

pihole 36

Above, various options were set for Pi-Hole and the web interface. These defaults are fine for most users.
Above, various options were set for Pi-Hole and the web interface. These defaults are fine for most users.

 

pihole 40

Done! We can check in on the web interface to make sure everything is working by navigating to the static IP address set previously
Done! We can check in on the web interface to make sure everything is working by navigating to the static IP address set previously

Success!

P-Hole is now configured.  You can disconnect your monitor and keyboard from the Pi.

Leave the Raspberry Pi plugged in somewhere with power and network connectivity.

Manually Setting DNS Server to use PiHole

Your other devices will still be receiving their DNS server details from your router.  You’ll need to manually set the DNS server on each device (or in the DHCP settings on your router if your router supports it).

You’ll need to set the DNS server on your devices to point to the IP address you set manually for your Raspberry Pi in the previous steps.

Manually Setting DNS Server on Linux

Click here to view our article on how to manually set your DNS server on Linux

Manually Setting DNS Server on Windows/Mac/iOS/Xbox

Click here to find out how to change DNS settings on other operating systems

SHARE:
Photo of author
Author
I'm Brad, and I'm nearing 20 years of experience with Linux. I've worked in just about every IT role there is before taking the leap into software development. Currently, I'm building desktop and web-based solutions with NodeJS and PHP hosted on Linux infrastructure. Visit my blog or find me on Twitter to see what I'm up to.

Leave a Comment