Skip to content
Tutorials Center
My learning

Topics

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.

Terminal
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.

CONSOLE
$ dig +short example.com A
93.184.216.34
$ dig +short example.com MX
10 mail.example.com.
Bash What the system resolver knows locally

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

Terminal
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 dig output: the header shows status: (NOERROR, NXDOMAIN for a name that does not exist, SERVFAIL for a server problem); the answer section lists name, TTL (seconds the answer may be cached), class, type and data.
  • Use +short in scripts, but remember an empty result also means "no such record", and CNAME chains print every hop.
  • System resolver versus DNS. dig queries DNS directly and ignores /etc/hosts and nsswitch.conf; getent hosts and ping use 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.com against dig @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, then dig @ns1.example.com example.com.
  • Missing trailing dot: fully qualified names end with . (mail.example.com.); it is normal in answers.
  • ANY queries are often refused or truncated by modern servers; ask for specific types.
  • nslookup is deprecated by many vendors but works for quick checks; its output is less consistent, so do not parse it in scripts.
  • Simpler tool: host gives short human-friendly answers with the same package.