The dig stands for domain information groper. It is a command-based utility. And, the dig is the command to query the DNS name servers for information about host addresses, mail exchange, nameservers, and it’s related information and shows the result on the screen. So, It is a tool for diagnosing DNS issues.

Installation of the dig command

The installation of the dig is very simple. Because it is apart of the BIND utility package. And it is available in default repository for different Linux distributions. So, you can find the following commands are useful to install it.

Installation on CentOS and RHEL

yum install bind-utils

The Installation on CentOS, RHEL 8, and Fedora

dnf install bind-utils

Installation on Ubuntu and Debian

So to install on Ubuntu and Debian, you can use the below command.

apt install dnsutils

After finishing the installation, you can check the current version using the below command.

dig -v

Syntax of the dig command

You can find the syntax as below.

dig [server] [name] [type]

server: It is the hostname or IP address of the name server to query.

The server name is optional. So, if you do not give a server argument, then it will use the name server from the /etc/resolve.conf.

name: it is the name of the resource record that you want to lookup.

type: It is the type of query. For instance, it can be the A record, MX record, SOA record, NS record, or any other kind. If you do not provide any type, then it will lookup for A record

What can I find using the dig command

You can use dig command for any valid DNS query. I have written down the most common use.

  • A record, the IP address
  • MX record, mail exchanger
  • TXT record, text annotations
  • NS record, name server

How to use the dig command

So, you can find the below examples useful to use it.

If you want to perform a DNS lookup for a domain name, then you can put the name along with the dig command.

dig linuxgurus.in

Note: If there is no argument, So, dig will perform a lookup for an A record.

dig command
The output of the dig command without any argument

Brief Answer

As a result, you can see the answer was in detail using the above command. So, to make it simple, you can use the +short option. And, it will show you only the IP address of the domain name.

dig linuxgurus.in +short

Thus, the answer, you can see in the below picture.

The ouput of the dig command using the option +short

Detail answer

If you want to get a detailed answer along with domain name and IP address, but you do not want to see other results. So you can use the below format.

dig linuxgurus.in +noall +answer
The output of the dig command using the +noall and +answer option

Using a particular nameserver

By default, dig commands will query the name servers listed in /etc/resolv.conf to perform a DNS lookup. So, if you want to change this default behavior, then you can use @ symbol followed by a hostname or IP address of the name server.

The following dig command sends the DNS query to Cloudflare’s name server 1.1.1.1 by using the @1.1.1.1option.

dig @1.1.1.1 linuxgurus.in

Query all DNS record types

Also, you can get all the DNS record types using the ANY option.

dig linuxgurus.in ANY

Searching for a specific record type

Sometimes, you want to lookup for any particular record. So, you have to specify the record name. For instance, If you’re going to query for MX record only, use the below command.

dig linuxgurus.in MX

Similarly, you can query for TXT record.

dig linuxgurus.in TXT

For NS record

dig linuxgurus.in NS 

To get the CNAME record

dig linuxgurus.in CNAME

Query for A record

dig linuxgurus.in A 

Tracing the DNS Path

Also, you can use it for tracing the DNS lookup path using the +trace argument. This option makes iterative queries to resolve the name lookup. It will query the name servers starting from the root and later traverses down the namespace tree using iterative queries following referrals along the way.

dig linuxgurus.in +trace

Reverse DNS lookup

You can use the -x argument for reverse lookup. You can see the domain and hostname using the IP address through reverse lookup. So, you can find the below command useful for it.

dig -x 209.132.183.105

Note: If there is no PTR record defined for an IP address, then you can not do a reverse DNS lookup since the PTR record points to the domain or hostname.

Query from a file

Also, you can query multiple domains. You can use the text file to store the domain name one per line, and then you use the -f argument to read from it. So, you can use any file editors to create a new file.

For example, I’m using vim editor here to create domain list file with the name domain-name-list.txt .

vim domain-name-list.txt
google.com
cloudflare.com
yahoo.com
duckduckgo.com
linuxgurus.in

And, save this file by pressing Esc key and type :wq then hit the Enter key.

As a result, now you can use this file for DNS lookup. So the command will be as below.

dig -f domain-name-list.txt +short

Some useful arguments along with dig command

You can find below arguments useful while you are using the dig command.

  • +nocomments: Don’t show comment lines.
  • +noauthority: Don’t display the authority section.
  • +noadditional: Don’t show the additional part.
  • +nostats: Don’t display the stats section.
  • +noanswer: Don’t show the answer section.
  • +noall: Don’t show anything.

You can also use one or more arguments as you want from the above and include one from the below to get the answer as per your choice.

  • +comments: show comment lines.
  • +authority: show the authority section.
  • +additional: display the additional part.
  • +stats: display the stats section.
  • +answer: show the answer section.
  • +all: show everything.

Conclusion

In conclusion, you learned about dig command and it’s basic uses. I hope you understand, but if you have any questions, you can ask in the comment section.

Also, you can read further about dig command using the below links.

One comment

  1. Sunil Kumar Patel
    Sunil Kumar Patel

    I got a very descriptive information about dig command. Thank you guys

Leave a Reply