Sub topic 12
Troubleshooting with dig and friends
This chapter assumes you know the DNS hierarchy, how a recursive resolver works, the core record types, and how TTL controls caching. You also need a command line with dig installed. It is part of the dnsutils or bind-utils package on most Linux systems and is built into macOS.
A method for diagnosing problems
Most "the site is down" reports fall into one of four layers. Check them in order and stop at the first one that fails.
- Does the name resolve at all?
- Does it resolve to the right value?
- Do all resolvers agree, or is a cache holding an old answer?
- Does the host respond once you reach the address?
dig answers the first three. Other tools cover the fourth.
Reading a record with dig
Ask for one record type at a time. The +short option prints only the answer values.
dig example.com A +short
dig example.com AAAA +short
dig example.com MX +short
dig example.com NS +shortAn empty result means one of two things: the name has no record of that type, or the name does not exist. Run dig example.com A without +short and read the status: field in the header.
| Status | Meaning | Typical cause |
|---|---|---|
| NOERROR with answers | The record exists | Normal |
| NOERROR with no answers | The name exists, but not this type | Missing AAAA or MX record |
| NXDOMAIN | The name does not exist | Typo, missing record, expired domain |
| SERVFAIL | The resolver could not get a valid answer | Broken delegation, dead name servers, failed DNSSEC validation |
| REFUSED | The server declined to answer | Querying a server that is not authoritative or does not allow you |
The second number in each answer line is the remaining TTL in seconds. If it counts down between runs, you are looking at a cached answer.
Walking the delegation with +trace
When the answer is wrong or missing, find where the chain breaks. The +trace option makes dig do the resolver's job itself.
dig +trace www.example.comThe output shows the root servers, then the TLD servers, then the authoritative servers for the zone. Compare the name servers listed at the TLD step with the ones in your zone. If they differ, the registrar still has old name servers. If the trace stops with no authoritative answer, the listed servers are down or do not host the zone.
Comparing resolvers
A change can be live on the authoritative servers and still be hidden by a cache. Use @ to send the same query to different resolvers.
dig @1.1.1.1 example.com ATo bypass caches entirely, query an authoritative server directly. Take a name from dig example.com NS +short and use it with @. The answer there is the truth. If it is correct but a public resolver shows something else, the cause is caching. Wait for the old TTL to run out.
Checking what the operating system sees
dig talks to DNS directly. Your programs use the operating system's resolver, which also reads the hosts file and has its own cache. If dig works but a program fails, test the system path.
import socket
for family, _, _, _, sockaddr in socket.getaddrinfo("example.com", 443, type=socket.SOCK_STREAM):
print(family.name, sockaddr[0])If this prints different addresses than dig, look for an entry in the hosts file or a stale local cache.
Common failures and their signs
- Works on one network, not another. The resolvers hold different cached data. Compare with
@queries. - Site loads over IPv4 but fails for some users. A wrong AAAA record. Check
dig example.com AAAA +short. - A CNAME at the zone apex fails or breaks mail. A CNAME cannot coexist with other records at the same name. Use A and AAAA records at the apex.
- Email bounces. Check
dig example.com MX +short. An MX target must be a hostname with an address record, not a CNAME. - DNS is correct but the site is still down. The problem is past DNS. Test the host with
curl -I https://example.comand look at the status code, redirects, or certificate errors.
Exercise
dig @1.1.1.1 example.com A returns an old IP address, but querying the zone's own name server returns the new one. What is the cause, and what should you do?
Show answer
A cache on the public resolver still holds the old record. The zone is correct. Wait for the old TTL to expire, since the remaining TTL in the answer shows how long. No change to the zone is needed.
Exercise
dig example.com A +short prints nothing. Which command shows whether the name is missing or only that record type is missing, and what do you look for?
Show answer
Run dig example.com A without +short and read the status in the header. NXDOMAIN means the name does not exist. NOERROR with no answer section means the name exists but has no A record.