Dig command linux / unix examples
Hey,
So, what is DIG command used for? Well, for those who are not familiar DIG is a tool to perform DNS lookups. It has a lot of switches to find out details about the DNS record we are looking for. It is a pretty straight and direct command line utility to find DNS Records *if you know the switch to use*. So lets skip the introductions and check them out 🙂
Simply using the dig command followed by a domain name returns the A record of the domain.
Example:
; <<>> DiG 9.9.5-3ubuntu0.6-Ubuntu <<>> learntutors.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 11391
;; flags: qr rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 1
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;learntutors.com. IN A
;; ANSWER SECTION:
learntutors.com. 300 IN A 104.28.13.115
learntutors.com. 300 IN A 104.28.12.115
;; Query time: 105 msec
;; SERVER: 127.0.1.1#53(127.0.1.1)
;; WHEN: Thu Dec 17 10:59:27 IST 2015
;; MSG SIZE rcvd: 76
2) To find MX (Mail eXchanger) record:
Example:
; <<>> DiG 9.9.5-3ubuntu0.6-Ubuntu <<>> -t MX learntutors.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 25062
;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 1
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;learntutors.com. IN MX
;; ANSWER SECTION:
learntutors.com. 300 IN MX 20 mx2.zoho.com.
learntutors.com. 300 IN MX 50 mx3.zoho.com.
learntutors.com. 300 IN MX 10 mx.zoho.com.
;; Query time: 190 msec
;; SERVER: 127.0.1.1#53(127.0.1.1)
;; WHEN: Thu Dec 17 11:05:13 IST 2015
;; MSG SIZE rcvd: 108
3. Find Name Servers of a domain
Example:
; <<>> DiG 9.9.5-3-Ubuntu <<>> -t NS learntutors.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 17347
;; flags: qr rd ra ad; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 1
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 512
;; QUESTION SECTION:
;learntutors.com. IN NS
;; ANSWER SECTION:
learntutors.com. 21599 IN NS greg.ns.cloudflare.com.
learntutors.com. 21599 IN NS brenda.ns.cloudflare.com.
;; Query time: 253 msec
;; SERVER: 8.8.8.8#53(8.8.8.8)
;; WHEN: Thu Dec 17 14:22:50 IST 2015
;; MSG SIZE rcvd: 98
4. To find SOA Records
Example:
; <<>> DiG 9.9.5-3-Ubuntu <<>> -t SOA learntutors.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 32705
;; flags: qr rd ra ad; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 512
;; QUESTION SECTION:
;learntutors.com. IN SOA
;; ANSWER SECTION:
learntutors.com. 21599 IN SOA brenda.ns.cloudflare.com. dns.cloudflare.com. 2020142160 10000 2400 604800 3600
;; Query time: 173 msec
;; SERVER: 8.8.8.8#53(8.8.8.8)
;; WHEN: Thu Dec 17 14:25:19 IST 2015
;; MSG SIZE rcvd: 105
5. To find TXT Records
Example:
; <<>> DiG 9.9.5-3-Ubuntu <<>> -t TXT learntutors.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 54437
;; flags: qr rd ra ad; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 1
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 512
;; QUESTION SECTION:
;learntutors.com. IN TXT
;; ANSWER SECTION:
learntutors.com. 29 IN TXT “google-site-verification=ooCm8t9CMpIutAQpCVpZxsK3pkfIoint36vKLYqPcSs”
learntutors.com. 29 IN TXT “v=spf1 a mx ip4:192.186.245.100 ip4:198.252.101.197 include:servers.mcsv.net include:_spf.google.com include:zoho.com ~all”
;; Query time: 158 msec
;; SERVER: 8.8.8.8#53(8.8.8.8)
;; WHEN: Thu Dec 17 14:34:14 IST 2015
;; MSG SIZE rcvd: 260
6. To find ALL records of a Domain
Now instead of getting the whole output, to print out only the answer part, add +short at the end of the command.
Example:
104.28.12.115
104.28.13.115 /
20 mx2.zoho.com.
50 mx3.zoho.com.
10 mx.zoho.com.
Or the same short answer can be viewed appending | exec sed -n ‘/QUESTION/,/Query/p’ to the end of the dig command. What it does is that it prints all lines between the QUESTION and ANSWER SECTION of the output.
;; QUESTION SECTION:
;learntutors.com. IN SOA
;; ANSWER SECTION:
learntutors.com. 21599 IN SOA brenda.ns.cloudflare.com. dns.cloudflare.com. 2020142160 10000 2400 604800 3600
;; Query time: 165 msec
The choice of command depends on your personal preference as basically all it does is the same, its just the presentation which is different