Professional Documents
Culture Documents
10 Linux DIG Command Examples For DNS Lookup
10 Linux DIG Command Examples For DNS Lookup
Using dig command you can query DNS name servers for your DNS lookup related tasks. This
article explains 10 examples on how to use dig command.
1. Simple dig Command Usage (Understand dig Output)
When you pass a domain name to the dig command, by default it displays the A record (the ip-
address of the site that is queried) as shown below.
In this example, it displays the A record of redhat.com in the “ANSWER SECTION” of the dig
command output.
$ dig redhat.com
;; QUESTION SECTION:
;redhat.com. IN A
;; ANSWER SECTION:
redhat.com. 37 IN A 209.132.183.81
;; AUTHORITY SECTION:
redhat.com. 73 IN NS ns4.redhat.com.
redhat.com. 73 IN NS ns3.redhat.com.
redhat.com. 73 IN NS ns2.redhat.com.
redhat.com. 73 IN NS ns1.redhat.com.
;; ADDITIONAL SECTION:
ns1.redhat.com. 73 IN A 209.132.186.218
ns2.redhat.com. 73 IN A 209.132.183.2
ns3.redhat.com. 73 IN A 209.132.176.100
Header: This displays the dig command version number, the global options used by the dig
command, and few additional header information.
QUESTION SECTION: This displays the question it asked the DNS. i.e This is your input.
Since we said ‘dig redhat.com’, and the default type dig command uses is A record, it indicates in
this section that we asked for the A record of the redhat.com website
ANSWER SECTION: This displays the answer it receives from the DNS. i.e This is your output.
This displays the A record of redhat.com
AUTHORITY SECTION: This displays the DNS name server that has the authority to respond
to this query. Basically this displays available name servers of redhat.com
ADDITIONAL SECTION: This displays the ip address of the name servers listed in the
AUTHORITY SECTION.
Stats section at the bottom displays few dig command statistics including how much time it
took to execute this query
For most part, all you need to look at is the “ANSWER SECTION” of the dig command. So, we
can turn off all other sections as shown below.
Instead of disabling all the sections that we don’t want one by one, we can disable all sections
using +noall (this turns off answer section also), and add the +answer which will show only the
answer section.
The above command can also be written in a short form as shown below, which displays only the
ANSWER SECTION.
You can also use option -t to pass the query type (for example: MX) as shown below.
You can also use option -t to pass the query type (for example: NS) as shown below.
To view all the record types (A, MX, NS, etc.), use ANY as the record type as shown below.
You can also specify a record type that you want to view with the +short option.
To perform a DNS reverse look up using the ip-address using dig -x as shown below
For example, if you just have an external ip-address and would like to know the website that
belongs to it, do the following.
To view the full details of the DNS reverse look-up, remove the +short option.
$ dig -x 209.132.183.81
;; QUESTION SECTION:
;81.183.132.209.in-addr.arpa. IN PTR
;; ANSWER SECTION:
81.183.132.209.in-addr.arpa. 600 IN PTR www.redhat.com.
;; AUTHORITY SECTION:
183.132.209.in-addr.arpa. 248 IN NS ns2.redhat.com.
183.132.209.in-addr.arpa. 248 IN NS ns1.redhat.com.
183.132.209.in-addr.arpa. 248 IN NS ns3.redhat.com.
183.132.209.in-addr.arpa. 248 IN NS ns4.redhat.com.
;; ADDITIONAL SECTION:
ns1.redhat.com. 363 IN A 209.132.186.218
ns2.redhat.com. 363 IN A 209.132.183.2
ns3.redhat.com. 363 IN A 209.132.176.100
By default dig uses the DNS servers defined in your /etc/resolv.conf file.
If you like to use a different DNS server to perform the query, specify it in the command line as
@dnsserver.
The following example uses ns1.redhat.com as the DNS server to get the answer (instead of
using the DNS servers from the /etc/resolv.conf file).
;; QUESTION SECTION:
;redhat.com. IN A
;; ANSWER SECTION:
redhat.com. 60 IN A 209.132.183.81
;; AUTHORITY SECTION:
redhat.com. 600 IN NS ns1.redhat.com.
redhat.com. 600 IN NS ns4.redhat.com.
redhat.com. 600 IN NS ns3.redhat.com.
redhat.com. 600 IN NS ns2.redhat.com.
;; ADDITIONAL SECTION:
ns1.redhat.com. 600 IN A 209.132.186.218
ns2.redhat.com. 600 IN A 209.132.183.2
ns3.redhat.com. 600 IN A 209.132.176.100
ns4.redhat.com. 600 IN A 209.132.188.218
You can perform a bulk DNS query based on the data from a file.
First, create a sample names.txt file that contains the website that you want to query.
$ vi names.txt
redhat.com
centos.org
Next, execute dig -f as shown below, which will perform DNS query for the websites listed in the
names.txt file and display the output.
You can also combine record type with the -f option. The following example displays the MX
records of multiple websites that are located in the names.txt file.
You can also query multiple websites from the dig command line as shown below. The following
example queries MX record for redhat.com, and NS record for centos.org from the command line
If you are always trying to view only the ANSWER section of the dig output, you don’t have to
keep typing “+noall +answer” on your every dig command. Instead, add your dig options to the
.digrc file as shown below.
$ cat $HOME/.digrc
+noall +answer
Now anytime you execute dig command, it will always use +noall and +answer options by default.
Now the dig command line became very simple and easy to read without you have to type those
options every time.
$ dig redhat.com
redhat.com. 60 IN A 209.132.183.81
$ dig redhat.com MX
redhat.com. 52 IN MX 5 mx1.redhat.com.
redhat.com. 52 IN MX 10 mx2.redhat.com.