DEV Community

Cover image for How to Configure Pi-hole: Network-Wide Ad Blocking That Actually Works
Mango Developer
Mango Developer

Posted on Originally published at mangodeveloper.com

How to Configure Pi-hole: Network-Wide Ad Blocking That Actually Works

$32

$33

Prerequisites

  • A Linux box on your network. Raspberry Pi 4 (or 3B+) is the canonical choice; a small NUC, a VM, or a spare laptop works equally well. Pi-hole is light - 50MB RAM, no real CPU. Don't run it on a Pi Zero unless you have no other choice; the SD card will die.
  • A static IP for the Pi-hole host. This is non-negotiable. If your Pi's IP changes, every device on your network loses DNS.
  • Port 53 free on that host. If systemd-resolved is running (default on Ubuntu desktop), it's bound to 127.0.0.53:53 and Pi-hole will conflict. Disable it: sudo systemctl disable --now systemd-resolved && sudo rm /etc/resolv.conf && echo "nameserver 1.1.1.1" | sudo tee /etc/resolv.conf.
  • Docker and Docker Compose installed.
  • About 1GB of disk for the gravity database (the blocklist storage).
  • Admin access to your router for setting DNS server via DHCP.
  • Optional: a UPS. When the Pi-hole goes down, every device on the network loses DNS, which feels like the internet is broken. A $40 UPS prevents this.

Architecture

 Your network
 ------------

 ┌──────────┐ DHCP gives Pi-hole as DNS ┌──────────┐
 │ Client │ ─────────────────────────────────────────> │ Pi-hole │
 │ (phone) │ <─────── 0.0.0.0 for ad domains ────────── │ (Docker) │
 └──────────┘ <─────── real IP for real domains ──────── └────┬─────┘
 │
 │ forwards
 │ non-blocked
 ▼
 ┌────────────────┐
 │ Upstream DNS │
 │ 1.1.1.1 │
 │ 9.9.9.9 │
 │ 8.8.8.8 │
 └────────────────┘
Enter fullscreen mode Exit fullscreen mode

The flow is simple: client asks Pi-hole for google.com, Pi-hole checks its blocklists, finds nothing, forwards to upstream, gets 142.250.x.x, caches, returns to client. Client asks for ads.google.com, Pi-hole finds it in a blocklist, returns 0.0.0.0 immediately, no upstream query.

The cache is important - it means even non-blocked queries are faster than going to upstream DNS directly. Pi-hole caches for the TTL of each record, which is usually 300-3600 seconds.

$34

$35

$36

Allow/Deny Lists and Regex

False positives happen. When a site breaks, you can either:

  • Disable Pi-hole temporarily for that client (the UI has a 30s/5min toggle).
  • Whitelist the domain permanently.

The whitelist is in Group Management → Domains → Allow. You can add a domain, a wildcard (*.example.com), or a regex.

Useful regex patterns:

# Allow all of doubleclick.net (Google's ad subsidiary, sometimes used for legit content)
^([^\.]+\.)?doubleclick\.net$

# Allow a specific subdomain
^telemetry\.example\.com$

# Allow anything matching a pattern
^(api|cdn)\.example\.com$
Enter fullscreen mode Exit fullscreen mode

Pi-hole uses POSIX extended regex by default. Test patterns with pihole -t -q "ads.example.com" - it'll tell you which list/regex matched.

The deny list (Group Management → Domains → Deny) is for adding domains that aren't in any of your lists but you want to block. Useful for blocking a specific telemetry endpoint you've discovered.

Local DNS Records

Pi-hole can serve local DNS records - A, AAAA, CNAME, SRV. This is gold for a homelab. Instead of remembering 192.168.1.50 for your Jellyfin server, you resolve jellyfin.lan.

In the web UI: Local DNS → DNS Records.

Domain IP Type
jellyfin.lan 192.168.1.50 A
git.lan 192.168.1.51 A
grafana.lan 192.168.1.52 A
home.lan 192.168.1.52 CNAME -> grafana.lan
*.apps.lan 192.168.1.60 A (wildcard - useful for k8s ingress)
Enter fullscreen mode Exit fullscreen mode

For wildcards, use Local DNS → CNAME Records with a wildcard pattern, or edit /etc/dnsmasq.d/02-local.conf:

# /etc/dnsmasq.d/02-local.conf
address=/apps.lan/192.168.1.60
address=/jellyfin.lan/192.168.1.50
address=/git.lan/192.168.1.51
cname=home.lan,grafana.lan
Enter fullscreen mode Exit fullscreen mode

Pi-hole reloads dnsmasq automatically when these files change.

For reverse DNS (resolving 192.168.1.50 to jellyfin.lan), use ptr-record:

ptr-record=50.1.168.192.in-addr.arpa,jellyfin.lan
ptr-record=51.1.168.192.in-addr.arpa,git.lan
Enter fullscreen mode Exit fullscreen mode

DHCP Server (Optional, Useful)

By default, your router handles DHCP. Pi-hole can take over DHCP, which gives you two benefits:

  1. Per-client identification in the dashboard. Pi-hole shows hostnames instead of just IPs.
  2. Per-client blocking groups. You can put the kids' devices in a group with stricter blocking.

In the web UI: Settings → DHCP. Enable it, set the IP range (e.g., 192.168.1.100 to 192.168.1.250), and disable DHCP on your router.

I don't usually enable this - most routers do DHCP fine, and you lose router features like static IP assignments via MAC. But if you want per-client groups, it's the way.

Alternative: keep DHCP on the router, but configure the router to hand out Pi-hole's IP as the DNS server. That's the simpler setup and what I recommend.

$37

$38

DNS-over-HTTPS / DNS-over-TLS

If you don't want to run Unbound but still want encrypted upstream DNS (so your ISP can't see your queries), use cloudflared:

# Add to docker-compose.yml
 cloudflared:
 image: cloudflare/cloudflared:2024.6.0
 container_name: cloudflared
 command: proxy-dns
 environment:
 TUNNEL_DNS_UPSTREAM: "https://1.1.1.1/dns-query,https://1.0.0.1/dns-query"
 ports:
 - "5053:5053/udp"
 restart: unless-stopped
 networks:
 pihole_net:
 ipv4_address: 172.20.0.4
Enter fullscreen mode Exit fullscreen mode

Then in Pi-hole Settings → DNS, set custom upstream to 172.20.0.4#5053.

This gives you DoH upstream - your ISP sees only an HTTPS connection to Cloudflare, no DNS queries. The trade-off: Cloudflare sees all your queries. Pick your trust model.

Conditional Forwarding

If you keep DHCP on your router, Pi-hole can't resolve local hostnames by name - it doesn't know that 192.168.1.50 is jellyfin.lan. Conditional forwarding fixes this: queries for 192.168.1.0/24 (reverse lookups) and for lan (forward lookups) get forwarded to your router, which knows the hostnames.

In the docker-compose env:

REV_SERVER: "true"
REV_SERVER_TARGET: "192.168.1.1" # your router
REV_SERVER_CIDR: "192.168.1.0/24"
REV_SERVER_DOMAIN: "lan"
Enter fullscreen mode Exit fullscreen mode

Or via web UI: Settings → DNS → Advanced DNS settings → "Conditional Forwarding."

Now Pi-hole can show client hostnames in the dashboard (instead of just IPs), and clients can resolve each other by name.

$39

$3a

$3b

$3c

$3d

$3e

$3f

$40

$41


Originally published on Mango Developer.

Top comments (0)