Sub topic 6
Testing with dig and the configuration checkers
This chapter assumes you can use a Linux shell and systemd, know what UDP and TCP on port 53 mean, and have read the earlier chapters on resolution, record types and server roles. It covers two kinds of testing. Checkers validate files before a server reads them. dig queries a running server and shows exactly what it returned.
Why test in two stages
A DNS server that fails to reload can keep serving old data, or it can stop answering. Your users then see the failure as "the internet is down". Checking the files first avoids most of these outages. Querying afterwards confirms that the server does what you intended.
The working order is:
- Edit the configuration or zone file.
- Run the matching checker.
- Reload the service.
- Query the server with
digand read the flags, status and answer.
The configuration checkers
Each program has a checker that parses its files without starting the server.
| Tool | Checks | Exit status |
|---|---|---|
| `named-checkconf` | `named.conf` and the files it includes | 0 if valid, non-zero on error |
| `named-checkzone` | One zone file, loaded as a given zone name | 0 if loaded, non-zero on error |
| `unbound-checkconf` | `unbound.conf` and its includes | 0 if valid, non-zero on error |
Run them as shown below. The paths match Debian and Ubuntu. On the RHEL family, BIND files live under /etc/named.conf and /var/named/.
sudo named-checkconf
sudo named-checkzone example.com /etc/bind/db.example.com
sudo unbound-checkconfOn success, named-checkconf prints nothing. named-checkzone prints the loaded serial and OK. unbound-checkconf prints no errors in /etc/unbound/unbound.conf.
Because the exit status is reliable, you can chain the checker and the reload so a broken file never gets loaded:
sudo named-checkconf && sudo named-checkzone example.com /etc/bind/db.example.com && sudo rndc reload example.comWhat the checkers do not catch
A passing check means the syntax is valid. It does not mean the data is right. These mistakes pass the checkers:
- You forgot to increase the SOA serial. The primary reloads the file, but secondaries compare serials and keep the old data.
- A record points to the wrong IP address.
- A name server host has no matching A record.
allow-queryoraccess-controlexcludes the clients that need service.
named-checkzone does catch some semantic faults: a CNAME that shares a name with other data, an NS record that points at a CNAME, and an NS name inside the zone that has no address record. It also reports a missing SOA or NS record at the apex. Treat its warnings as errors.
A common beginner mistake is a missing trailing dot. In a zone file, ns1.example.com without the final dot is read as relative and becomes ns1.example.com.example.com.. The checker may accept this, but the records will be wrong.
Using dig
dig sends one query and prints the full response. Its general form is:
dig [@server] name [type] [options]If you omit @server, dig uses the resolvers listed in /etc/resolv.conf. On many systems that is the local systemd-resolved stub at 127.0.0.53, not the server you are testing. Always name the server with @ when you test a specific one.
The set of queries from the chapter's reference examples is:
dig @127.0.0.1 www.example.com A +norecurse
dig @127.0.0.1 example.com SOA +short
dig @127.0.0.1 example.org A +dnssecReading the output
Run a query and look at the header lines:
dig @127.0.0.1 www.example.com AA typical response begins like this:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 41873
;; flags: qr aa rd; QUERY: 1, ANSWER: 1, AUTHORITY: 2, ADDITIONAL: 3Three parts of this header matter most.
Status. This is the response code.
| Status | Meaning | Usual cause |
|---|---|---|
| `NOERROR` | The query succeeded. With zero answers it means the name exists but has no record of that type. | Normal, or you asked for the wrong type |
| `NXDOMAIN` | The name does not exist. | Typo, record missing, or zone not loaded |
| `SERVFAIL` | The server could not produce an answer. | Broken zone, unreachable upstream, DNSSEC validation failure |
| `REFUSED` | The server declined to answer. | Access control denied your address, or the server is not authoritative and recursion is off |
Flags. Each flag is a short code.
| Flag | Meaning |
|---|---|
| `qr` | This is a response. |
| `aa` | Authoritative answer. The server owns the zone. |
| `rd` | Recursion desired. The client asked for it. |
| `ra` | Recursion available. The server offers it to you. |
| `ad` | Authenticated data. A validating resolver verified the DNSSEC chain. |
| `tc` | Truncated. The answer did not fit in UDP, so retry over TCP. |
Sections. The ANSWER section holds the records you asked for. AUTHORITY lists name servers or the SOA for negative answers. ADDITIONAL holds helpful extra records such as glue addresses.
Useful options
| Option | Effect |
|---|---|
| `+short` | Prints only the record data. |
| `+norecurse` | Clears the `rd` flag, so the server answers only from its own data or cache. |
| `+tcp` | Forces TCP. Use it to test that TCP port 53 is open. |
| `+dnssec` | Requests DNSSEC records and lets you see the `ad` flag. |
| `+trace` | Walks from the root servers down, without using your resolver's cache. |
| `+noall +answer` | Prints only the answer section. |
| `-x ADDRESS` | Does a reverse lookup. |
Testing each server role
Match the query to the role the server plays.
An authoritative server should answer for its own zone with the aa flag set. It should refuse or ignore recursion for other names.
dig @192.0.2.10 www.example.com A +norecurse
dig @192.0.2.10 example.org AThe first query should return aa and the address. The second should return REFUSED or an answer without the ra flag, because an authoritative-only server does not resolve other zones.
A recursive resolver should return answers for other domains, set ra, and not set aa. Run the same query twice and compare the query time. The second answer comes from the cache and is usually near 0 ms. The TTL in the answer also counts down between runs.
dig @127.0.0.1 example.org A | grep -E 'status|flags|Query time'
dig @127.0.0.1 example.org A | grep -E 'status|flags|Query time'A secondary server should return the same SOA serial as the primary. Comparing serials is the quickest way to see whether replication works:
dig @192.0.2.10 example.com SOA +short
dig @192.0.2.11 example.com SOA +shortIf the numbers differ, the secondary has not received the latest transfer.
Testing access control
Test from a client that should be allowed and from one that should not. From a host outside the allowed networks, a query to a resolver restricted by access-control: 0.0.0.0/0 refuse returns status: REFUSED. If it returns an answer, your restriction is not working, and the server may be an open resolver.
You can also pick the source address on the same host, if it has more than one:
dig @192.0.2.10 -b 192.0.2.50 example.org ATesting TCP and truncation
DNS uses UDP first and falls back to TCP for large answers. A firewall that blocks TCP port 53 causes failures that only show up on big responses, such as DNSSEC-signed ones. Check it directly:
dig @192.0.2.10 example.com DNSKEY +tcpIf this times out while UDP queries work, open TCP port 53.
Common errors when testing
connection timed out; no servers could be reached. The server is not running, is not listening on that address, or a firewall drops the packets. Check withss -lntup | grep ':53 '.SERVFAILon a signed domain only. Trydig +cd(checking disabled). If the answer then succeeds, DNSSEC validation is failing. Common causes are a wrong system clock or a broken chain of trust.- Old data after an edit. Either the serial did not increase, the reload did not run, or you are reading a cached answer. Query with
+norecurseagainst the authoritative server to bypass caches. - No
aaflag on your own zone. The zone did not load. Look at the logs, for examplejournalctl -u namedorjournalctl -u unbound.
A complete verification script
This script combines the checkers and the queries. It stops on the first error and tests that the zone serial matches after a reload.
#!/usr/bin/env bash
set -euo pipefail
ZONE=example.com
FILE=/etc/bind/db.example.com
SERVER=127.0.0.1
named-checkconf
named-checkzone "$ZONE" "$FILE"
rndc reload "$ZONE"
sleep 1
flags=$(dig @"$SERVER" "$ZONE" SOA +norecurse | grep -m1 'flags:')
case "$flags" in
*" aa"*) echo "authoritative: ok" ;;
*) echo "zone not authoritative: $flags" >&2; exit 1 ;;
esac
dig @"$SERVER" "$ZONE" SOA +shortRun it with sudo, because rndc needs access to the control key.
Exercise
You edited a zone file and added a record. named-checkzone printed OK and you ran rndc reload. A secondary at 192.0.2.11 still does not return the new record. Which dig commands do you run, and what is the most likely cause?
Show answer
Run dig @192.0.2.10 example.com SOA +short and dig @192.0.2.11 example.com SOA +short and compare the serial numbers. The most likely cause is that you did not increase the SOA serial, so the secondary sees no change and keeps its old copy. The checker cannot detect this, because an unchanged serial is valid syntax. Increase the serial, reload, and compare again.
Exercise
A query to your resolver for a signed domain returns SERVFAIL. How do you tell whether DNSSEC validation is the cause?
Show answer
Repeat the query with +cd, which disables DNSSEC checking: dig @127.0.0.1 example.org A +cd. If it now returns NOERROR with an answer, validation is failing. Then check the system clock and the trust anchor configuration. If +cd also returns SERVFAIL, the problem is upstream reachability or the remote zone itself.