Network-Wide Ad Blocking with Pi-hole on Raspberry Pi
Ohidur Rahman Bappy
MAR 22, 2025
Introduction
Network-wide ad blocking can significantly enhance your online experience by eliminating unwanted advertisements across all your devices. In this guide, we'll walk you through setting up Pi-hole on a Raspberry Pi for efficient ad blocking.
Prerequisites
Before we start, ensure your Raspberry Pi is up and running with an internet connection. A basic understanding of command-line operations will be helpful.
Disabling the Default DNS Server
First, disable any existing DNS server running on port 53:
sudo systemctl stop system-resolved.service
sudo systemctl disable system-resolved.service
Update your DNS resolver:
sudo nano /etc/resolv.conf
Replace the existing nameserver with Google DNS:
nameserver 8.8.8.8
Installing Pi-hole
-
Prepare for Installation
- Pi-hole is a software that configures a DNS server to block specified domains and provides a web interface for management.
-
Run the Installation Script
- To install Pi-hole, execute the following command:
curl -sSL https://install.pi-hole.net | sudo bash
-
Follow the Setup Wizard
- The installation wizard will guide you through setting up your Raspberry Pi as a network-wide ad blocker.
-
Donate (Optional)
- Consider supporting Pi-hole development by following donation steps when prompted.
-
Set a Static IP Address
- Configure a static IP through your router using DHCP reservation in addition to the wizard's setup.
-
Choose an Upstream DNS Provider
- Use the <kbd>ARROW</kbd> keys to select a provider like Cloudflare, and press <kbd>ENTER</kbd>.
-
Manage Block Lists
- Select or deselect third-party block lists using <kbd>ARROW</kbd> and <kbd>SPACEBAR</kbd>.
-
Set Protocols
- Choose the protocols for blocking ads, typically both IPv4 and IPv6.
-
Confirm Network Settings
- Ensure the displayed settings for the static IP are correct.
-
Web Admin Interface
- Install the web interface to easily manage Pi-hole settings and view statistics.
-
Logging DNS Queries
- Recommended to keep logging on for tracking domains and viewing analytics.
-
Privacy Settings
- Choose your preferred level of privacy, with "0 Show everything" providing complete stats.
Accessing the Pi-hole Web Interface
After installation, access the Pi-hole admin interface via:
http://<Your_RPi_IP>/admin/
- Log in with the provided password.
- Explore dashboards like Query Log, Whitelist, Blacklist, and more.
Additional Configuration
-
Change Password: Use the command
sudo pihole -a -p
. -
Install Additional Packages:
sudo apt install php-sqlite3 sudo apt install libsqlite3-dev usermod -aG pihole www-data ``
-
View Logs:
sudo tailf /var/log/apache2/error.log
-
Repair/Reconfigure: Use
pihole -r
.
Docker Setup
Here's how to set up Pi-hole with Docker:
-
Create Docker Volumes
docker volume create pihole_app docker volume create dns_config
-
Disable Default DNS Resolver
sudo systemctl stop systemd-resolved sudo systemctl disable systemd-resolved
-
Setup Docker Compose File
Create a
docker-compose.yml
:version: "3" services: pihole: container_name: pihole image: pihole/pihole:latest ports: - "53:53/tcp" - "53:53/udp" - "67:67/udp" - "80:80/tcp" environment: TZ: 'America/Dhaka' WEBPASSWORD: YOUR_VERY_SECURE_PASSWD volumes: - './etc-pihole:/etc/pihole' - './etc-dnsmasq.d:/etc/dnsmasq.d' cap_add: - NET_ADMIN restart: unless-stopped dns: - 127.0.0.1 - 8.8.8.8
- Clear DNS Cache: Run
ipconfig /flushdns
andping youtube.com
to test.
Conclusion
Setting up network-wide ad blocking with Pi-hole on a Raspberry Pi is a powerful way to control web content across all devices. With the added flexibility of a web interface and Docker setup, Pi-hole offers a robust solution to enhance your browsing experience.