Sub topic 12
Querying and debugging with dig
This chapter assumes you know what a resolver is, what the main record types do, and how caching and TTLs work. You also need a terminal and dig. It is part of the dnsutils package on Debian and Ubuntu, bind-utils on RHEL and Fedora, and is installed by default on macOS.
dig sends one DNS query and prints the full reply. Unlike ping or a browser, it skips the hosts file and search domains. It does not hide errors. That makes it the standard tool for finding out what DNS is returning.
Basic queries
The general form is dig [@server] name [type]. If you leave out the type, dig asks for an A record.
dig example.com A
dig +short example.com AAAA
dig +noall +answer example.com MX+shortprints only the record data.+noall +answerturns off every output section, then turns the answer section back on. You keep the full record line, including the TTL.
Use +short in scripts. Use +noall +answer when you want to see the TTL.
Choosing a server with @
Without @server, dig uses the resolver from your system configuration (for example /etc/resolv.conf). With @, it asks that server directly.
dig @8.8.8.8 example.com A
dig +noall +answer example.com ACompare the two answers. They can differ in the addresses returned and in the TTL. The TTL in each answer line is the time left in that resolver's cache, so two resolvers rarely agree on it. When one resolver has the answer cached and another has just fetched it, the cached one shows a smaller number.
Reading the full output
Run dig example.com A and look at the layout. A typical reply has these parts:
- The header comment lines, which show the
digversion and the command line. ;; ->>HEADER<<-with the opcode, status, query ID, and flags.- The
OPT PSEUDOSECTION, which holds EDNS settings such as the advertised UDP size. - The
QUESTION SECTION, which echoes what you asked. - The
ANSWER SECTION, with the records that answer the question. - The
AUTHORITY SECTION, with NS records (or an SOA record for a negative answer). - The
ADDITIONAL SECTION, with helpful extras such as glue addresses. - Statistics: query time, the server that answered, the time, and the message size.
Status codes
The status: field in the header tells you whether the query worked.
| Status | Meaning | Common cause |
|---|---|---|
| `NOERROR` | The query was processed. | Success. An empty answer section means the name exists but has no record of that type. |
| `NXDOMAIN` | The name does not exist. | Typo, or a record that was never created or was deleted. |
| `SERVFAIL` | The server could not complete the query. | Broken delegation, unreachable authoritative servers, or a DNSSEC validation failure. |
| `REFUSED` | The server declined to answer. | Access controls, such as a server that only answers its own clients. |
NOERROR with zero answers is called NODATA. It is easy to mistake for success. A name can have an A record and no AAAA record. Asking for AAAA then returns NOERROR and an empty answer.
Flags
The flags: field lists single-purpose bits.
| Flag | Name | Meaning |
|---|---|---|
| `qr` | Query response | This message is a response. |
| `aa` | Authoritative answer | The server is authoritative for the zone and answered from its own data. |
| `tc` | Truncated | The reply did not fit in a UDP packet. Retry over TCP. |
| `rd` | Recursion desired | The client asked for recursion. `dig` sets this by default. |
| `ra` | Recursion available | The server offers recursion. |
| `ad` | Authentic data | The resolver validated the answer with DNSSEC. |
Two flags are most useful when debugging. If you query a recursive resolver, you will see ra and not aa, because the answer came from its cache or from another server. If you query an authoritative server, you will see aa. A reply without aa from a server that should be authoritative means the server is not configured for that zone, or you are asking the wrong server.
Reading the answer line
example.com. 3600 IN A 192.0.2.10The fields are the name, the remaining TTL in seconds, the class (IN for Internet), the type, and the record data. Query a resolver twice a few seconds apart. If the TTL drops each time, you are reading from a cache. If it resets to the same large number, either the cache was refilled or you are talking to an authoritative server.
Walking the delegation with +trace
+trace makes dig do the work of a recursive resolver itself.
dig +trace example.comIt starts with the root servers, then follows each referral to the TLD servers, then to the authoritative servers for the zone. At each step it prints the NS records it received and which server provided them.
This is a good way to find broken delegation. If the parent zone lists name servers that do not respond, or that answer without aa, you will see the trace stop or return an error at that step. If the TLD servers and your DNS provider disagree about the NS set, that is also visible here.
Note that +trace ignores your configured resolver and its cache. A successful trace and a failing application can both be true, because the application uses a cached or filtered answer from the recursive resolver.
Querying the authoritative server directly
A common debugging step is to find the zone's name servers and ask one of them.
dig +short NS example.com
dig @ns1.example.com example.com A +noall +answer +commentsThe first command lists the servers. The second asks one of them and shows the header, so you can check for aa. Since this server has the zone's actual data, the answer is not stale cache. If the authoritative answer is right and your resolver's answer is wrong, the problem is caching, and waiting for the TTL to expire will fix it. If the authoritative answer is wrong, fix the zone.
When a zone has several name servers, query each one and compare the SOA serial. A serial that differs between servers means a secondary has not yet received the latest zone transfer.
dig +short SOA example.com @ns1.example.com
dig +short SOA example.com @ns2.example.comOther useful options
| Option | Effect |
|---|---|
| `-x ADDRESS` | Reverse lookup, which queries a PTR record. |
| `+tcp` | Force TCP instead of UDP. |
| `+norecurse` | Clear the `rd` flag, so the server answers only from its own data or cache. |
| `+dnssec` | Request DNSSEC records and set the DO bit. |
| `+nssearch` | Query every authoritative server for the zone and print each SOA serial. |
| `-4` / `-6` | Use IPv4 or IPv6 to reach the server. |
| `+time=N` `+tries=N` | Change the timeout and retry count. |
+norecurse lets you test whether a resolver has a name cached. A cached answer comes back with a decreasing TTL. A cache miss returns no answer, or a referral.
A reverse lookup looks like this:
dig -x 192.0.2.10 +noall +answerdig builds the name 10.2.0.192.in-addr.arpa. and asks for its PTR record.
A repeatable debugging routine
Instead of a diagram, follow this order, which narrows the problem step by step.
- Ask your normal resolver:
dig name type. Note the status, flags, and TTL. - Ask a different public resolver with
@. If the two agree, the problem is probably not your local resolver. - Run
dig +trace name. This checks the delegation chain from the root. - Query each authoritative server for the zone. Compare answers and SOA serials.
- Check what the application sees. The
digtool bypasses the system resolver, so usegetaddrinfoto see what a program gets.
import socket
results = socket.getaddrinfo("example.com", 443, type=socket.SOCK_STREAM)
for family, socktype, proto, canonname, sockaddr in results:
print(family.name, sockaddr[0])If dig shows an answer but this script fails or returns something different, look at the hosts file, search domains, or the system's resolver order, not at DNS itself.
Common mistakes
- Treating an empty
NOERRORas a failure of the zone. It only means no record of that type exists. - Reading the TTL from a resolver as the zone's configured TTL. Only an authoritative answer shows the configured value.
- Forgetting that
digignores the hosts file and search domains. A name that works inpingmay fail indig, and the reverse. - Testing a change against a cache that still holds the old answer. Query the authoritative server first.
- Forgetting the trailing dot in names inside a zone file. Without it, the zone origin is appended.
Exercise
You change an A record, and dig @ns1.example.com www.example.com A returns the new address with the aa flag. A colleague's laptop still reaches the old address. Which two further dig checks would you run, and what would each tell you?
Show answer
First run dig +noall +answer www.example.com A (or dig @RESOLVER ... against the laptop's resolver) and read the TTL. A nonzero, decreasing TTL with the old address means that resolver still has the old answer cached, and it will update when the TTL runs out. Second, run dig +short SOA example.com against each authoritative server and compare serials. If one server has an older serial, it still serves the old data, so fix zone transfer. If both match and the resolver is correct, check the laptop itself for a hosts file entry or an application-level cache.
Exercise
A query returns status: NOERROR and an empty ANSWER SECTION. What does that mean, and how does it differ from NXDOMAIN?
Show answer
The name exists, but it has no record of the requested type (NODATA). NXDOMAIN means the name itself does not exist. For example, a host with only an A record returns NOERROR with no answer for an AAAA query.