Creating a simple adblocking DNS server using dnsmasq

This guide will show you how to create a minimalistic adblocking DNS server. This is a great alternative to Pi-Hole, which requires being run on a Raspberry Pi or in a Docker container.

First, install dnsmasq using your distribution’s package manager.

Next, replace the contents of /etc/dnsmasq.conf with the following:

domain-needed
bogus-priv
no-resolv
server=8.8.8.8
server=8.8.4.4
interface=eth0
listen-address=127.0.0.1
cache-size=10000
local-ttl=300
addn-hosts=/etc/hosts-block

Note: you can remove the interface and listen-address lines if you want dnsmasq to listen on all interfaces.

Next, you will need to download an ad server list. We will be using Peter Lowe’s adservers list in this example, but you can use other ones, such as this one available to use.

sudo curl "https://pgl.yoyo.org/adservers/serverlist.php?hostformat=hosts&showintro=0" -o /etc/hosts-block

Finally, start dnsmasq

# Alpine Linux
rc-service dnsmasq start

# systemd distros (Debian, Ubuntu, Fedora, etc)
systemctl start dnsmasq 

That’s it! Enjoy your new ad-blocking DNS server. Remember to set your routers’ DHCP server to hand out the IP of your new DNS server.