Search This Blog

Saturday, November 9, 2024

Raspberry Pi: Pi-hole network wide adblocker

These days our browsing experience can feel very cluttered with ads and it has been like that for many decades. I remember how quiet the internet browsing experience became with the first adblockers. My first addon for this purpose was Adblock Plus, later on I switched over to uBlock Origin. Since I have many devices, including phones it requires me to manage these addons on multiple devices.

It was a few years ago that I heard of Pi-hole and I liked the idea that it would remove ads even from my phone and the games I play there. That it would remove many ads on multiple devices was also a clear selling point.

Over time my understanding of how a network is built has increased and so has my understanding of Raspberry Pi, to the degree that I now felt ready to try create a Pi-hole installation and run it at home.
My current router is locked down from my ISP point of view, so I tested it by redirecting a device to the Pi-hole for testing.

This is a practical guide of how I did it. What I used is: Raspberry Pi 5 (8gb RAM), SD card for the OS, USB-C charger. 

Creating a Pi-hole installation

1. Download Raspberry Pi Imager to flash the OS to your SD card: https://downloads.raspberrypi.org/imager/imager_latest.exe
The operative system image is already included in your Raspberry Pi Imager.

2. Install it by running as admin and run the program after the installation is complete.

3. The GUI of the program let's you choose your Raspberry Pi Model. In my case I took the 5.

4. Choose the OS "Raspberry Pi OS Lite (64-bit)" it has no GUI, so it is all command line.
Pi-hole is compatible with Ubuntu and Debian as well, along with a few other Linux distros.

5. Set options such as SSH-access and the proper wifi-settings so the Raspberry Pi connects to your wifi directly. In the future I plan to use a regular Cat 8, 0.5m ethernet cable or something similar. Put the finished SD card in your Raspberry Pi and start it up, it should then connect to your network.

6. To SSH into it you will need to know the IP address, you get this by looking in your router's list of connected devices. At this stage you also want to lock the IP address, this is because you will lose network connectivity if the router/device won't find the Pi-hole at the given IP in the DNS-settings.

7. It is time to start PowerShell as admin now and install Pi-hole on the headless Raspberry Pi OS Lite. Write SSH and the username@IP. 
Example: SSH admin@192.168.0.14
The password is the same you put in the Raspberry Imager setup for SSH.

8. Once successfully connected you will get a command line available to you.
Run sudo raspi-config to set details for your computer. If you did the settings in the Raspberry Pi Imager correctly, you won't need much config here. 

9. When you have double checked your settings, run sudo curl -sSL https://install.pi-hole.net | bash
This will start the installation process of the actual Pi-hole software.

10. Make sure your Pi has a static IP and then continue through the prompts.
Add Cloudflare as uplink DNS, install the premade list and install the web server.
Enable query logging.
Show everything.
Then let the script work.
Take note of the login password, and access the page using the IP/admin.
Example: 192.168.0.14/admin

Now you have set up your Pi-hole and the next step to actually block ad traffic is to direct it through the device.

Directing traffic through the Pi-hole

Debian 12
Open terminal and write the following commands:
sudo su
nano /etc/resolv.conf

Replace the pre-existing numbers with the IP-address of your Pi-hole
nameserver 8.8.8.8
nameserver 8.8.4.4

Click ctrl+x to save and then confirm with Y.

Windows 11
Press ctrl+r
Write ncpa.cpl and hit enter
Right-click your adapter of choice and take properties
Go into the properties of IPv4
Enter a manual DNS (the IP of your Pi-hole device)




Your router
Since the GUIs of routers differ this is the general guideline.
Logon to your router by browsing to its IP (you can find this gateway IP by doing ipconfig)
Set both primary and secondary DNS addresses to your Pi-hole's IP. Should you set the secondary IP to 1.1.1.1 for example, then it will use the backup DNS if your primary goes down.