Member-only story
Lightweight ad-blocking with dnsmasq and Raspberry Pi
Did you know that you can put that spare Raspberry Pi of yours to work as a caching DNS server and ad-blocker using off-the-shelf packages for Linux?

Now, first of all let’s get something out of the way. Many of us will be aware of the open source project written in PHP called “PiHole.” PiHole automates dnsmasq
and adds a UI dashboard on top, so that you can geek out over the how many adverts you may have blocked.
It’s not that there’s anything against PiHole, but
dnsmasq
is quite capable on its own, and designed to be so light-weight, it is shipped with all Android phones by default.
So why would we want to deal with dnsmasq
, if PiHole is so great? Well, first of all, it means running PHP, a webserver, and a database on your poor little Raspberry Pi, and these are all unnecessary for the task of blocking ads. It also means that many users have little to no understanding of what PiHole is actually doing under the hood.
In this post I’ll show you what dnsmasq
can do on its own. We will setup a Raspberry Pi as a caching DNS server, and then show how it can also be used with a block-list of domains, and finally, how you can get all of your devices on your home-network to use it.
Overview
There’ll be six parts to this post and along the way, I’ll provide my own insights and feedback as I set this up for myself.
- Pre-reqs and configuring the Operating System
- Setting up dnsmasq on RaspiOS (aka Raspbian) as a caching DNS server
- Testing out the caching DNS server
- Sourcing and configuring a block list for ads and privacy trackers
- Configuring our home network to use the new DNS server
- Wrapping up — usability and other approaches to blocking ads
1. Pre-reqs
Once this project is in place, all DNS requests from your devices will be made through the Raspberry Pi. For that reason, you do not want to run this over WiFi, so whether you’re using a Raspberry Pi Zero with a USB ethernet adapter, or a RPi4, may attention here. Half of the benefits of running a local DNS server are to reduce latency, using WiFi will…