brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Guide: dig for beginners, look up DNS records and read the answer

Guide: dig for beginners, look up DNS records and read the answer

Guide dns

13 steps 8 min read

What you get, and what you need

dig answers the question of what DNS says in one line. By the end of this guide you can look up any record of a domain, read every part of the answer and tell the difference between “the name does not exist” and “the server is having a bad day”.

You need a Linux or macOS terminal and about fifteen minutes. macOS ships with dig. On Debian and Ubuntu it comes in a package:

# Debian, Ubuntu (older releases call the package dnsutils)
sudo apt install bind9-dnsutils

# check that it works
dig -v

One thing about the screenshots. The machine that made them cannot send plain DNS packets, so I asked Google’s public resolver over HTTPS instead, which is why every command starts with +https @dns.google and the footer says (HTTPS). On your own machine you type the shorter version without those two parts, and the answer section looks the same. Only the SERVER line differs.

Step 1 of 13: Ask your first question

The simplest dig command is the name of a domain. Without a record type dig asks for the A record, the IPv4 address behind the name:

dig example.com

That is a lot of text for one address. dig prints everything the server sent back, and once you know where to look the noise disappears. The screenshot below is a real run.

Terminal output of dig example.com: header, question, answer section with two A records, and the footer with query time and server
A plain lookup: the answer is the two lines in the middle

Step 2 of 13: Read the answer, section by section

The same output again, now with colours. The blue block is the header, and the part that matters is status: NOERROR: the server understood the question and had no problem answering it. The pink block repeats the question, handy when you have typed three commands and forgot which one this is. The green block is the answer: the name, the TTL, the class (always IN, for internet), the record type and the value.

The number after the name is the TTL, in seconds: how long a resolver may keep this answer before it asks again. A TTL of 87 on an answer that was 300 seconds when it was fresh simply means the resolver stored it a while ago. The teal block at the bottom tells you how long the question took and which server answered it. When an answer surprises me, that last line is the first thing I check.

The dig output of the previous step with four coloured blocks: header, question, answer and footer, each with a short explanation
The four parts of every dig answer

Most of the time you only want the value. Add +short and dig prints just that, one line per record:

dig +short example.com

I would use this form in a script or a one-liner. It has a downside: you lose the status and the TTL, so when the output is empty you cannot tell why. For that case, run it again without +short.

Terminal output of dig +short example.com showing two IPv4 addresses
+short keeps only the values

Step 4 of 13: Ask for another record type

A name can hold more than one kind of record. Put the type after the name. AAAA is the IPv6 address, the longer one with colons:

dig example.com AAAA

If a site works on IPv4 but not for people on IPv6, comparing the A and the AAAA answer is a good first step. If there is no AAAA record the answer section is empty, and that is perfectly normal.

Terminal output of dig example.com AAAA with two IPv6 addresses in the answer section
The AAAA record holds the IPv6 addresses

Step 5 of 13: Find out where the mail goes

Mail does not use the A record. It uses MX records, and every one has a number in front of the server name. The lowest number is tried first; the others are backups:

dig gmail.com MX

Here 5 wins and the servers with 10, 20, 30 and 40 take over when it is down. It is the command to run after changing mail settings at a registrar, for a .nl domain as much as for any other, to see whether the world already sees the new records.

Terminal output of dig gmail.com MX with five mail servers and priorities 5, 10, 20, 30 and 40
MX records: the lowest number is the preferred mail server

Step 6 of 13: See who runs the domain with NS

The NS records name the servers that hold the authoritative answers for a domain. I combine them with +noall +answer, which switches everything off except the answer section but keeps the TTL:

dig +noall +answer iana.org NS
Terminal output of dig +noall +answer iana.org NS listing four name servers
Only the answer lines, with the TTL still visible

Step 7 of 13: Read the notes in TXT records

TXT records hold free text, and a few uses became standard. A line that starts with v=spf1 is an SPF policy: it says which servers may send mail for the domain. In this example the policy is -all with nothing in front of it, which means nobody may. The other line is a verification code, the kind a service asks you to add to prove the domain is yours.

dig +noall +answer example.com TXT
Terminal output of dig +noall +answer example.com TXT showing an SPF record and a verification string
An SPF policy and a verification code

Step 8 of 13: Follow an alias with CNAME

Some names are only a pointer to another name. dig follows it for you and prints the whole chain, so you see both steps:

dig +noall +answer www.wikipedia.org

The first line says www.wikipedia.org is a CNAME for dyna.wikimedia.org, and the second line is the address of that name. If a site you manage shows the wrong content, check whether a CNAME sends visitors somewhere you forgot about.

Terminal output showing www.wikipedia.org as a CNAME for dyna.wikimedia.org followed by its A record
A CNAME and the address it leads to

Step 9 of 13: Look up a name from an IP address

To go the other way, from an address to a name, use -x. This is called a reverse lookup and it asks for a PTR record:

dig -x 8.8.8.8

It only works when whoever owns the address has set a name for it, so an empty answer is no proof that something is broken. It is useful for checking what a server calls itself, for example in mail logs.

Terminal output of dig -x 8.8.8.8 with a PTR record that points to dns.google
A reverse lookup answers with a name

Step 10 of 13: Know what NXDOMAIN means

Now a question with no good answer. I asked for a name that does not exist under iana.org. Look at the status in the header: it is NXDOMAIN instead of NOERROR, and the answer section is gone. NXDOMAIN means the name does not exist. The SOA line in the authority section tells caches how long they may remember that “no”.

Two other statuses are worth knowing. SERVFAIL means the server could not find out, which is a problem with the domain’s own DNS or with the resolver. REFUSED means the server does not want to answer you at all.

Terminal output with status NXDOMAIN highlighted and an SOA record in the authority section
NXDOMAIN: the name does not exist

Step 11 of 13: Ask a different DNS server

By default dig asks the resolver your computer uses, which is usually your router handing out your provider’s DNS server. Put @ in front of an address to ask someone else:

dig @1.1.1.1 example.com

If your own resolver gives a different answer from a public one, that is the clue: a stale cache or a record that has not reached your provider yet. After a DNS change at your registrar, compare the two and wait for the TTL of the old record to run out before you decide something is wrong.

Terminal output of dig @1.1.1.1 with the footer line SERVER 1.1.1.1 highlighted
The footer shows which server answered

Step 12 of 13: Read the SOA record

The SOA record is the administrative card of a domain. +multiline prints it in a readable way, with a comment next to each number:

dig +noall +answer +multiline iana.org SOA

The serial number goes up whenever the zone changes, which is how secondary servers know to fetch a new copy. The refresh, retry and expire values are timers for those servers. As a beginner you can mostly ignore them, but it is good to know where they live.

Terminal output of dig +multiline for the SOA record of iana.org with serial, refresh, retry, expire and minimum
The SOA record with +multiline

Step 13 of 13: Keep the cheat sheet next to you

Everything from this guide on one card. You can also download it as a one-page PDF.

dig cheat sheetdig [@server] name [type] [+options]
Record types
A
IPv4 address
AAAA
IPv6 address
MX
mail servers, lowest number first
NS
name servers of the domain
TXT
free text: SPF, verification
CNAME
alias of another name
SOA
zone info: serial, refresh, expire
Shorter output
+short
only the values
+noall +answer
only the answer lines
+multiline
readable SOA
+trace
walk down from the root servers
Servers and flags
@1.1.1.1
ask this server
-x IP
reverse lookup
-4 -6
force IPv4 or IPv6
+tcp
TCP instead of UDP
+https
DNS over HTTPS
Read the status
NOERROR
it worked
NXDOMAIN
the name does not exist
SERVFAIL
the server could not find out
REFUSED
the server will not answer you
in the shelldig example.comdig +short example.com AAAAdig @1.1.1.1 example.com MXdig +noall +answer iana.org NS

One option I did not show a screenshot of is +trace. It starts at the root servers and follows the delegation down, one step at a time, instead of trusting your resolver’s cache. When a lookup works for you and not for a friend, it shows where the chain breaks. Plain DNS packets are blocked on the machine I used, so I leave the screenshot out and only describe it.

I would start with dig +short, and when the answer is empty or odd, run it again without +short and read the status.

Further reading

Link