← Full reference

dig

DNS queries — A, AAAA, MX, TXT, NS, reverse lookup, tracing; how dig differs from getent hosts

dig asks a DNS server directly and shows the whole answer: the records, their TTLs, the status and which server answered. It's how you check what DNS actually returns, without application caches or local overrides.

Syntax

dig [@server] [options] name [type] [+options]

The default type is A. The default server comes from /etc/resolv.conf. Argument order is free: dig MX example.com and dig example.com MX are equivalent.

Record types

TypeWhat it holds
A / AAAAIPv4 / IPv6 address
CNAMEan alias: the name points to another name
MXmail servers with priority (lower number — higher priority)
TXTarbitrary text: SPF, DKIM, domain ownership verification
NSDNS servers responsible for the zone
SOAzone parameters: primary server, serial number, timers
PTRname by IP (reverse zone); queried with -x
SRVa service: priority weight port host
CAAwhich certificate authorities may issue certificates for the domain

Dash options

OptionWhat it does
@serverask a specific DNS server (IP or name)
-t TYPErecord type — same as giving the type as a separate word
-x IPreverse query: name by address
-p portDNS server port (default 53)
-4 / -6talk to the server over IPv4 / IPv6 only
-f filebatch mode: one query per line of the file

Plus options

OptionWhat it does
+shortvalues only, no extra output
+noall +answeronly the answer section, but with TTL and type
+noall +answer +authoritythe answer and authority sections
+multilinelong records (SOA, DNSSEC) with per-line comments
+ttlunitshuman-readable TTL: 2m58s
+tracefollow the chain from the root servers to the authoritative one
+norecursedon't ask the server to look it up for us — ask only what it knows itself
+tcpquery over TCP instead of UDP
+time=N / +tries=Nwait timeout and number of attempts
+dnssecrequest DNSSEC signatures

Reading the full output

;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 2545
;; flags: qr rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 0

;; ANSWER SECTION:
example.com.		178	IN	A	104.20.23.154

;; Query time: 16 msec
;; SERVER: 192.168.65.7#53(192.168.65.7) (UDP)
PartMeaning
statusNOERROR — the name exists; NXDOMAIN — no such name; SERVFAIL — the server couldn't answer (often broken DNSSEC or a broken authoritative server); REFUSED — the server refused to answer
flagsqr — this is a response, rd — recursion requested, ra — available, aa — the answer comes from the zone's authoritative server
ANSWER SECTIONrecords: name, TTL in seconds (how much longer it may be cached), class IN, type, value
AUTHORITY SECTIONwho is responsible for the zone; in negative answers usually the zone's SOA
SERVERwhich server answered and over which protocol

NOERROR with ANSWER: 0 means "the name exists, but there are no records of this type".

Examples

# the domain's address — values only
dig +short example.com

# answer with TTL and type, without the rest of the noise
dig +noall +answer example.com

# mail servers, TXT records, the zone's servers
dig +short example.com MX
dig +short example.com TXT
dig +short example.com NS

# ask a specific server — compare answers from different DNS servers
dig @1.1.1.1 +short example.com

# ask the authoritative server directly: fresh data, no caches
dig @$(dig +short NS example.com | head -1) +noall +answer example.com

# reverse query: name by IP
dig +short -x 1.1.1.1

# answer status: NOERROR, NXDOMAIN, SERVFAIL
dig nosuch.invalid | grep status

# the resolution path from the root
dig +trace example.com

# how programs on this machine see the name (including /etc/hosts)
getent hosts localhost
getent ahostsv4 example.com

dig vs getent hosts

Programs (curl, ssh, a browser) resolve names through the system library. The order of sources is set by the hosts: line in /etc/nsswitch.conf, usually files dns: first /etc/hosts, then the DNS servers from /etc/resolv.conf. An entry in /etc/hosts wins over DNS.

dig doesn't look at /etc/hosts — it always goes to DNS. To check where a program will actually go, use getent:

CommandWhat it shows
getent hosts namethe first address found (tries IPv6 first)
getent ahosts nameall addresses the way a program gets them via getaddrinfo
getent ahostsv4 nameIPv4 only

There are simpler utilities too: host name and nslookup name — they also query DNS but show fewer details.

Pitfalls

  • dig doesn't see /etc/hosts. A name that exists only in /etc/hosts gives NXDOMAIN in dig, even though curl reaches it just fine. And vice versa: dig may show the "correct" IP while the program goes to an overridden one.
  • +short is silent on errors. Empty output happens both with NXDOMAIN and when there's no record of the requested type, and the exit code is still 0. Check the reason in the full output (status:).
  • getent hosts looks for IPv6 first. If /etc/hosts has only IPv4 for the name and DNS has AAAA, getent hosts shows the IPv6 from DNS. getent ahosts / ahostsv4 show what a program will get.
  • +short for a CNAME prints both the intermediate name and the addresses — the first line may not be an IP.
  • Cache. A recursive server returns a record from cache until its TTL expires. After a zone change the old answer can live on for another TTL seconds — compare with the authoritative server's answer.
  • Names without a trailing dot. In the output names are fully qualified, with a dot: example.com.. In a query you can leave the dot out.
  • In the sandbox external DNS may be unreachable: then dig waits, prints timed out and no servers could be reached, and exits with code 9.

See also

curl · ip · ss · ssh