Building a Self-Hosted Recursive DNS Resolver with Unbound on Linux

Approach Comparison: Public Forwarders vs. Recursive Resolvers

Every network connection begins with a DNS query. In standard home, lab, or cloud environments, systems resolve hostnames through one of three distinct architectures. The design you pick controls who monitors your query history, how quickly cached records return, and whether you can cryptographically verify response records.

1. Upstream Public DNS (Google 8.8.8.8, Cloudflare 1.1.1.1, Quad9)

Your systems send every outbound query straight to external recursive servers. Setup requires nothing more than editing two IP addresses. However, this convenience hands a complete log of your internal domain lookups to third-party providers. When an upstream provider suffers a BGP hijack, outage, or throttling event, your outbound traffic stops resolving instantly.

2. Forwarding Proxy (Dnsmasq, Basic BIND Forward-Only)

A lightweight local proxy caches answers on your LAN while forwarding misses to public resolvers. Repeat queries on the local network resolve rapidly. Even so, cache misses still leak metadata to upstream servers, and your infrastructure continues to rely on third-party cryptographic validation.

3. True Iterative Recursive Resolver (Unbound Querying Root Servers)

Your server queries the 13 root server clusters (a.root-servers.net through m.root-servers.net) directly. From there, it follows referrals to the relevant Top-Level Domain (TLD) servers, and finally reaches the authoritative nameservers for the target domain. No third party collects your aggregated query stream. DNSSEC validation happens right on your host, and hot records stay cached in local RAM.

Pros and Cons: The Real Trade-Offs

Operating a dedicated resolver requires ongoing care. Consider these trade-offs before deploying Unbound across your network:

The Advantages

  • Query Privacy: No single external organization tracks your entire lookup history. Root operators see only TLD requests like .com or .org. Authoritative nameservers see only queries for their specific domains.
  • Sub-Millisecond Cache Latency: Once Unbound caches an A or AAAA record, local machines retrieve it in 0.2–0.8ms over a standard gigabit LAN, compared to 15–45ms across the internet.
  • Local DNSSEC Validation: Signatures undergo cryptographic verification against root trust anchors on your machine. If an attacker injects a spoofed response along the path, Unbound discards it immediately.
  • Custom Local Overrides: Unbound handles split-horizon overrides cleanly without requiring a full authoritative server suite like BIND9 or PowerDNS.

The Disadvantages

  • Cold-Cache Latency: Resolving an unvisited domain requires multiple iterative round trips (Root → TLD → Authoritative). That initial cold query can take 80–220ms, whereas massive public resolvers frequently hold those records warm in their memory pools.
  • Operational Overhead: You must track root trust anchors, keep memory usage within bounds, update root hints, and prevent unauthorized clients from abusing your server for amplification attacks.

Recommended Setup: Hardware, Architecture, and Security Baselines

For an environment supporting 50 to 250 active client machines, a minimal VM or LXC container running Debian 12 or Ubuntu 24.04 LTS handles the load with capacity to spare. Allocate 2 vCPUs, 1GB of RAM, 10GB of disk space, and a static IP on your management VLAN. Under typical office loads of 20–50 queries per second, Unbound generally draws less than 120MB of resident memory.

Key architecture guidelines:

  • Never expose port 53 to the public internet: Bind Unbound strictly to your internal network interfaces or loopback. Internet-facing open resolvers get pulled into UDP reflection attacks within hours.
  • Reconfigure systemd-resolved: Ubuntu defaults to running systemd-resolved on 127.0.0.53:53. You must disable its stub listener before Unbound can claim port 53.
  • Enable aggressive negative caching: Instruct Unbound to cache NXDOMAIN (non-existent domain) responses according to RFC 8198. This suppresses repetitive queries caused by typos, telemetry tools, and misconfigured background services.

Implementation Guide: Deploying and Hardening Unbound Step-by-Step

Step 1: Install Unbound and Fetch Root Hints

Install Unbound, the root trust anchor utility, and standard diagnostic tools via APT:

sudo apt update && sudo apt install -y unbound unbound-anchor dnsutils curl

# Download the official root hints list from InterNIC
sudo curl -o /var/lib/unbound/root.hints https://www.internic.net/domain/named.root

# Bootstrap the official DNSSEC root key anchor
sudo -u unbound unbound-anchor -a /var/lib/unbound/root.key || true

Step 2: Free Up Port 53 from systemd-resolved

Disable the local stub listener in systemd-resolved so Unbound can bind to port 53 across all network interfaces:

# Create an override file to disable the stub listener
sudo mkdir -p /etc/systemd/resolved.conf.d
cat <<EOF | sudo tee /etc/systemd/resolved.conf.d/disable-stub.conf
[Resolve]
DNSStubListener=no
EOF

# Point host resolution directly to the upstream interface file
sudo ln -sf /run/systemd/resolve/resolv.conf /etc/resolv.conf

# Restart systemd-resolved to apply the change
sudo systemctl restart systemd-resolved

Step 3: Construct a Hardened unbound.conf

Deploy a hardened baseline configuration under /etc/unbound/unbound.conf.d/server.conf. Adjust the access control subnets to match your local network ranges:

cat <<EOF | sudo tee /etc/unbound/unbound.conf.d/server.conf
server:
    # Network and Interface Bindings
    interface: 0.0.0.0
    interface: ::0
    port: 53
    do-ip4: yes
    do-ip6: yes
    do-udp: yes
    do-tcp: yes

    # Access Control: Allow trusted LAN subnets, drop all foreign queries
    access-control: 127.0.0.0/8 allow
    access-control: ::1 allow
    access-control: 192.168.1.0/24 allow
    access-control: 10.10.0.0/16 allow
    access-control: 0.0.0.0/0 refuse
    access-control: ::0/0 refuse

    # Trust Anchors & Root Hints for Recursive Iteration
    root-hints: "/var/lib/unbound/root.hints"
    auto-trust-anchor-file: "/var/lib/unbound/root.key"

    # Security and Information Leak Prevention
    hide-identity: yes
    hide-version: yes
    harden-glue: yes
    harden-dnssec-stripped: yes
    harden-below-nxdomain: yes
    qname-minimisation: yes
    use-caps-for-id: no

    # Caching and Concurrency Tuning (Sized for ~1GB RAM)
    num-threads: 2
    msg-cache-slabs: 4
    rrset-cache-slabs: 4
    infra-cache-slabs: 4
    key-cache-slabs: 4
    rrset-cache-size: 128m
    msg-cache-size: 64m
    so-rcvbuf: 4m
    so-sndbuf: 4m
    prefetch: yes
    prefetch-key: yes
    serve-expired: yes
    serve-expired-ttl: 86400

    # Logging Baseline
    verbosity: 1
    log-queries: no
EOF

Step 4: Validate Syntax and Start the Service

Verify your syntax before reloading the daemon:

# Validate configuration syntax
sudo unbound-checkconf

# Enable and start the Unbound service
sudo systemctl enable unbound
sudo systemctl restart unbound
sudo systemctl status unbound --no-pager

Step 5: Verify Iterative Resolution and DNSSEC Validation

Inspect how your server resolves an authentic domain using dig:

# Perform a lookup against localhost
dig @127.0.0.1 cloudflare.com +dnssec

Check the flags: line in the response header. You should see qr, rd, ra, and crucially ad (Authentic Data). The ad flag confirms Unbound validated the signature chain up to the root zone.

Next, confirm that validation properly rejects corrupted zones by querying a known invalid test domain:

# Should return SERVFAIL due to an invalid cryptographic signature
dig @127.0.0.1 dnssec-failed.org

Unbound should return status: SERVFAIL without returning records in the ANSWER section. This proves the resolver protects clients against spoofed data.

Step 6: Automate Maintenance for Root Hints

Root hints change rarely, but keeping them synchronized prevents subtle resolution issues over time. Set up a lightweight monthly cron job to refresh the file:

cat << 'EOF' | sudo tee /etc/cron.monthly/update-unbound-hints
#!/bin/sh
set -e
curl -s -o /var/lib/unbound/root.hints.new https://www.internic.net/domain/named.root
if [ -s /var/lib/unbound/root.hints.new ]; then
    mv /var/lib/unbound/root.hints.new /var/lib/unbound/root.hints
    systemctl reload unbound
fi
EOF

sudo chmod +x /etc/cron.monthly/update-unbound-hints

Once verified, distribute the resolver IP through your DHCP options or assign it statically on your servers. Your network now resolves domains directly against root infrastructure, keeping your traffic data internal and cryptographically validated.

Share: