Sub topic 12
Using BIND as a Recursive Resolver or Forwarder
This chapter assumes you can read a named.conf file, know the difference between authoritative and recursive servers, and have BIND installed from the earlier chapters. You should also be comfortable with dig and with restarting services under systemd.
When BIND is the resolver
Earlier chapters used Unbound as the caching resolver and BIND as the authoritative server. BIND can do both jobs, and a single named process can be a resolver for your clients. Reasons to choose it:
- You already run BIND and want one daemon, one configuration language and one set of tools (
rndc,named-checkconf). - You need views, response policy zones or other BIND-only features on the resolver.
- Your site policy standardises on BIND.
A server that is both authoritative for public zones and recursive for clients is a risk. Recursion answers queries for any name, and an exposed recursive server can be used in amplification attacks. If a host serves zones to the internet, keep recursion off there and run the resolver on a different host or in a different view. The split-horizon chapter shows the view approach.
Recursion versus forwarding
BIND can answer a client query in two ways.
- Full recursion. BIND starts at the root servers, follows referrals to the top-level domain servers and then to the domain's servers, and caches what it learns. This is the default when
recursion yes;is set and no forwarders are configured. - Forwarding. BIND sends the query to another resolver, such as an ISP or corporate resolver, and caches the answer it gets back. This is useful when outbound traffic to the internet is restricted, or when an upstream resolver already holds a large shared cache.
A recursive configuration
The following named.conf fragment turns BIND into a caching resolver for the local network. It reuses the trusted ACL defined in the authoritative chapter.
acl "trusted" {
127.0.0.1;
::1;
192.0.2.0/24;
};
options {
directory "/var/cache/bind";
listen-on { 127.0.0.1; 192.0.2.10; };
listen-on-v6 { ::1; };
recursion yes;
allow-recursion { trusted; };
allow-query { trusted; };
allow-query-cache { trusted; };
dnssec-validation auto;
max-cache-size 256m;
max-cache-ttl 86400;
};What each setting does:
| Option | Purpose |
|---|---|
| `recursion yes;` | Lets BIND resolve names it is not authoritative for. |
| `allow-recursion` | Restricts which clients may ask for recursion. Others get `REFUSED`. |
| `allow-query` | Restricts who may send any query at all. |
| `allow-query-cache` | Restricts who may read cached data. A cache that others can read leaks information about what your clients look up. |
| `dnssec-validation auto;` | Validates answers using the built-in root trust anchor. |
| `max-cache-size` | Caps memory used by the cache. The default is a share of system memory. |
| `max-cache-ttl` | Caps how long any record stays cached, in seconds. |
If you omit allow-recursion, BIND defaults to allowing localnets and localhost in most packaged builds. Do not rely on that. State the intent explicitly.
Forwarding to an upstream resolver
Add a forwarders statement to options to send all non-local queries upstream.
options {
directory "/var/cache/bind";
listen-on { 127.0.0.1; 192.0.2.10; };
recursion yes;
allow-recursion { trusted; };
forwarders { 192.0.2.53; 192.0.2.54; };
forward only;
dnssec-validation auto;
};The forward setting controls what happens when the forwarders do not answer:
forward only;returnsSERVFAILif no forwarder answers. Use this when the server must never contact the internet directly.forward first;tries the forwarders and then falls back to full recursion. This is the default when forwarders are listed, and it is more resilient.
You can also forward a single domain, which mirrors the Unbound forward-zone from earlier chapters:
zone "corp.example" {
type forward;
forward only;
forwarders { 192.0.2.53; };
};A zone of type forward applies only to names under corp.example. Everything else follows the global options. If the internal domain is signed with DNSSEC, or is unsigned and unreachable from the public tree, check how validation treats it, as described below.
DNSSEC and forwarding
With dnssec-validation auto;, BIND validates answers itself, even when it forwards. A forwarder that strips DNSSEC records, or an upstream that does not support them, causes SERVFAIL for signed names. Check this with:
dig @127.0.0.1 example.org A +dnssec
dig @127.0.0.1 dnssec-failed.org AThe second name is deliberately broken. A validating resolver should answer SERVFAIL. A resolver that returns an address is not validating.
Internal zones that do not exist in the public tree need a negative trust anchor or a local authoritative copy, otherwise validation treats them as bogus. Declaring them as type forward does not turn validation off for them. If you hit this, add:
options {
validate-except { "corp.example"; };
};validate-except tells BIND not to require DNSSEC proof for names under that domain.
Applying and checking the configuration
Always check before reloading. A syntax error that reaches named can stop it from starting.
sudo named-checkconf
sudo rndc reconfig
dig @192.0.2.10 www.example.org A
dig @192.0.2.10 www.example.org ARun the same query twice. The Query time in the second answer should be 0 msec or close to it, because the answer now comes from the cache. The TTL in the answer section should also be lower on the second reply.
Test the access control from a host outside the trusted ranges:
dig @192.0.2.10 www.example.org AThe expected result is status: REFUSED. If you get an answer, recursion is open to that host.
Cache management
rndc can inspect and clear the cache without a restart.
sudo rndc flushname www.example.org
sudo rndc flushtree example.org
sudo rndc flush
sudo rndc dumpdb -cache
sudo rndc recursingflushnameremoves one name. Use it after you change a record upstream and cannot wait for the TTL.flushtreeremoves a name and everything below it.flushempties the whole cache. Expect a burst of upstream traffic afterwards.dumpdb -cachewrites the cache tonamed_dump.dbin the working directory.recursinglists the queries currently in progress, written tonamed.recursing.
Common errors
| Symptom | Likely cause | What to check | Column |
|---|---|---|---|
| `REFUSED` for your own clients | Client address not in `allow-recursion` or `allow-query-cache` | Compare the client IP with the ACL. Check for NAT. | |
| `SERVFAIL` for every signed domain | Forwarder strips DNSSEC data, or the clock is wrong | Query the forwarder directly with `+dnssec`. Check `timedatectl`. | |
| `SERVFAIL` only for an internal domain | Validation of an unsigned private zone | Add `validate-except` or a forward zone with a trust anchor setup. | |
| Works for 127.0.0.1, not for the LAN | `listen-on` lacks the LAN address | Run `ss -lntup | grep ':53 '` and compare. |
| Slow first lookup, fast second | Normal cache behaviour | Not an error. | |
| Timeouts to the internet | Firewall blocks outbound UDP and TCP port 53 | Allow both protocols outbound, or use forwarders. |
Exercises
Exercise
A BIND resolver at 192.0.2.10 returns correct answers to hosts on 192.0.2.0/24 but returns REFUSED to a host at 198.51.100.7. Which two options in named.conf control this, and what should you do if the host should be allowed?
Show answer
allow-recursion and allow-query-cache (and allow-query, which gates all queries). Add the 198.51.100.0/24 range, or the single address, to the trusted ACL, run named-checkconf, then rndc reconfig. Test again from that host with dig.
Exercise
You configure forwarders { 192.0.2.53; }; with forward only;, and the forwarder goes offline. What does a client see for a name that is not cached, and how does forward first; change that?
Show answer
With forward only; BIND returns SERVFAIL because it never contacts the internet directly. With forward first; BIND tries the forwarder, then falls back to full recursion from the root servers, so the client still gets an answer if outbound traffic is allowed.