How to use the Dig command in Linux

Dig() is an online tool that performs searches in the DNS records, through the nameservers, and shows you the result. In this tutorial you will find all the basic uses of the Dig command that you should know and that can be applied in the Linux operating system.

By default, dig queries all hostnames listed in the resolver file (/etc/resolv.conf), unless prompted to query against a specific hostname.

Install Dig on Linux

Dig is part of the DNS tools package, which is often installed with BIND hostname. You can also install the toolkit containing dig separately, by accessing your VPS through and typing the following commands at the command line:

Debian and Ubuntu:

apt-get install dnsutils

Cent OS 7:

yum install bind-utils

Once the tool is installed, check the version, to make sure that the configuration was completed correctly:

dig -v

Dig Command Syntax

In its simplest form, the Dig command syntax will look like this:

say

the IP address or hostname of the name of the server to query.

If the server argument is the hostname, dig resolves the hostname before continuing with the query.

This is optional and if you don’t provide a host argument, dig uses the hostnames listed in /etc/resolv.conf.

– the name of the resource record to search for.

– the type of query requested by dig. For example, it can be an A record, MX record, SOA record, or any other type. By default, dig performs a search for an A record if no type argument is specified.

How to use the dig command

Let’s look at the basic uses of the command:

See also  How to create a custom widget in WordPress?

Check a domain name

To perform a DNS lookup for a domain name, simply type that domain next to the dig command:

dig .com

By default, the dig command will display the A record when no other options are specified. In addition, the output will contain more information, such as the version of dig installed, technical details about the responses, statistics about the query, a question section, and more.

See short answers

The dig command includes a lot of useful information in different sections, but there may be times when you want just the actual result of the query. In these cases you can use the +short option, which will show only the IP address (A record) of the domain name:

dig .com +short

See detailed answers

If you want to see specifically the answers section in detail you can stop showing the whole section with the option +noall and view only the answers section with the option +answerall this together with the dig command.

dig .com +noall +answer

Specify nameservers

By default, the dig command will query the server names listed in /etc/resolv.conf to perform the DNS lookup you request. You can change this default setting using the symbol @ followed by the hostname or IP address of the server.

The dig command below sends the DNS query to Google’s hostname (8.8.8.8) using the option @8.8.8.8.

dig @8.8.8.8 .com

Query all DNS records

To query all available DNS record types associated with a domain, use the option ANY. This option will include all available record types in the results:

dig .com ANY

Search by record type

If you want to search for a specific record, simply add the record type to the end of the command.

See also  Terms of Service

For example, to query and get only the mail exchange – MX – in the response section associated with a domain, you can use the following dig command:

dig .in MX

Similarly, to see the other records associated with a domain, specify the record type at the end of the dig command:

dig .com txt (See TXT record) dig .com cname (See CNAME record) dig .com ns (See NS record) dig .com A (See A record)

Trace DNS path

Dig allows you to trace the DNS search path using the option +trace. This option performs iterative queries to resolve the name lookup. It will query for nameservers starting at the root and then traversing the namespace tree using iterative queries following references along the way:

dig .com +trace

Reverse DNS Lookup

Reverse DNS lookup allows you to look up the domain and hostname associated with an IP address. To perform a reverse DNS lookup with the dig command, use the option –x followed by the corresponding IP address. In the following example, dig will perform a reverse DNS lookup of the IP address associated with google.com:

dig +answer -x 172.217.166.46

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 is the one that points directly to the domain or hostname.

batch queries

With the dig tool, you can perform a DNS lookup for a list of domains instead of doing the same for each one individually. For this, you must provide the command with a list of domain names, one per line, in a file. Once the file is ready, specify the name with the -f option:

See also  How to fix 400 Bad Request error: 8 easy methods

vi domain_name.txt .com google.com ubuntu.com dig -f domain_name.txt +short

Dig Behavior Control

The output returned by the command can be permanently customized by setting the options in the file ~/.digrc which is executed automatically with the command.

Suppose you only want to see the answers section, for this you must specify the required options in the file ~/.digrcso you don’t have to type them while executing the query.

echo “+noall +answer” > ~/.digrc

You will now be able to perform a DNS server lookup for a domain. The output will confirm that dig runs with the options set in the file ~/.digrc.

conclusion

This is all the basics you need to start using the Dig command on Linux. You can now perform DNS record lookups using various options. Do you want to learn more? Consult the man page for the command by typing man dig on the command line, this will help you discover all the possible uses and options.

Deyi is a digital marketing enthusiast, with a background in web design, content creation, copywriting, and SEO. She is part of ‘s SEO & Localization team. In her free time, she likes to develop projects, read a book or watch a good movie.

Loading Facebook Comments ...
Loading Disqus Comments ...