10 Linux Dig (Domain Information Groper) Commands to Query DNS

In our previous article, we have explained nslookup command examples and usage, which is a networking command-line tool used for querying and getting information of DNS (Domain Name System).

Here, in this article, we come up with another command line tool called dig, which is much similar to the Linux nslookup tool. We will see the usage of the dig command closely with their examples and usage.

[ You might also like: How to Install and Use dig and nslookup Commands in Linux ]

Dig stands for (Domain Information Groper) is a network administration command-line tool for querying Domain Name System (DNS) name servers.

It is useful for verifying and troubleshooting DNS problems and also to perform DNS lookups and displays the answers that are returned from the name server that was queried.

Dig is part of the BIND domain name server software suite. dig command replaces older tools such as nslookup and the host. dig tool is available in major Linux distributions.

1. Query Domain “A” Record

# dig yahoo.com

; <<>> DiG 9.16.1-Ubuntu <<>> yahoo.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 20076
;; flags: qr rd ra; QUERY: 1, ANSWER: 6, AUTHORITY: 0, ADDITIONAL: 1

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

;; ANSWER SECTION:
yahoo.com.		387	IN	A	98.137.11.163
yahoo.com.		387	IN	A	74.6.143.26
yahoo.com.		387	IN	A	74.6.143.25
yahoo.com.		387	IN	A	74.6.231.20
yahoo.com.		387	IN	A	74.6.231.21
yahoo.com.		387	IN	A	98.137.11.164

;; Query time: 4 msec
;; SERVER: 127.0.0.53#53(127.0.0.53)
;; WHEN: Fri Dec 10 12:58:13 IST 2021
;; MSG SIZE  rcvd: 134

The above command causes dig to look up the "A" record for the domain name yahoo.com. Dig command reads the /etc/resolv.conf file and querying the DNS servers listed there. The response from the DNS server is what dig displays.

Let us understand the output of the commands:

  • Lines beginning with ; are comments not part of the information.
  • The first line tells us the version of the dig (9.16.1) command.
  • Next, dig shows the header of the response it received from the DNS server.
  • Next comes the question section, which simply tells us the query, which in this case is a query for the "A" record of yahoo.com. The IN means this is an Internet lookup (in the Internet class).
  • The answer section tells us that yahoo.com has the IP address 98.137.11.163.
  • Lastly, there are some stats about the query. You can turn off these stats using the +nostats option.

2. Query Domain “A” Record with +short

By default, dig is quite verbose. One way to cut down the output is to use the +short option. which will drastically cut the output as shown below.

# dig yahoo.com +short

98.137.11.164
74.6.231.21
74.6.231.20
74.6.143.25
74.6.143.26
98.137.11.163

Note: By default, dig looks for the "A" record of the domain specified, but you can specify other records also. The MX or Mail eXchange record tells mail servers how to route the email for the domain. Likewise TTL, SOA, etc.

3. Querying MX Record for Domain

Querying different types of DNS resource records only.

# dig yahoo.com MX

; <<>> DiG 9.16.1-Ubuntu <<>> yahoo.com MX
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 60630
;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 65494
;; QUESTION SECTION:
;yahoo.com.			IN	MX

;; ANSWER SECTION:
yahoo.com.		51	IN	MX	1 mta6.am0.yahoodns.net.
yahoo.com.		51	IN	MX	1 mta5.am0.yahoodns.net.
yahoo.com.		51	IN	MX	1 mta7.am0.yahoodns.net.

;; Query time: 4 msec
;; SERVER: 127.0.0.53#53(127.0.0.53)
;; WHEN: Fri Dec 10 13:03:32 IST 2021
;; MSG SIZE  rcvd: 117

4. Querying SOA Record for Domain

# dig yahoo.com SOA

; <<>> DiG 9.16.1-Ubuntu <<>> yahoo.com SOA
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 25140
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 65494
;; QUESTION SECTION:
;yahoo.com.			IN	SOA

;; ANSWER SECTION:
yahoo.com.		1800	IN	SOA	ns1.yahoo.com. hostmaster.yahoo-inc.com. 
2021121001 3600 300 1814400 600

;; Query time: 128 msec
;; SERVER: 127.0.0.53#53(127.0.0.53)
;; WHEN: Fri Dec 10 13:04:08 IST 2021
;; MSG SIZE  rcvd: 99

5. Querying TTL Record for Domain

# dig yahoo.com TTL

; <<>> DiG 9.16.1-Ubuntu <<>> yahoo.com TTL
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 64017
;; flags: qr rd ra; QUERY: 1, ANSWER: 6, AUTHORITY: 0, ADDITIONAL: 1

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

;; ANSWER SECTION:
yahoo.com.		1606	IN	A	74.6.143.25
yahoo.com.		1606	IN	A	74.6.231.21
yahoo.com.		1606	IN	A	74.6.143.26
yahoo.com.		1606	IN	A	98.137.11.164
yahoo.com.		1606	IN	A	98.137.11.163
yahoo.com.		1606	IN	A	74.6.231.20

;; Query time: 4 msec
;; SERVER: 127.0.0.53#53(127.0.0.53)
;; WHEN: Fri Dec 10 13:04:58 IST 2021
;; MSG SIZE  rcvd: 134

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

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 65494
;; QUESTION SECTION:
;TTL.				IN	A

;; Query time: 0 msec
;; SERVER: 127.0.0.53#53(127.0.0.53)
;; WHEN: Fri Dec 10 13:04:58 IST 2021
;; MSG SIZE  rcvd: 32

6. Querying Only Answer Section

# dig yahoo.com +nocomments +noquestion +noauthority +noadditional +nostats

; <<>> DiG 9.16.1-Ubuntu <<>> yahoo.com +nocomments +noquestion +noauthority +noadditional +nostats
;; global options: +cmd
yahoo.com.		1556	IN	A	74.6.231.20
yahoo.com.		1556	IN	A	98.137.11.163
yahoo.com.		1556	IN	A	98.137.11.164
yahoo.com.		1556	IN	A	74.6.143.26
yahoo.com.		1556	IN	A	74.6.231.21
yahoo.com.		1556	IN	A	74.6.143.25

7. Querying ALL DNS Records Types

# dig yahoo.com ANY +noall +answer

; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.10.rc1.el6 <<>> yahoo.com ANY +noall +answer
;; global options: +cmd
yahoo.com.              3509    IN      A       72.30.38.140
yahoo.com.              3509    IN      A       98.138.253.109
yahoo.com.              3509    IN      A       98.139.183.24
yahoo.com.              1709    IN      MX      1 mta5.am0.yahoodns.net.
yahoo.com.              1709    IN      MX      1 mta6.am0.yahoodns.net.
yahoo.com.              1709    IN      MX      1 mta7.am0.yahoodns.net.
yahoo.com.              43109   IN      NS      ns2.yahoo.com.
yahoo.com.              43109   IN      NS      ns8.yahoo.com.
yahoo.com.              43109   IN      NS      ns3.yahoo.com.
yahoo.com.              43109   IN      NS      ns1.yahoo.com.
yahoo.com.              43109   IN      NS      ns4.yahoo.com.
yahoo.com.              43109   IN      NS      ns5.yahoo.com.
yahoo.com.              43109   IN      NS      ns6.yahoo.com.

8. DNS Reverse Look-up

Querying DNS Reverse Look-up. Only display answer section with using +short.

# dig -x 72.30.38.140 +short

ir1.fp.vip.sp2.yahoo.com.

9. Querying Multiple DNS Records

Query multiple website’s DNS specific query viz. MX, NS, etc. records.

# dig yahoo.com mx +noall +answer redhat.com ns +noall +answer

; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.10.rc1.el6 <<>> yahoo.com mx +noall +answer redhat.com ns +noall +answer
;; global options: +cmd
yahoo.com.              1740    IN      MX      1 mta6.am0.yahoodns.net.
yahoo.com.              1740    IN      MX      1 mta7.am0.yahoodns.net.
yahoo.com.              1740    IN      MX      1 mta5.am0.yahoodns.net.
redhat.com.             132     IN      NS      ns1.redhat.com.
redhat.com.             132     IN      NS      ns4.redhat.com.
redhat.com.             132     IN      NS      ns3.redhat.com.
redhat.com.             132     IN      NS      ns2.redhat.com.

10. Create .digrc file

Create .digrc file under $HOME/.digrc to store default dig options.

# dig yahoo.com
yahoo.com.              3427    IN      A       72.30.38.140
yahoo.com.              3427    IN      A       98.138.253.109
yahoo.com.              3427    IN      A       98.139.183.24

We have store +noall +answer options permanently in the .digrc file under the user’s home directory. Now, whenever the dig command is executed it will show only answer section of the dig output. No Need to type every-time options like +noall +answer.

In this article, we tried to find out the dig command which may help you to search (DNS) Domain Name Service-related information. Share your thoughts through the comment box.

Hey TecMint readers,

Exciting news! Every month, our top blog commenters will have the chance to win fantastic rewards, like free Linux eBooks such as RHCE, RHCSA, LFCS, Learn Linux, and Awk, each worth $20!

Learn more about the contest and stand a chance to win by sharing your thoughts below!

Narad Shrestha
He has over 10 years of rich IT experience which includes various Linux Distros, FOSS and Networking. Narad always believes sharing IT knowledge with others and adopts new technology with ease.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

4 Comments

Leave a Reply
  1. Let’s say, IP of my PC=192.168.1.5
    IP of my local DNS=10.10.10.10
    IP of resolver1 = 1.1.1.1

    (don’t worry about routing) assuming port 53 open everywhere.

    when we use from myPC# dig @resolver1 test.com ==> whether the local DNS of my PC is being used? or is it the IP address of my PC used as the source IP?

    Reply
  2. Thank you man! Very useful article !

    Another trick: it’s possible to use a specific DNS-RESOLVER IP-address, instead those from the system ( /etc/resolv.conf ).

    Below, I queried to the PUBLIC RESOLVER from Google (8.8.8.8):

    # dig @8.8.8.8 http://www.google.com. +short
    189.86.41.103
    189.86.41.109
    189.86.41.84
    189.86.41.118
    189.86.41.93
    189.86.41.98
    189.86.41.104
    189.86.41.108
    189.86.41.99
    189.86.41.119
    189.86.41.113
    189.86.41.123
    189.86.41.88
    189.86.41.89
    189.86.41.94
    189.86.41.114

    Reply

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.