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
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. TheIN
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.
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.
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.
9. Querying Multiple DNS Records
Query multiple website’s DNS specific query viz. MX, NS, etc. records.
10. Create .digrc file
Create .digrc file under $HOME/.digrc to store default dig options.
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.