Linux dig Command: Simplified Guide for Efficient DNS Lookups

Introduction:

The dig command is a staple for web developers needing to troubleshoot and analyze DNS (Domain Name System) issues. It stands for Domain Information Groper, providing detailed DNS information with simplicity and flexibility.

Check dig Installation:

Run `dig -v`` in your terminal. This command checks if dig is installed on your Linux machine by returning the installed version.

dig -v

DiG 9.10.6

Install dig if not found

sudo apt-get update
sudo apt-get install dnsutils

Perform Basic DNS Query:

Use dig example.com for a straightforward DNS lookup. This command fetches the A record of example.com, showing the IP address associated with the domain.

dig example.com

; <<>> DiG 9.10.6 <<>> example.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 11748
;; flags: qr rd ra ad; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 512
;; QUESTION SECTION:
;example.com. IN A

;; ANSWER SECTION:
example.com. 18293 IN A 93.184.216.34

;; Query time: 44 msec
;; SERVER: 8.8.8.8#53(8.8.8.8)
;; WHEN: Wed Dec 20 18:06:43 +07 2023
;; MSG SIZE rcvd: 56

Specify DNS Server for Query:

To query a specific DNS server, such as Google’s public DNS, use dig @8.8.8.8 example.com. This is useful for comparing how DNS resolution occurs through different servers.

dig @8.8.8.8 example.com

; <<>> DiG 9.10.6 <<>> @8.8.8.8 example.com
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 39681
;; flags: qr rd ra ad; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 512
;; QUESTION SECTION:
;example.com. IN A

;; ANSWER SECTION:
example.com. 18237 IN A 93.184.216.34

;; Query time: 47 msec
;; SERVER: 8.8.8.8#53(8.8.8.8)
;; WHEN: Wed Dec 20 18:07:39 +07 2023
;; MSG SIZE rcvd: 56

Reverse DNS Lookup:

dig -x 93.184.216.34 performs a reverse DNS lookup. Instead of translating a domain to an IP address, it does the opposite, revealing the domain linked to a given IP.

dig -x 93.184.216.34

; <<>> DiG 9.10.6 <<>> -x 93.184.216.34
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NXDOMAIN, id: 61393
;; flags: qr rd ra; QUERY: 1, ANSWER: 0, AUTHORITY: 1, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 512
;; QUESTION SECTION:
;34.216.184.93.in-addr.arpa. IN PTR

;; AUTHORITY SECTION:
216.184.93.in-addr.arpa. 436 IN SOA ns1.edgecastcdn.net. noc.edgecast.com. 1589310095 3600 600 604800 600

;; Query time: 40 msec
;; SERVER: 8.8.8.8#53(8.8.8.8)
;; WHEN: Wed Dec 20 18:09:00 +07 2023
;; MSG SIZE rcvd: 126

Batch Mode for Multiple Queries:

For querying multiple domains, dig offers a batch mode. Simply create a file with a list of domains and execute dig -f yourfile.txt. It’s an efficient way to handle bulk DNS queries.

Customize dig Defaults:

Customize dig's behavior by editing ~/.digrc. This allows you to set default options tailored to your regular usage patterns.

Conclusion:

dig is an invaluable tool for developers dealing with DNS. It’s precise, flexible, and provides a depth of information crucial for diagnosing and understanding domain resolutions.