Ad blocking with Unbound in Linux (and FreeBSD)
Introduction
Unbound is, according to the manpage, “Unbound DNS validating resolver 1.16.3.”, Then it says that it is a caching DNS resolver. It is developed by NLnet Labs and it’s under the BSD license.
You might know the Google service “8.8.8.8” and Cloudflare’s 1.1.1.1. Those are sold as “Fast”, “Secure”, and blah blah DNS servers, but are they faster than installing a local DNS server? Clearly not, and this also allows you to configure the DNS server. How long to keep the cache, or which domains you do not want to resolve (for whatever reason).
Installation
What should be done here is to install Unbound in your router, but as
that is not an option for me nor for the most people reading this
post. We will just install it locally. I am using Void, so change the
command as needed for your distribution. If you’re using FreeBSD, you
can use local-unbound (It is better than regular unbound as it is
optimized for local use, but we don’t have that in Linux)
OpenBSD users might be interested on using unwind instead. It is simpler.
# First, we install Unbound
$ sudo xbps-install unbound
# Then, we enable the service at boot.
$ sudo ln -s /etc/sv/unbound /var/service
That’s all you have to do. You have Unbound up and running. Next step
is to modify your /etc/resolv.conf, which is the file that contains
your resolving DNS server. Modify the file using nano or any text
editor that you use and add the line “nameserver 127.0.0.1” on top of
everything. My file looks like this
nameserver 127.0.0.1
nameserver 192.168.1.1
Everytime you reconnect to a connection the file with get
rewritten. In Linux to fix this you can either configure your DNS
servers in NetworkManager, wicd or whatever that you use to use
127.0.0.1 as main DNS server, or you can just simply make the file
unwritteable with chattr +i /etc/resolv.conf. In FreeBSD you only
have to enable local_unbound, and 127.0.0.1 will always be the
default DNS server, as long as local_unbound is running.
After adding that to /etc/resolv.conf, check that unbound is working
by pinging any website. If it resolves, congratulations! your unbound
installation is working.
Ad-blocking with domain blocklist.
You can specify to Unbound which domains not to resolve, we will use this to block all the domains used for ads. I will be using a script made by vermaden, originally for BSD systems only, but I have adapted it so it also works in Linux. If you’re in FreeBSD the only thing you have to do is to run the script. In Linux you have you have to do a couple more steps.
$ wget https://files.suragu.net/unbound-blacklist-fetch.sh
$ sudo mkdir -p /var/unbound/conf.d/
$ chmod +x unbound-blacklist-fetch.sh
$ sudo ./unbound-blacklist-fetch.sh
$ cat /var/unbound/conf.d/blacklist.conf # Check if everything worked
$ echo "include: /var/unbound/conf.d/blacklist.conf" | sudo tee -a /etc/unbound/unbound.conf
$ sudo sv down unbound; sudo sv up unbound # systemctl in other distros.
Then try resolving a known ad domain, such as doubleclick.com:
$ ping doubleclick.com
ping: doubleclick.com: Name or service not known
And that’s it! You’re blocking annoying domains at DNS level. You can
modify the /var/unbound/conf.d/blacklist.conf file to add (or
remove) domains.