Sub topic 7
Configuring Unbound as a caching resolver
This chapter assumes you can use a Linux shell and edit files as root. It also assumes you know basic TCP/IP (addresses, ports, UDP versus TCP) and can manage services with systemd. You should already have Unbound installed and know how to use dig. Earlier chapters cover both.
What a caching resolver does
A recursive resolver answers questions for clients such as laptops, servers and containers. When a client asks for www.example.org, the resolver starts at the root servers and follows referrals down to the authoritative server. It sends the final answer back to the client. It also keeps that answer in a cache until the record's TTL runs out.
Unbound is built for this one job. It does not serve zones as an authoritative server. Its default behavior is already close to what you want: it validates DNSSEC when a trust anchor is present, and it caches. Most of your configuration work is about three things:
- Which interfaces it listens on.
- Which clients may use it.
- A few privacy and performance options.
The configuration file layout
Unbound reads /etc/unbound/unbound.conf. On Debian and Ubuntu, that file normally contains an include: line that pulls in every file under /etc/unbound/unbound.conf.d/. On the RHEL family, the main file holds the settings and also includes /etc/unbound/conf.d/*.conf.
Put your changes in a separate file so package upgrades do not overwrite them. On Debian or Ubuntu, create /etc/unbound/unbound.conf.d/resolver.conf. Every option goes inside a server: clause, and the file is indented with spaces.
A minimal resolver configuration
This configuration listens on loopback and one LAN address. It answers only local networks and refuses everyone else.
server:
interface: 127.0.0.1
interface: 192.0.2.10
access-control: 127.0.0.0/8 allow
access-control: 192.0.2.0/24 allow
access-control: 0.0.0.0/0 refuse
hide-identity: yes
hide-version: yes
qname-minimisation: yes
prefetch: yesEach line does a specific job.
| Option | Effect |
|---|---|
| `interface` | An address Unbound binds to. Repeat the line for more addresses. If you omit it, Unbound listens only on localhost. |
| `access-control` | A network and an action. Unbound picks the most specific matching netblock for each client. |
| `hide-identity`, `hide-version` | Stop Unbound answering `id.server` and `version.bind` queries, which reveal the host name and software version. |
| `qname-minimisation` | Sends upstream servers only as many labels as they need. The root servers see `org.`, not `www.example.org.`. |
| `prefetch` | Refreshes popular records shortly before they expire, so busy names rarely cause a cache miss. |
The access-control action matters. allow answers recursive queries from that network. refuse replies with a REFUSED error. deny drops the query without any reply. Use refuse for a clear signal during testing. Use deny if you want to avoid sending replies to spoofed sources.
Why the default is closed
If you leave out access-control, Unbound refuses all clients except localhost. That default prevents an open resolver, which attackers abuse in DNS amplification attacks. Every network you add with allow is a decision to trust it.
Checking and starting the service
Always check the configuration before you restart. A typo can stop the service, and a stopped resolver breaks name lookups for every client at once.
sudo unbound-checkconf
sudo systemctl enable --now unbound
sudo systemctl status unbound
sudo ss -lntup | grep ':53 'unbound-checkconf prints no errors in /etc/unbound/unbound.conf when the file is valid. The ss output should show unbound bound to port 53 on both of your addresses, over UDP and TCP. If another process holds port 53, usually systemd-resolved on 127.0.0.53, Unbound cannot bind. Either stop that stub listener or bind Unbound only to addresses it does not use.
For later changes, run unbound-checkconf again and then sudo systemctl reload unbound. A reload re-reads the configuration without dropping the cache contents in most cases. Some options, such as interface, take effect fully only after a restart.
Testing the resolver
Test from a client on the allowed network and from a machine outside it. Use the server address explicitly with @.
dig @192.0.2.10 www.example.org A
dig @192.0.2.10 www.example.org A
dig @192.0.2.10 example.org A +dnssecRun the first query twice. Compare the Query time line. The first lookup walks the hierarchy and typically takes tens of milliseconds. The second comes from the cache and usually reports 0 msec. The TTL in the answer section also counts down between runs, which confirms the answer is cached.
In the DNSSEC query, look at the flags: line in the header. The ad flag (Authenticated Data) means Unbound validated the signatures. The ad flag appears only for names in signed zones.
From a client outside the allowed networks, you should see status: REFUSED. If you instead get a timeout, a firewall is dropping the traffic or Unbound is not listening on that address.
Forwarding queries upstream
By default Unbound recurses from the root itself. Sometimes you must send queries to another resolver instead, for example when the network blocks outbound port 53 except to an ISP resolver. Use a forward-zone. The name "." matches everything.
forward-zone:
name: "."
forward-addr: 192.0.2.53A forwarder is a trade-off. Your resolver depends on the upstream server and trusts its answers. Unbound still validates DNSSEC itself, but the upstream must pass DNSSEC records through unchanged. Prefer full recursion when your network allows it.
Tuning the cache
The defaults suit a small network. Two settings are worth knowing when you scale up.
server:
num-threads: 2
msg-cache-size: 64m
rrset-cache-size: 128m
cache-min-ttl: 60
cache-max-ttl: 86400msg-cache-sizeholds complete responses.rrset-cache-sizeholds the individual record sets and should be about twice as large.- Keep
num-threadsat or below the number of CPU cores. Each thread has its own slice of the cache. cache-min-ttlforces short TTLs upward. Keep it low. Values over a few minutes make clients see stale addresses after a legitimate change.cache-max-ttlcaps very long TTLs so old data does not stay in memory for days.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `unbound-checkconf` reports an unknown keyword | The option is outside `server:`, or the keyword is misspelled | Move it under the right clause and check spelling |
| Service fails with "address already in use" | `systemd-resolved` or another daemon owns port 53 | Free the port or bind Unbound to a different address |
| Clients get `REFUSED` | Client network missing from `access-control` | Add the network with `allow` and reload |
| Clients time out | Firewall blocks UDP or TCP 53, or the wrong `interface` | Open both protocols and check `ss` |
| `SERVFAIL` for one domain only | DNSSEC validation failed for that domain | Query with `+cd` to confirm, then investigate the domain's signatures |
The +cd flag (checking disabled) asks Unbound to skip validation for one query. If dig @192.0.2.10 broken.example +cd succeeds while the plain query returns SERVFAIL, the problem is the domain's DNSSEC setup and not your resolver. Do not turn validation off globally to fix one broken domain.
Exercise
A client on 198.51.100.7 gets status: REFUSED from your Unbound server at 192.0.2.10. Your configuration allows only 127.0.0.0/8 and 192.0.2.0/24. What do you change, and how do you apply it safely?
Show answer
Add access-control: 198.51.100.0/24 allow (or a narrower netblock) to the server: clause, because REFUSED means the client's network matched the 0.0.0.0/0 refuse rule. Then run sudo unbound-checkconf to validate the file and sudo systemctl reload unbound to apply it. Confirm with dig @192.0.2.10 www.example.org from the client.
Exercise
You run dig @192.0.2.10 www.example.org A twice. The first reply shows Query time: 48 msec and the second shows Query time: 0 msec. What does this show, and which Unbound option helps keep this fast for popular names?
Show answer
The second answer came from Unbound's cache, so no upstream servers were queried. The prefetch: yes option refreshes popular records shortly before their TTL expires, so frequently used names rarely cause a cache miss.