Linux Command Reference ยท Networking
Linux dig and nslookup commands
Reference for dig and nslookup on Linux: query A, AAAA, MX, TXT and NS records, use specific servers, trace delegation, do reverse lookups, and read the answers.
dig and nslookup ask DNS servers questions: what address does this name have, which servers handle its mail, what text records exist. They come from the bind-utils or dnsutils package. dig prints the raw answer in a consistent, scriptable format and is the tool of choice; nslookup is older, interactive-capable and still common on Windows.
When a site "doesn't load" but the IP works, DNS is the first suspect, and these tools tell you what the resolver really sees.
dig example.com dig +short example.com MX dig @8.8.8.8 example.com AAAA nslookup example.com
Try it
Neither tool is installed in our sandbox and there is no network, so queries are read-only. getent hosts asks the system resolver (the same path your programs use), and it can answer for names in /etc/hosts, which works offline.
Read-only: a short dig answer.
$ dig +short example.com A 93.184.216.34 $ dig +short example.com MX 10 mail.example.com.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
dig [@SERVER] [NAME] [TYPE] [+OPTIONS] dig -x ADDRESS nslookup [-type=TYPE] NAME [SERVER]
| Option | Description |
|---|---|
@SERVER |
Ask this DNS server instead of the system resolver. |
TYPE |
Record type: A, AAAA, MX, NS, TXT, CNAME, SOA, PTR, ANY. |
+short |
Print only the answer data. |
+trace |
Follow delegation from the root servers down. |
+noall +answer |
Show only the answer section, with TTLs. |
-x ADDRESS |
Reverse lookup of an IP address. |
+tcp |
Use TCP instead of UDP. |
+dnssec |
Request DNSSEC records. |
+time=N, +tries=N |
Timeout and retries. |
-4, -6 |
Force IP version for the query. |
In nslookup: server IP switches the server and set type=MX changes the record type, in interactive mode.
Returns: dig exits 0 even for NXDOMAIN (read the status: field instead); 9 means no reply from the server, 10 internal error. nslookup exits 1 if the lookup fails.
Usage notes
- Reading
digoutput: the header showsstatus:(NOERROR,NXDOMAINfor a name that does not exist,SERVFAILfor a server problem); the answer section lists name, TTL (seconds the answer may be cached), class, type and data. - Use
+shortin scripts, but remember an empty result also means "no such record", andCNAMEchains print every hop. - System resolver versus DNS.
digqueries DNS directly and ignores/etc/hostsandnsswitch.conf;getent hostsandpinguse the full system path. A name that works in one but not the other points to local configuration. - Compare resolvers:
dig @1.1.1.1 example.comagainstdig @8.8.8.8 example.com, or your internal server, to find stale caches or split-horizon DNS. - Check propagation by asking the authoritative servers:
dig +short NS example.com, thendig @ns1.example.com example.com. - Missing trailing dot: fully qualified names end with
.(mail.example.com.); it is normal in answers. ANYqueries are often refused or truncated by modern servers; ask for specific types.nslookupis deprecated by many vendors but works for quick checks; its output is less consistent, so do not parse it in scripts.- Simpler tool:
hostgives short human-friendly answers with the same package.
Related
host,ping,curl,ip,hostname- Lessons: Network Troubleshooting, Networking Basics