Chapter 3 · Lesson 8 of 22
Domains and HTTPS

Check DNS records with dig

dig is a command line tool that asks a DNS server for the records of a domain, so you can see what the internet will resolve it to.

By the end you will

Look up a domain's DNS records with dig and ask a chosen resolver or nameserver.

Time
14 min
Level
Beginner
Check
4 questions
You need
  • A terminal on your server or computer
  • A domain you can look up

A site works from your laptop but not from a friend’s phone, or you changed a record an hour ago and nothing seems different. A browser tells you very little about why. dig asks DNS directly and shows exactly what each server answers, including how long the answer may be cached.

How it works#

DNS is a chain of lookups. Your computer asks a recursive resolver (your ISP’s, or a public one such as 1.1.1.1). If the resolver has no fresh cached answer, it walks down from the root servers to the domain’s authoritative nameservers, which hold the real records. It then caches the answer for the record’s TTL and returns it to you.

So two kinds of server can answer a dig. A resolver gives you the cached view, which may be old. An authoritative server gives you the configured truth. Comparing them is the main debugging trick in DNS.

A reply has a status. NOERROR means the query worked, even if the answer section is empty. NXDOMAIN means the name does not exist. SERVFAIL means the resolver could not get a valid answer. Reading that status first saves a lot of guessing.

Also remember that A records hold IPv4 addresses and AAAA records hold IPv6 addresses. A name can have both, and clients will often prefer IPv6 when it is present.

Step 1: Install dig#

On Ubuntu and Debian the tool comes in the dnsutils package:

bash
sudo apt update
sudo apt install dnsutils

On macOS it is already installed. Newer Debian and Ubuntu releases may name the package bind9-dnsutils, which apt pulls in through dnsutils.

What you should see: dig -v prints a version line.

Step 2: Look up an A record#

bash
dig example.com A

Find the ANSWER SECTION. It lists the name, the TTL in seconds, the class IN, the type A and the IPv4 address. The header line shows status: NOERROR on success. For a short output:

bash
dig +short example.com A

What you should see: one or more IP addresses, one per line. Run it twice a few seconds apart on a cached name and the TTL in the long form counts down.

Step 3: Pick which resolver to ask#

bash
dig +short example.com A @1.1.1.1

The part after @ is the server you query. Without it, dig uses your system’s resolver, which may cache. Try @8.8.8.8 as a second opinion.

What you should see: the same address, or an older one if that resolver has not refreshed.

Step 4: Look up nameservers and ask them directly#

bash
dig +short example.com NS

This lists the authoritative nameservers. Ask one of them for the answer straight from the source:

bash
dig +short example.com A @ns1.example.net

Replace ns1.example.net with a name from the previous command. The authoritative server is not cached, so it shows the record as you configured it.

What you should see: your current configured value. If this is wrong, fix it at your DNS provider. If it is right but a resolver is wrong, wait for the cache to expire.

Step 5: Check other record types and the whole path#

bash
dig +short example.com AAAA
dig +short example.com MX
dig +short example.com TXT
dig +trace example.com A

+trace does the walk from the root servers itself, so it ignores your resolver’s cache. It prints each step down to the authoritative answer.

What you should see: for +trace, a series of blocks listing root servers, then the .com servers, then your domain’s nameservers, and finally the answer.

Check it worked#

You are looking for the IP address of your server in the output. For a record you just created, the authoritative query should return the new address, and a public resolver such as @1.1.1.1 should match it once its cache has refreshed. Empty output from +short means no record of that type was found for that name.

Alternative: nslookup#

bash
nslookup example.com
nslookup example.com 1.1.1.1

The second form asks the resolver at 1.1.1.1. The answer appears under “Non-authoritative answer” when it came from a cache.

Edge cases and gotchas#

  • dig asks for A only by default. If your site is IPv6 only or you set a CNAME, a plain dig can look empty. Query AAAA or CNAME by name.
  • A stale IPv6 record sends some visitors to the old server. You updated the A record but forgot the AAAA record. Check both.
  • The system resolver may be a local stub. On Ubuntu, dig without @ often asks a local resolver at 127.0.0.53. Use @1.1.1.1 to bypass it.
  • A CNAME at the root cannot coexist with other records. The bare domain normally needs A or AAAA records. Your provider may offer an alias-style record instead.
  • Wrong name, right record. example.com and www.example.com are separate names. Check the exact one visitors use.
  • Trailing dot and search domains. dig host may try your machine’s search suffixes. A fully qualified name ends with a dot, as in example.com..
  • Negative answers are cached too. If you looked up a name before you created it, resolvers may remember NXDOMAIN for a while. Ask the authoritative server for the real state.

Troubleshooting#

SymptomLikely causeFix
command not found: digdnsutils not installedInstall dnsutils as in Step 1
status: NXDOMAINName does not existCheck the spelling and add the record at your DNS provider
status: SERVFAILResolver cannot get a valid answer, often a broken or mismatched nameserver setupTry another resolver, check the domain’s nameserver settings
status: NOERROR with empty answerName exists but not that record typeQuery the right type, or add the record
;; communications error or connection timed out; no servers could be reachedResolver unreachable or port 53 blockedTry @1.1.1.1, check the network and firewall
Resolver answer differs from authoritativeCache not expired yetWait out the TTL, then query again
+short prints a name, not an IPRecord is a CNAMEFollow the target with a second query
Answer is a different IP than the provider panel showsAsking a cache, or editing the wrong zoneAsk @ the authoritative server from NS

Real-world scenario#

You pointed example.com at a new server an hour ago and a teammate says it still loads the old one. You start with the authoritative server, since it is the truth:

bash
dig +short example.com NS
dig +short example.com A @ns1.example.net

It returns 203.0.113.10, the new address. Then you ask public resolvers:

bash
dig +short example.com A @1.1.1.1
dig +short example.com A @8.8.8.8

One returns the new address, the other the old one. So the change is saved and one cache has not expired. Running dig example.com A @8.8.8.8 without +short shows the remaining TTL counting down, which tells you roughly when it will refresh. Last, you check dig +short example.com AAAA and find an old IPv6 address that nobody updated. That explains why your teammate on an IPv6 network still sees the old site. You fix the AAAA record, and the story ends there. For what the TTL means for timing, see DNS propagation and TTL.

Quick reference#

bash
dig example.com A                 # full answer with TTL
dig +short example.com A          # just the value
dig +short example.com AAAA       # IPv6
dig +short example.com NS         # authoritative servers
dig +short example.com MX         # mail
dig +short example.com A @1.1.1.1 # ask a specific resolver
dig +trace example.com A          # walk from the root, no cache
dig -x 203.0.113.10               # reverse lookup

Next#

Now see why a change takes time to show up in DNS propagation and TTL.

Check what you learned4 questions
1What does dig +short example.com A print?
2What does the @1.1.1.1 part of a dig command do?
3How do you see a record exactly as you configured it, with no cache?
4dig example.com A returns an empty answer and status: NOERROR. What does that mean?

Frequently asked questions

What does +short do?

It prints only the answer values, such as an IP address, and hides the headers and extra sections. It is handy for quick checks and scripts.

Why do two resolvers give different answers?

Resolvers cache answers. If a record was changed recently, one may still hold the old value until its cache expires.

dig shows the right IP but my browser goes to the old server. Why?

The browser and operating system keep their own DNS caches, and dig may be asking a different resolver. Also check for an old entry in /etc/hosts and for an IPv6 (AAAA) record.

Why does dig example.com print nothing for a record I know exists?

By default dig asks for the A record only. Name the type you want, for example dig example.com MX or dig example.com TXT, and make sure you asked the right name.

View MD Edit
Share
Helpful?
Progress is saved in this browser only.

Keep going: guides and tutorials

View MD Edit
Share
Helpful?
Run this on your own server

Levelrail deploys apps from git with health-checked releases and one-click rollback.

docker pull ghcr.io/glincker/levelrail:latest Install Levelrail From GLINR Studios