Blocking ads with Unbound and DoT

Reading time: 7m

Want to block ads network-wide and encrypt DNS traffic with DNS-Over-TLS (DoT) ?

In this post, I will go over the configuration of Unbound (DNS server) with DoT (DNS over TLS)

For this you will need Unbound 1.9.6 linked with libssl and libcrypto.  For this I chose Gentoo Linux:

* net-dns/unbound
Latest version available: 1.9.6
Latest version installed: 1.9.6
Size of files: 5,548 KiB
Description: A validating, recursive and caching DNS resolver
License: BSD GPL-2

So you’d install unbound by executing:

emerge -qv net-dns/unbound

After unbound is installed, you’ll want to edit the configuration so it looks like this:

    tls-cert-bundle: /etc/unbound/ca-certificates.crt
    do-not-query-localhost: no
    verbosity: 2
    logfile: /etc/unbound/unbound.log
    port: 53
    outgoing-interface: LAN.IP
    access-control: LAN.SUBNET/BITS allow
    access-control: allow
    access-control: refuse
    access-control: ::0/0 refuse
    ip-ratelimit: 2000
    hide-identity: yes
    hide-version: yes
    harden-large-queries: yes
    harden-short-bufsize: yes
    harden-glue: yes
    use-syslog: no
    root-hints: "/etc/unbound/root.hints"
    do-tcp: yes
    do-udp: yes

    name: "."
    forward-tls-upstream: yes

Be sure to adjust the ACLs to allow your IP ranges to query the server, everything else should be denied

In order to get the root.hints file, you’ll need to download the latest from InterNIC:

 wget -O /etc/unbound/root.hints

To get the CA certificates file you’ll need to validate you’ll need the ca-certificates package:

emerge -qv app-misc/ca-certificates
cp /etc/ssl/certs/ca-certificates.crt /etc/unbound/ca-certificates.crt

To activate logging you’ll need to give your unbound user access to write to the logfile:

touch /var/log/unbound.log
chown unbound:unbound /var/log/unbound.log
chmod 700 /var/log/unbound.log

Touch the blocklist file:

mkdir /var/unbound
touch /etc/unbound/ad-blocklist.conf
ln -s /etc/unbound/ad-blocklist.conf /var/unbound/ad-blocklist.conf
chown -h unbound:unbound /var/unbound/ad-blocklist.conf
chown unbound:unbound /etc/unbound/ad-blocklist.conf /var/unbound

At this point you’ll need a script called unbound-adblock:

Simply clone on your server, cd to unbound-adblock, run, and it should populate /etc/unbound/ad-blocklist.conf

Check the configuration of Unbound:


It should return “unbound-checkconf: no errors in /etc/unbound/unbound.conf” if all is well.

Add it to runlevel default:

# rc-update add unbound default
* service unbound added to runlevel default

Start the service:

rc-service unbound start

Test that the server works for local and LAN queries:

# dig +short @localhost
$ dig @192.168.x.x +short

After that, I set the default DNS server for my network with DHCP, so every device that asks for an IP gets one.  Bear in mind this means you’ll need a statically configured IP on the server, or DHCP reservation on your DHCP server/router to have your DNS server on a specific IP address.

You could also enforce the usage of your DNS server using a dst-nat firewall rule which catches 53/udp packets and redirects them to your DNS server’s IP.  Since Unbound is doing port 853 (DoT) outbound, only clients issuing lookups on 53/udp will get redirected and protected via DoT.  Note that when you enforce redirects of 53/udp packets, even servers that aren’t configured to use your DNS will end up going through yours regardless of their configuration.


Leave a Reply

Your email address will not be published. Required fields are marked *