Atlantic.Net Blog

How to Use Dig Command in Linux with Examples

Dig stands for “Domain Information Groper” used to gather DNS information. Generally, it is used by the system or network administrators for verifying DNS lookup and troubleshoot DNS-related problems. By default, the dig command sends a query to the DNS server listed in the /etc/resolv.com. It also allows you to query the specific DNS server.

In this tutorial, we will show you how to use the dig command through practical examples.

Step 1 – Install dig

By default, the dig command is not available in Linux.

For Debian and Ubuntu operating systems, install the dig command using the following command:

apt-get install dnsutils -y

For CentOS and RHEL operating systems, install the dig command using the following command:

yum install bind9-utils -y

After installing the dig command, verify the dig version using the following command:

dig -v

Sample output:

DiG 9.9.5-3ubuntu0.19-Ubuntu

Step 2 – Basic Dig Command

Running a dig command without any options will display the A record of the target domain.

For example, perform a DNS lookup for a domain ubuntu.org run the following command:

dig ubuntu.com

Sample output:

; <<>> DiG 9.9.5-3ubuntu0.19-Ubuntu <<>> ubuntu.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 9230
;; flags: qr rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 1

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

;; ANSWER SECTION:
ubuntu.com.		77	IN	A	91.189.88.181
ubuntu.com.		77	IN	A	91.189.88.180

;; Query time: 263 msec
;; SERVER: 127.0.1.1#53(127.0.1.1)
;; WHEN: Tue Aug 24 08:55:56 IST 2021
;; MSG SIZE  rcvd: 71

In the above output, you can find the main information in the ANSWER SECTION.

Step 3 – Query With Specific DNS Server

Running the dig command without any options will use the DNS server specified in the /etc/resolv.conf. You can also query the domain using the specific DNS server.

Run the following command to query the ubuntu.com domain using the DNS 4.4.4.4:

dig @8.8.4.4 ubuntu.com

Sample output:

; <<>> DiG 9.9.5-3ubuntu0.19-Ubuntu <<>> @8.8.4.4 ubuntu.com
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 25217
;; flags: qr rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 1

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

;; ANSWER SECTION:
ubuntu.com.		40	IN	A	91.189.88.181
ubuntu.com.		40	IN	A	91.189.88.180

;; Query time: 130 msec
;; SERVER: 8.8.4.4#53(8.8.4.4)
;; WHEN: Tue Aug 24 09:00:48 IST 2021
;; MSG SIZE  rcvd: 71

Step 4 – Querying ALL DNS Records Types

If you want to get the information of all DNS records, run the following command:

dig @8.8.4.4 ubuntu.com ANY +noall +answer

Sample output:

; <<>> DiG 9.9.5-3ubuntu0.19-Ubuntu <<>> @8.8.4.4 ubuntu.com ANY +noall +answer
; (1 server found)
;; global options: +cmd
ubuntu.com.		3599	IN	SOA	ns1.canonical.com. hostmaster.canonical.com. 2018054375 10800 3600 604800 3600
ubuntu.com.		3599	IN	MX	10 mx.canonical.com.
ubuntu.com.		599	IN	NS	ns1.canonical.com.
ubuntu.com.		599	IN	NS	ns2.canonical.com.
ubuntu.com.		599	IN	NS	ns3.canonical.com.
ubuntu.com.		59	IN	AAAA	2001:67c:1360:8001::2b
ubuntu.com.		59	IN	AAAA	2001:67c:1360:8001::2c
ubuntu.com.		59	IN	A	91.189.88.180
ubuntu.com.		59	IN	A	91.189.88.181

Step 5 – Querying Only Answer Section

You can use the options +noall and +answer to display only the answer section.

dig @8.8.4.4 ubuntu.com +noall +answer

Sample output:

; <<>> DiG 9.9.5-3ubuntu0.19-Ubuntu <<>> @8.8.4.4 ubuntu.com +noall +answer
; (1 server found)
;; global options: +cmd
ubuntu.com.		12	IN	A	91.189.88.181
ubuntu.com.		12	IN	A	91.189.88.180

Step 6 – Querying A Record

The “a record” in DNS is used to bind a domain name with an IP address. You can use the +short option to query the a record of a specified domain.

dig ubuntu.com A +short

Sample output:

91.189.88.180
91.189.88.181

Step 7 – Querying NS Record

The NS record contains the information of a list of authoritative DNS servers for a domain name. You can use the NS option to query NS records:

dig +nocmd ubuntu.com NS +noall +answer

Sample output:

ubuntu.com.		714	IN	NS	ns2.canonical.com.
ubuntu.com.		714	IN	NS	ns3.canonical.com.
ubuntu.com.		714	IN	NS	ns1.canonical.com.

Step 8 – Querying MX Record

An MX record is a mail exchange record used to specify the mail server. You can use the MX option to query the MX record:

dig +nocmd ubuntu.com MX +noall +answer

Sample output:

ubuntu.com.		4502	IN	MX	10 mx.canonical.com.

Step 9 – Querying TTL Record

You can use the TTL option to query the TTL record:

dig +nocmd ubuntu.com TTL +noall +answer

Sample output:

;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 50516
;; flags: qr rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 1

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

;; ANSWER SECTION:
ubuntu.com.		59	IN	A	91.189.88.180
ubuntu.com.		59	IN	A	91.189.88.181

;; Query time: 4 msec
;; SERVER: 127.0.1.1#53(127.0.1.1)
;; WHEN: Tue Aug 24 09:24:32 IST 2021
;; MSG SIZE  rcvd: 71

Step 10 – Perform Reverse Lookup

A reverse DNS lookup will display the information about the domain and hostname associated with an IP address. You can use the -x option followed by the IP address to perform a reverse DNS lookup:

dig +answer -x 8.8.8.8

Sample output:

; <<>> DiG 9.9.5-3ubuntu0.19-Ubuntu <<>> +answer -x 8.8.8.8
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 58576
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

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

;; ANSWER SECTION:
8.8.8.8.in-addr.arpa.	7200	IN	PTR	dns.google.

;; Query time: 580 msec
;; SERVER: 127.0.1.1#53(127.0.1.1)
;; WHEN: Tue Aug 24 09:27:00 IST 2021
;; MSG SIZE  rcvd: 73

Conclusion

In the above guide, we explained how to use the dig command to query the DNS server through various examples. Now you can perform DNS lookups for domains using various options. Give it a try today on your VPS from Altantic.Net.

Get a $250 Credit and Access to Our Free Tier!

Free Tier includes:
G3.2GB Cloud VPS a Free to Use for One Year
50 GB of Block Storage Free to Use for One Year
50 GB of Snapshots Free to Use for One Year