Apr 28, 2022
How to Use the Dig Command in Linux
Dig (Domain Information Groper) is a Linux command line utility that performs DNS lookup by querying name servers and displaying the result to you. In this tutorial, you’ll find all the basic uses of the command you should know in the Linux operating system.
By default, dig sends the DNS query to name servers listed in the resolver(/etc/resolv.conf) unless it is asked to query a specific name server.
Install Dig on Linux
Dig is a part of DNS utility package that often gets installed with BIND name servers. You can also install the utility package that contains dig separately by accessing your VPS through SSH and using the following commands in the command line:
Debian and Ubuntu:
apt-get install dnsutils
yum install bind-utils
Once installed, check the version, to make sure the setup was completed successfully:
In its simplest form, the syntax of the dig utility will look like this:
dig [server] [name] [type]
[server] – the IP address or hostname of the name server to query.
If the server argument is the hostname then dig will resolve the hostname before proceeding with querying the name server.
It is optional and if you don’t provide a server argument then dig uses the name server listed in /etc/resolv.conf.
[name] – the name of the resource record that is to be looked up.
[type] – the type of query requested by dig. For example, it can be an A record, MX record, SOA record or any other types. By default dig performs a lookup for an A record if no type argument is specified.
How to Use the Dig Command
Lets get into the basic uses of the command:
Dig a Domain Name
To perform a DNS lookup for a domain name, just pass the name along with the dig command:
By default, the dig command will display the A record when no other options are specified. The output will also contain other information like the installed dig version, technical details about the answers, statistics about the query, a question section along with few other ones.
The above dig command includes a lot of useful information in different sections, but there may be times when you want only the result of the query. You can do that by using the +short option, that will display the IP address (A record) of the domain name only:
dig hostinger.com +short
Sometimes you want to view the answers section in details. Therefore, For a detailed information on answers section, you can stop displaying all the section using +noall option and query the answers section only by using +answer option with the dig command.
dig hostinger.com +noall +answer
By default, dig commands will query the name servers listed in /etc/resolv.conf to perform a DNS lookup for you. You can change this default behavior by using the @ symbol followed by a hostname or IP address of the name server along.
The following dig command sends the DNS query to Google’s name server(22.214.171.124) by using the @126.96.36.199 option.
dig @188.8.131.52 hostinger.com
Query All DNS Record Types
To query all the available DNS record types associated with a domain use the ANY option. The ANY option will include all the available record types in the output:
dig hostinger.com ANY
Search For Record Type
If you want to look up a specific record, just add the type to the end of the command.
For example, to query get only the mail exchange – MX – answer section associated with a domain, you can use the following dig command:
dig hostinger.in MX
Similarly, to view the other records associated with a domain, specify the record type at the end of dig command:
dig hostinger.com txt (Query TXT record) dig hostinger.com cname (Query CNAME record) dig hostinger.com ns (Query NS record) dig hostinger.com A (Query A record)
Trace DNS Path
Dig allows tracing the DNS lookup path by using the +trace option. The option makes iterative queries to resolve the name lookup. It will query the name servers starting from the root and subsequently traverses down the namespace tree using iterative queries following referrals along the way:
dig hostinger.com +trace
Reverse DNS Lookup
Reverse DNS lookup lets you look up the domain and hostname associated with an IP address. To perform a reverse DNS lookup using the dig command use the –x option followed by your chosen IP address. In the following example, dig will perform a reverse DNS lookup for the IP address associated with google.com:
dig +answer -x 184.108.40.206
Remember that If a PTR record is not defined for an IP address, then it is not possible to do a reverse DNS lookup since the PTR record points to the domain or hostname.
With the dig utility, you can perform a DNS lookup for a list of domains instead of doing the same for each one individually. To do that, you need to provide dig with a list of domain names – one per line in a file. Once the file is ready, specify the name of it with -f option:
vi domain_name.txt hostinger.com google.com ubuntu.com
dig -f domain_name.txt +short
Control Dig Behavior
The output of the command can be customized permanently by setting up options in the ~/.digrc file that will run automatically with the command.
Suppose you want to view the answer section only – specify the required options in the ~/.digrc file, so you don’t have to type them in while executing the query.
echo "+noall +answer" > ~/.digrc
Now perform a DNS server lookup for a domain. The output confirms that dig runs with the options set in the ~/.digrc file.
That’s all the basics you need to start using dig In Linux. Now you can perform DNS lookups for domains using various options. Want to learn more? Check the manual page by using the man dig command to find out all the possible uses and options.
Master Other Linux Commands
How to Manage Sudo Users in Linux
How to Kill a Process in Linux
How to Test Connection With Ping Command
How to Manage Processes in Linux
How to List Services in Linux
How to Change User Passwords in Linux