CrackOps
Interview PrepTracksLinuxAWS LabSoonInterview RoomSoon
Sign inStart free
CrackOps

Interview prep for the people who keep production up.

[email protected]
ProductInterview PrepTracksDashboardInterview Room
RolesDevOpsSREPlatformCloud
CompanyAboutContactPrivacyTerms
© CrackOps
Linux / Networking / dig

dig

Query DNS and read the full answer

Intermediatednsnetworkdiagnostics

dig asks a DNS server a question and prints exactly what came back: the status, flags, records and TTLs, plus which server answered and how long it took. It is the tool for debugging DNS, from a wrong IP after a cut-over to NXDOMAIN in a pod.

Syntaxdig [@SERVER] NAME [TYPE] [+short] [+noall +answer] [+trace] [-x ADDRESS]

Watch it run

You need the IP behind a name, and you want to understand every part of dig's output before you rely on it.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 2
dig example.com

Read it top to bottom: the status, the flags, what was asked, what came back, and who answered. Two A records means two IPs; clients pick one, which spreads the load.

Anatomy of a command

$
commanddig

Domain Information Groper, from the BIND tools (package bind9-dnsutils on Ubuntu).

How it works

dig builds a DNS query packet itself (it does not use the system's getaddrinfo, so /etc/hosts and nsswitch.conf are ignored) and sends it over UDP port 53 to the server given with @, or to the first nameserver in /etc/resolv.conf. On Ubuntu that is 127.0.0.53, the systemd-resolved stub, which forwards to the real resolver (in a cloud VPC, the provider's resolver). That recursive resolver answers from its cache or walks the tree: root servers refer it to the .com servers, which refer it to the domain's authoritative servers, which return the record. Every record carries a TTL telling resolvers how many seconds they may cache it. dig prints the header (status NOERROR, NXDOMAIN or SERVFAIL; flags such as aa for an authoritative answer, rd/ra for recursion), each section, the query time and the server used. Large answers switch to TCP. +trace skips the resolver and does the walk from the root itself.

dig asks the local stub, which asks a recursive resolver, which walks root, TLD and authoritative servers and caches the answer for its TTL.

never readA example.comforwardwho has .com?who has example?A example.com?dig @serverdigdevops-lab/etc/hostslocal namesskipped by digresolved stub127.0.0.53VPC resolver10.0.0.2 + cacheRoot serversthe '.' zone.com serversgtld-servers.netAuthoritativeexample.com zone
1/8
dig sends a query to resolv.conf's server

dig builds the DNS packet itself and sends it to the nameserver in /etc/resolv.conf, 127.0.0.53 on Ubuntu. Unlike ping or curl, it never looks at /etc/hosts.

← → to step

Options you'll actually use

@SERVERAsk this server instead of the one in /etc/resolv.conf
TYPERecord type to ask for: A, AAAA, CNAME, MX, NS, TXT, SOA, SRV, PTR, ANY
+shortPrint only the answer values, one per line (script-friendly)
+noall +answerPrint only the answer section, with names and TTLs
-x ADDRReverse lookup: find the PTR name for an IP
+traceResolve from the root servers down, showing each referral
+tcpQuery over TCP instead of UDP
+norecurseDon't ask for recursion: see only what that server knows
+searchApply the search domains from /etc/resolv.conf
+time=N +tries=NTimeout per try and number of tries
-4 / -6Use only IPv4 or IPv6 transport to reach the server

Common mistakes

Testing a new /etc/hosts entry with dig

dig talks to DNS servers directly and never reads /etc/hosts, so it shows the DNS answer, not what applications will use.

getent hosts api.devops-lab.io # uses nsswitch.conf like real programs
"I changed the record but dig still shows the old IP, the change failed"

Resolvers cache answers for the record's TTL. The change is probably live at the source.

dig @<authoritative-ns> name # then wait out the TTL
Treating NXDOMAIN and SERVFAIL the same

NXDOMAIN means the name definitely doesn't exist (fix the name or the zone). SERVFAIL means the resolver couldn't get an answer (broken delegation, DNSSEC failure, upstream down).

Read the status field, then use +trace for SERVFAIL.
Parsing the full dig output in scripts

The header, comments and sections change with versions and options.

dig +short name # or +noall +answer

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Look up the A record of localhost with dig. Why does it fail or differ in this offline sandbox, while ping localhost works?

  2. 2

    Show which DNS server dig will use by default on this machine.

  3. 3

    Write a dig command that prints only the IPv6 addresses of example.com, one per line.

  4. 4

    Write the command to ask the server 10.0.0.2 for the MX records of devops-lab.io, showing only the answer section with TTLs.

Related commands

nslookupLook up DNS names quicklypingCheck if a host answers and how fastcurlMake HTTP requests and debug APIskubectl execRun a command or shell inside a running container
PrevioustracerouteNextnslookup
Linux / Networking / dig

dig

Query DNS and read the full answer

Intermediatednsnetworkdiagnostics

dig asks a DNS server a question and prints exactly what came back: the status, flags, records and TTLs, plus which server answered and how long it took. It is the tool for debugging DNS, from a wrong IP after a cut-over to NXDOMAIN in a pod.

Syntaxdig [@SERVER] NAME [TYPE] [+short] [+noall +answer] [+trace] [-x ADDRESS]

Watch it run

You need the IP behind a name, and you want to understand every part of dig's output before you rely on it.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 2
dig example.com

Read it top to bottom: the status, the flags, what was asked, what came back, and who answered. Two A records means two IPs; clients pick one, which spreads the load.

Anatomy of a command

$
commanddig

Domain Information Groper, from the BIND tools (package bind9-dnsutils on Ubuntu).

How it works

dig builds a DNS query packet itself (it does not use the system's getaddrinfo, so /etc/hosts and nsswitch.conf are ignored) and sends it over UDP port 53 to the server given with @, or to the first nameserver in /etc/resolv.conf. On Ubuntu that is 127.0.0.53, the systemd-resolved stub, which forwards to the real resolver (in a cloud VPC, the provider's resolver). That recursive resolver answers from its cache or walks the tree: root servers refer it to the .com servers, which refer it to the domain's authoritative servers, which return the record. Every record carries a TTL telling resolvers how many seconds they may cache it. dig prints the header (status NOERROR, NXDOMAIN or SERVFAIL; flags such as aa for an authoritative answer, rd/ra for recursion), each section, the query time and the server used. Large answers switch to TCP. +trace skips the resolver and does the walk from the root itself.

dig asks the local stub, which asks a recursive resolver, which walks root, TLD and authoritative servers and caches the answer for its TTL.

never readA example.comforwardwho has .com?who has example?A example.com?dig @serverdigdevops-lab/etc/hostslocal namesskipped by digresolved stub127.0.0.53VPC resolver10.0.0.2 + cacheRoot serversthe '.' zone.com serversgtld-servers.netAuthoritativeexample.com zone
1/8
dig sends a query to resolv.conf's server

dig builds the DNS packet itself and sends it to the nameserver in /etc/resolv.conf, 127.0.0.53 on Ubuntu. Unlike ping or curl, it never looks at /etc/hosts.

← → to step

Options you'll actually use

@SERVERAsk this server instead of the one in /etc/resolv.conf
TYPERecord type to ask for: A, AAAA, CNAME, MX, NS, TXT, SOA, SRV, PTR, ANY
+shortPrint only the answer values, one per line (script-friendly)
+noall +answerPrint only the answer section, with names and TTLs
-x ADDRReverse lookup: find the PTR name for an IP
+traceResolve from the root servers down, showing each referral
+tcpQuery over TCP instead of UDP
+norecurseDon't ask for recursion: see only what that server knows
+searchApply the search domains from /etc/resolv.conf
+time=N +tries=NTimeout per try and number of tries
-4 / -6Use only IPv4 or IPv6 transport to reach the server

Common mistakes

Testing a new /etc/hosts entry with dig

dig talks to DNS servers directly and never reads /etc/hosts, so it shows the DNS answer, not what applications will use.

getent hosts api.devops-lab.io # uses nsswitch.conf like real programs
"I changed the record but dig still shows the old IP, the change failed"

Resolvers cache answers for the record's TTL. The change is probably live at the source.

dig @<authoritative-ns> name # then wait out the TTL
Treating NXDOMAIN and SERVFAIL the same

NXDOMAIN means the name definitely doesn't exist (fix the name or the zone). SERVFAIL means the resolver couldn't get an answer (broken delegation, DNSSEC failure, upstream down).

Read the status field, then use +trace for SERVFAIL.
Parsing the full dig output in scripts

The header, comments and sections change with versions and options.

dig +short name # or +noall +answer

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Look up the A record of localhost with dig. Why does it fail or differ in this offline sandbox, while ping localhost works?

  2. 2

    Show which DNS server dig will use by default on this machine.

  3. 3

    Write a dig command that prints only the IPv6 addresses of example.com, one per line.

  4. 4

    Write the command to ask the server 10.0.0.2 for the MX records of devops-lab.io, showing only the answer section with TTLs.

Related commands

nslookupLook up DNS names quicklypingCheck if a host answers and how fastcurlMake HTTP requests and debug APIskubectl execRun a command or shell inside a running container
PrevioustracerouteNextnslookup