2006
09.13

Using Dig Instead of NSLookup

If you’re an old-timer you probably find yourself automatically typing in nslookup when you go to troubleshoot DNS issues. It’s a familiar interface and you don’t have time to learn a bunch of new command line switches that do the exact same thing. Besides, nslookup has been threatening depracation for a long time now, yet it is still in every new version of Linux that comes out. Luckily, dig is not to tough to learn. You should resolve yourself today to learn it’s syntax because it really can save you some time.

Here is the very basic usage syntax:

> dig [@server-name] [domain] [q-type] host

See there, that is totally painless. Let’s learn by example. Here is how you would look up all the mail exchanger(mx) records for gmail.com:

$user@localhost:>> dig gmail.com mx

That would produce these results:

; <<>> DiG 9.3.1 <<>> gmail.com mx
;; global options:  printcmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 15796
;; flags: qr rd ra; QUERY: 1, ANSWER: 5, AUTHORITY: 4, ADDITIONAL: 4

;; QUESTION SECTION:
;gmail.com.                     IN      MX

;; ANSWER SECTION:
gmail.com.              2638    IN      MX      5 gmail-smtp-in.l.google.com.
gmail.com.              2638    IN      MX      10 alt1.gmail-smtp-in.l.google.com.
gmail.com.              2638    IN      MX      10 alt2.gmail-smtp-in.l.google.com.
gmail.com.              2638    IN      MX      50 gsmtp163.google.com.
gmail.com.              2638    IN      MX      50 gsmtp183.google.com.

;; AUTHORITY SECTION:
gmail.com.              45177   IN      NS      ns3.google.com.
gmail.com.              45177   IN      NS      ns4.google.com.
gmail.com.              45177   IN      NS      ns1.google.com.
gmail.com.              45177   IN      NS      ns2.google.com.

;; ADDITIONAL SECTION:
ns1.google.com.         174016  IN      A       216.239.32.10
ns2.google.com.         174016  IN      A       216.239.34.10
ns3.google.com.         174016  IN      A       216.239.36.10
ns4.google.com.         174016  IN      A       216.239.38.10

;; Query time: 330 msec
;; SERVER: 127.0.0.1#53(127.0.0.1)
;; WHEN: Tue Sep 12 15:55:28 2006
;; MSG SIZE  rcvd: 294

If you wanted to look up the name servers(ns) for gmail.com, but use a specific google DNS server(@servername) to do it, you could do this:

$user@localhost:>> dig @ns2.google.com gmail.com ns

; <<>> DiG 9.3.1 <<>> @ns2.google.com gmail.com ns
; (1 server found)
;; global options:  printcmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 17405
;; flags: qr aa rd; QUERY: 1, ANSWER: 4, AUTHORITY: 0, ADDITIONAL: 4

;; QUESTION SECTION:
;gmail.com.                     IN      NS

;; ANSWER SECTION:
gmail.com.              345600  IN      NS      ns2.google.com.
gmail.com.              345600  IN      NS      ns3.google.com.
gmail.com.              345600  IN      NS      ns4.google.com.
gmail.com.              345600  IN      NS      ns1.google.com.

;; ADDITIONAL SECTION:
ns2.google.com.         345600  IN      A       216.239.34.10
ns3.google.com.         345600  IN      A       216.239.36.10
ns4.google.com.         345600  IN      A       216.239.38.10
ns1.google.com.         345600  IN      A       216.239.32.10

;; Query time: 105 msec
;; SERVER: 216.239.34.10#53(216.239.34.10)
;; WHEN: Tue Sep 12 16:39:20 2006
;; MSG SIZE  rcvd: 170

If you don’t specify a server with the @servername switch, then it defaults to whatever server is listed in resolv.conf. The real power of dig though is in it’s global options. For instance, the +short option makes dig output only the results instead of printing in a report type format. It’s quite useful when combined with bash scripting. For example, if you wanted to store all of gmail’s name servers in an array and then ping them in turn, you could do something like this:

#!/bin/sh

gmailers=(`dig +short gmail.com ns`)

count=1
max=${#gmailers[*]}
while [ $count -lt $max ]
do
  ping -c1 ${gmailers[$count]}
  count=$((count+1))
done

Learning dig only takes a few minutes but it can save you tons of time down the road. Do yourself a favor the next time you need to troubleshoot an e-mail problem and you reach for nslookup. Type man dig instead and make yourself learn it.

Switch to our mobile site