Setting up Pi-hole for Home Ad-blocking

Apr 15 2022

I recently was fascinated by Raspberry Pi and the maker communities behind it. One of the useful projects you can build with Raspberry Pi is Pi-hole. It blocks ads, tracking and malicious websites on every device on your local Wi-fi network, and have an easily manageable web interface.

Caveat: Pi-hole could not block Youtube Ads. Your best bet would be using a browser like Brave (Founded by Brendan Eich, creator of JavaScript) or paying for Youtube premium.

What you will need

  • A Raspberry Pi, for example Raspberry Pi 4 Model B
  • Wi-Fi Router

Connect Raspberry Pi to your local network

First, you would need to connect Raspberry Pi to your local network using ethernet cable or Wi-fi network. Additionally, you might want to enable SSH access on your Pi.

To connect to network:

For remote access, head to Remote access and SSH configuration documentation for setup instructions.

  • In GUI, Go to Preferences → Raspberry Pi Configuration → Interfaces → Enable SSH, then ssh pi@<pi-IP>.
  • In headless mode, create an ssh file and userconf.txt (See Configuring a User) on the boot partition of the SD card.

Installation

You can follow the steps on the official website to install Pi-hole on a Raspberry Pi 4 Model B.

On your Pi, clone the Pi-hole repository and run the installation script:

git clone --depth 1 https://github.com/pi-hole/pi-hole.git Pi-hole
cd "Pi-hole/automated install/"
sudo bash basic-install.sh

Follow through the installation wizard. This article has some good information about the wizard steps.

Router DNS Configuration

Next, you need to configuring the router's DHCP clients to use Raspberry Pi as their DNS Server so that when the clients resolves IP address of an Ad server Pi-hole would block it. To do that you need a couple of things:

  • Raspberry Pi's IP Address
  • Access to router's web admin interface. You can find your router IP using netstat -rn | grep default. This returns the IP of the default gateway, for example 192.168.0.1.

Find Raspberry Pi's IP Address

There are plenty of ways to find your Raspberry Pi's IP address.

Since my Raspberry Pi supports mDNS by the Avahi service, ping raspberrypi.local and ping6 raspberrypi.local from my computer return Pi's IP addresses.

If that's not the case for you, you could try out the nmap method. Run sudo nmap -sn <your-computer-IP>/24 to scan the devices on your local network and look for the hostname containing Raspberry Pi.

Static IP Address

You'll want the Raspberry Pi's IP address to be static so that you won't need to reconfigure Pi-hole and your router if you restart Raspberry Pi.

To set static IP address on a Raspberry Pi, edit /etc/dhcpcd.conf and add the following. You can learn more about dhcpcd here.

interface wlan0 # interface eth0 if you are using ethernet cable
static ip_address=<your-pi-IP>/24
static routers=<router-ip>
static domain_name_servers=

To apply the changes reboot using sudo reboot or bring down / bring up the network interface:

sudo ip link set wlan0 down
sudo ip link set wlan0 up

Also, reserve a static IP for the Raspberry Pi's MAC address in router admin page DHCP settings.

Reconfigure Pi-hole

If the IP Address of the Pi changes, you will need to reconfigure Pi-hole. Opening a terminal from Raspberry Pi and running pihole -r does the job.

Configure Router DNS

Configuring DNS is pretty straightforward: Check the manual of your router brand, go to the router admin page and set DNS override to the IP address of the Raspberry Pi.

Note that it's recommended to use Pi-hole as the single DNS server (Or servers, if you care about redundancy) because your clients might send blocked requests to secondary DNS Servers.

Pi-hole Admin

Go to http://pi.hole/admin or http://<Your-Pi-IP>/admin to manage Pi-hole.

To check Pi-hole service status, run pihole status on the command line on the Raspberry Pi.

You can also use dig to find out the performance of DNS queries. For example: dig www.youtube.com | grep "Query time" .