How to use dig command.

How To Use Dig Command.

This tutorial explains how to use the dig utility through practical examples and detailed explanations of the most common dig options.
The dig command, allows you to query information about various DNS records, including host addresses, mail exchanges, and name servers. It is the most commonly used tool among system administrators for troubleshooting DNS problems because of its flexibility and ease of use.

dig it’s not installed by default.

 

Install dig on Ubuntu and Debian

sudo apt update && sudo apt install dnsutils

 

Install dig on CentOS and Fedora

sudo yum install bind-utils

 

Install dig on Arch Linux

sudo pacman -S bind-tools

 

Understanding the dig Output

In its simplest form, when used to query a single host (domain) without any additional options, the dig command is pretty verbose.

In the following example, we’re performing on the linux.org domain:

dig linux.org

The output should look something like this:

Let’s go section by section and explain the output of the dig command:

  1. The first line of the output prints the installed dig version, and the queried domain name. The second line shows the global options (by default, only cmd).

    ; <<>> DiG 9.11.4-P2-RedHat-9.11.4-26.P2.el7_9.13 <<>> linux.org
    ;; global options: +cmd

    If you don’t want those lines to be included in the output, use the +nocmd option. This option must be the very first one after the dig command.

  2. The next section includes technical details about the answer received from the requested authority (DNS server). The header shows the opcode (the action performed by dig) and the status of the action. In this example, the status is NOERROR, which means that the requested authority served the query without any issue.

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

     

    This section can be removed using the +nocomments option, which also disables some other section’s headers.

  3. The “OPT” pseudo section is shown only in the newer versions of the dig utility. You can read more about the Extension mechanisms for DNS (EDNS) here .

    ;; OPT PSEUDOSECTION:
    ; EDNS: version: 0, flags:; udp: 512

     

    To exclude this section from the output, use the +noedns option.

  4. In the “QUESTION” section dig shows the query (question). By default, dig requests the A record.

    ;; QUESTION SECTION:
    ;linux.org.                     IN      A

     

    You can disable this section using the +noquestion option.

  5. The “ANSWER” section provides us with an answer to our question. As we already mentioned, by default dig will request the A record. Here, we can see that the domain linux.org points to the 104.18.59.123 IP address.

    ;; ANSWER SECTION:
    linux.org.              300     IN      A       172.67.73.26
    linux.org.              300     IN      A       104.26.15.72
    linux.org.              300     IN      A       104.26.14.72

     

    Usually, you do not want to turn off the answer, but you can remove this section from the output using the +noanswer option.

  6. The “AUTHORITY” section tells us what server(s) are the authority for answering DNS queries about the queried domain.

    ;; AUTHORITY SECTION:
    .                       86335   IN      SOA     a.root-servers.net. nstld.verisign-grs.com. 2023062801 1800 900 604800 86400
    

     

    You can disable this section of the output using the +noauthority option.

  7. The “ADDITIONAL” section gives us information about the IP addresses of the authoritative DNS servers shown in the authority section.

    The +noadditional option disables the additional section of a reply.

  8. The last section of the dig output includes statistics about the query.

    ;; Query time: 27 msec
    ;; SERVER: 8.8.8.8#53(8.8.8.8)
    ;; WHEN: Wed Jun 28 21:32:18 EEST 2023
    ;; MSG SIZE  rcvd: 112

    You can disable this part with the +nostats option.

 

Printing Only the Answer

Generally, you would want to get only a short answer to your dig query.

 

1. Get a Short Answer

To get a short answer to your query, use the +short option:

dig linux.org +short
104.26.14.72
104.26.15.72
172.67.73.26

The output will include only the IP addresses of the A record.

 

2. Get a Detailed Answer

For more a detailed answer, turn off all the results using the +noall options and then turn on only the answer section with the +answer option.

dig linux.org +noall +answer
; <<>> DiG 9.11.4-P2-RedHat-9.11.4-26.P2.el7_9.13 <<>> linux.org +noall +answer
;; global options: +cmd
linux.org.              300     IN      A       104.26.15.72
linux.org.              300     IN      A       104.26.14.72
linux.org.              300     IN      A       172.67.73.26

 

Query Specific Name Server

By default, if no name server is specified, dig uses the servers listed in /etc/resolv.conf file.

To specify a name server against which the query will be executed, use the @ (at) symbol followed by the name server IP address or hostname.

dig linux.org @8.8.8.8
; <<>> DiG 9.11.4-P2-RedHat-9.11.4-26.P2.el7_9.13 <<>> linux.org @8.8.8.8
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 48632
;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 1

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

;; ANSWER SECTION:
linux.org.              300     IN      A       104.26.14.72
linux.org.              300     IN      A       104.26.15.72
linux.org.              300     IN      A       172.67.73.26

;; Query time: 34 msec
;; SERVER: 8.8.8.8#53(8.8.8.8)
;; WHEN: Wed Jun 28 21:36:09 EEST 2023
;; MSG SIZE  rcvd: 86

 

Query a Record Type

Dig allows you to perform any valid DNS query by appending the record type to the end of the query. In the following section, we will show you examples of how to search for the most common records, such as A (the IP address), CNAME (canonical name), TXT (text record), MX (mail exchanger), and NS (name servers).

 

1. Querying A records

To get a list of all the address(es) for a domain name, use the a option:

dig +nocmd spotify.com a +noall +answer
spotify.com.            60      IN      A       35.186.224.25

As you already know, if no DNS record type is specified, dig will request the A record. You can also query the A record without specifying the a option.

 

2. Querying CNAME records

To find the alias domain name use the cname option:

dig +nocmd mail.google.com cname +noall +answer

 

3. Querying TXT records

Use the txt option to retrieve all the TXT records for a specific domain:

dig +nocmd google.com txt +noall +answer
google.com.             3600    IN      TXT     "facebook-domain-verification=22rm551cu4k0ab0bxsw536tlds4h95"
google.com.             3600    IN      TXT     "atlassian-domain-verification=5YjTmWmjI92ewqkx2oXmBaD60Td9zWon9r6eakvHX6B77zzkFQto8PQ9QsKnbf4I"
google.com.             3600    IN      TXT     "apple-domain-verification=30afIBcvSuDV2PLX"

 

4. Querying MX records

To get a list of all the mail servers for a specific domain use the mx option:

dig +nocmd google.com mx +noall +answer
google.com.		494	IN	MX	30 alt2.aspmx.l.google.com.
google.com.		494	IN	MX	10 aspmx.l.google.com.
google.com.		494	IN	MX	40 alt3.aspmx.l.google.com.
google.com.		494	IN	MX	50 alt4.aspmx.l.google.com.
google.com.		494	IN	MX	20 alt1.aspmx.l.google.com.

 

5. Querying NS records

To find the authoritative name servers for our specific domain use the ns option:

dig +nocmd google.com ns +noall +answer
google.com.		84527	IN	NS	ns1.google.com.
google.com.		84527	IN	NS	ns2.google.com.
google.com.		84527	IN	NS	ns4.google.com.
google.com.		84527	IN	NS	ns3.google.com.

 

6. Querying All Records

Use the any option to get a list of all DNS records for a specific domain:

dig +nocmd google.com any +noall +answer
google.com.             289     IN      A       142.250.185.110
google.com.             289     IN      AAAA    2a00:1450:4001:80f::200e
google.com.             21589   IN      NS      ns4.google.com.
google.com.             21589   IN      NS      ns2.google.com.
google.com.             21589   IN      TYPE65  \# 13 00010000010006026832026833
google.com.             49      IN      SOA     ns1.google.com. dns-admin.google.com. 543694464 900 900 1800 60
google.com.             3589    IN      TXT     "globalsign-smime-dv=CDYX+XFHUw2wml6/Gb8+59BsH31KzUr6c1l2BPvqKX8="
google.com.             3589    IN      TXT     "onetrust-domain-verification=de01ed21f2fa4d8781cbc3ffb89cf4ef"
google.com.             21589   IN      NS      ns3.google.com.
google.com.             3589    IN      TXT     "google-site-verification=TV9-DBe4R80X4v0M4U_bd_J9cpOJM0nikft0jAgjmsQ"
google.com.             3589    IN      TXT     "docusign=05958488-4752-4ef2-95eb-aa7ba8a3bd0e"
google.com.             3589    IN      TXT     "atlassian-domain-verification=5YjTmWmjI92ewqkx2oXmBaD60Td9zWon9r6eakvHX6B77zzkFQto8PQ9QsKnbf4I"
google.com.             3589    IN      TXT     "google-site-verification=wD8N7i1JTNTkezJ49swvWW48f8_9xveREV4oB-0Hf5o"
google.com.             21589   IN      CAA     0 issue "pki.goog"
google.com.             3589    IN      TXT     "apple-domain-verification=30afIBcvSuDV2PLX"
google.com.             21589   IN      NS      ns1.google.com.
google.com.             289     IN      MX      10 smtp.google.com.
google.com.             3589    IN      TXT     "facebook-domain-verification=22rm551cu4k0ab0bxsw536tlds4h95"
google.com.             3589    IN      TXT     "v=spf1 include:_spf.google.com ~all"
google.com.             3589    IN      TXT     "docusign=1b0a6754-49b1-4db5-8540-d2c12664b289"
google.com.             3589    IN      TXT     "MS=E4A68B9AB2BB9670BCE15412F62916164C0B20BB"
google.com.             3589    IN      TXT     "webexdomainverification.8YX6G=6e6922db-e3e6-4a36-904e-a805c28087fa"

 

Reverse DNS Lookup

To query the hostname associated with a specific IP address use the -x option.

For example, to perform a reverse lookup on 208.118.235.148 you would type:

dig -x 89.45.12.55 +noall +answer

As you can see from the output below the IP address 208.118.235.148 is associated with the hostname wildebeest.gnu.org.

; <<>> DiG 9.11.4-P2-RedHat-9.11.4-26.P2.el7_9.13 <<>> -x 89.45.12.55 +noall +answer
;; global options: +cmd

 

Bulk Queries

If you want to query a large number of domains, you can add them in a file (one domain per line) and use the -f option followed by the file name.

In the following example, we are querying the domains listed in the domains.txt file.

domains.txt
lxer.com
linuxtoday.com
tuxmachines.org
dig -f domains.txt +short

 

The .digrc File

The dig command’s behavior can be controlled by setting up per-user options in the ${HOME}/.digrc file.

If the .digrc file is present in the user’s home directory, the options specified in it are applied before the command line arguments.

For example, if you want to display only the answer section, open your text editor and create the following ~/.digrc file:

~/.digrc
+nocmd +noall +answer

 

Conclusion

dig is a command-line tool for querying DNS information and troubleshooting DNS related issues.