Recursive DNS Resolver

Simple steps to establish an encrypted, recursive, caching DNS resolver using:

  • Unbound
  • DNSdist
  • DNSCrypt

Unbound

Install with:

sudo apt update && sudo apt install unbound

The relevant files will be in:

  • /etc/unbound/unbound.conf for config
  • /var/lib/unbound for root.hints and root.key
  • /opt/encrypted-dns/encrypted-dns.toml for DNSCrypt server
  • /etc/dnsdist/dnsdist.conf for DNSdist config

Make sure log exists with proper permissions:

sudo touch /etc/unbound/unbound.log
sudo chown unbound:unbound /etc/unbound/unbound.log

Add a local apparmor override:

sudo nano /etc/apparmor.d/local/usr.sbin.unbound`
/etc/unbound/unbound.log rw,

Reload apparmor:

sudo apparmor_parser -r /etc/apparmor.d/usr.sbin.unbound

The entire /etc/unbound/unbound.conf looks like this:

server:
    interface: ::
    interface: 0.0.0.0
    access-control: ::/0 allow
    access-control: 0.0.0.0/0 allow
    port: 53
    do-ip4: yes
    do-ip6: yes
    do-udp: yes
    do-tcp: yes
    verbosity: 1
    harden-glue: yes
    harden-dnssec-stripped: yes
    use-caps-for-id: no
    edns-buffer-size: 1472
    prefetch: yes
    hide-identity: yes
    hide-version: yes
    cache-min-ttl: 360
    cache-max-ttl: 86400
    so-rcvbuf: 1m
    unwanted-reply-threshold: 10000
    logfile: "/etc/unbound/unbound.log"
    root-hints: "/var/lib/unbound/root.hints"
    auto-trust-anchor-file: "/var/lib/unbound/root.key"

ratelimit: 1000
ip-ratelimit: 1000

Restart Unbound:

sudo systemctl restart unbound

Troubleshoot Unbound with:

sudo unbound-control status

Flush a record from cache:

sudo unbound-control flush example.com

Root Hints

Download root hints file from Internic:

sudo wget https://www.internic.net/domain/named.root -qO- | sudo tee /var/lib/unbound/root.hints

Include root server key for DNSSEC into /var/lib/unbound/root.key:

. IN DS 19036 8 2 49AAC11D7B6F6446702E54A1607371607A1A41855200FD2CE1CDDE32F24E8FB5

And verify it here.

Verify that root.key is owned by unbound.

Resolved

Edit resolved systemd config at /etc/systemd/resolved.conf and set:

DNSStubListener=no

Otherwise Unbound won't be able to listen on port 53. Now open 53 in firewall:

sudo ufw allow 53

DNSCrypt

wget the latest release into /opt and then untar it and cd into /opt/encrypted-dns and create/edit config with:

sudo cp example-encrypted-dns.toml encrypted-dns.toml && sudo nano encrypted-dns.toml

Update to:

listen_addrs = [
    { local = "0.0.0.0:443",    external = "149.56.14.48:443" },
    { local = "[::]:443",    external = "[2607:5300:201:3100::40eb]:443" }
]

Make sure external = matches your public IP.

Update upstream_addr to 127.0.0.1:53 and set provider_name = zcyph.cc.

Enable dnssec = true and no_filters = true and no_logs = true and anonymised_dns enabled = true.

Turn it into a systemd unit for convenience:

sudo nano /etc/systemd/system.encrypted-dns.service

[Unit]
Description=DNSCrypt v2 server
ConditionFileIsExecutable=/opt/encrypted-dns/encrypted-dns
After=syslog.target network-online.target

[Service]
StartLimitInterval=5
StartLimitBurst=10
ExecStart=/opt/encrypted-dns/encrypted-dns -c /opt/encrypted-dns/encrypted-dns.toml
WorkingDirectory=/opt/encrypted-dns/
Restart=always
RestartSec=10

[Install]
WantedBy=multi-user.target

Reload and enable:

sudo systemctl enable --now encrypted-dns
sudo systemctl daemon-reload

DNSDist

Install with sudo apt update && sudo apt install dnsdist. Edit its config:

sudo nano /etc/dnsdist/dnsdist.conf

Change to avoid port 53 error:

setLocal("127.0.0.1:5353")

Restart and check:

sudo systemctl restart dnsdist and sudo systemctl status dnsdist

Certbot

Install: sudo apt install certbot

The next part depends on whether there's a webserver. The standalone plugin is:

sudo certbot certonly --standalone --preferred-challenges http --agree-tos --email rblpt6pc@mailer.me -d dns1.zcyph.cc

DoH

Enable DoH in sudo nano /etc/dnsdist/dnsdist.conf and test it with echo | openssl s_client -connect dns2.zcyph.cc:443 | grep subject.