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
| Type | What it holds |
|---|---|
A / AAAA | IPv4 / IPv6 address |
CNAME | an alias: the name points to another name |
MX | mail servers with priority (lower number — higher priority) |
TXT | arbitrary text: SPF, DKIM, domain ownership verification |
NS | DNS servers responsible for the zone |
SOA | zone parameters: primary server, serial number, timers |
PTR | name by IP (reverse zone); queried with -x |
SRV | a service: priority weight port host |
CAA | which certificate authorities may issue certificates for the domain |
Dash options
| Option | What it does |
|---|---|
@server | ask a specific DNS server (IP or name) |
-t TYPE | record type — same as giving the type as a separate word |
-x IP | reverse query: name by address |
-p port | DNS server port (default 53) |
-4 / -6 | talk to the server over IPv4 / IPv6 only |
-f file | batch mode: one query per line of the file |
Plus options
| Option | What it does |
|---|---|
+short | values only, no extra output |
+noall +answer | only the answer section, but with TTL and type |
+noall +answer +authority | the answer and authority sections |
+multiline | long records (SOA, DNSSEC) with per-line comments |
+ttlunits | human-readable TTL: 2m58s |
+trace | follow the chain from the root servers to the authoritative one |
+norecurse | don't ask the server to look it up for us — ask only what it knows itself |
+tcp | query over TCP instead of UDP |
+time=N / +tries=N | wait timeout and number of attempts |
+dnssec | request 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)
| Part | Meaning |
|---|---|
status | NOERROR — 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 |
flags | qr — this is a response, rd — recursion requested, ra — available, aa — the answer comes from the zone's authoritative server |
ANSWER SECTION | records: name, TTL in seconds (how much longer it may be cached), class IN, type, value |
AUTHORITY SECTION | who is responsible for the zone; in negative answers usually the zone's SOA |
SERVER | which 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:
| Command | What it shows |
|---|---|
getent hosts name | the first address found (tries IPv6 first) |
getent ahosts name | all addresses the way a program gets them via getaddrinfo |
getent ahostsv4 name | IPv4 only |
There are simpler utilities too: host name and nslookup name — they also query DNS but show fewer details.
Pitfalls
digdoesn't see/etc/hosts. A name that exists only in/etc/hostsgivesNXDOMAINindig, even thoughcurlreaches it just fine. And vice versa:digmay show the "correct" IP while the program goes to an overridden one.+shortis silent on errors. Empty output happens both withNXDOMAINand when there's no record of the requested type, and the exit code is still0. Check the reason in the full output (status:).getent hostslooks for IPv6 first. If/etc/hostshas only IPv4 for the name and DNS hasAAAA,getent hostsshows the IPv6 from DNS.getent ahosts/ahostsv4show what a program will get.+shortfor 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
digwaits, printstimed outandno servers could be reached, and exits with code 9.