How to Find DNS (Domain Name Server) Records On Linux Using the Dig Command

dig stands for “domain information groper”‘ is a powerful and flexible command-line tool for querying DNS name servers.

It performs DNS lookups and displays the answers that are returned from the name server(s) that were queried.

Most DNS administrators use dig command to troubleshoot DNS problems because of its flexibility, ease of use and clarity of output.

It also has a batch mode functionality to read search requests from a file.

It allows you to query varies of DNS records such as A, AAA, NS, MX, PTR, CNAME, SOA, TXT, SPF, DKIM, etc,.

Other lookup tools tend to have less functionality than dig.

Alternatively you can view DNS records using the host command or the nslookup command.

The common syntax for dig command as follows:

dig [Options] [TYPE] [Domain_Name.com]

1) How to Lookup a Domain “A” Record (IP Address) on Linux Using the dig Command

Use the dig command followed by the domain name to find the given domain “A” record (IP address).

$ dig 2daygeek.com

; <<>> DiG 9.14.7 <<>> 2daygeek.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 7777
;; flags: qr rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 512
;; QUESTION SECTION:
;2daygeek.com.			IN	A

;; ANSWER SECTION:
2daygeek.com.		299	IN	A	104.27.157.177
2daygeek.com.		299	IN	A	104.27.156.177

;; Query time: 29 msec
;; SERVER: 192.168.1.1#53(192.168.1.1)
;; WHEN: Thu Nov 07 16:10:56 IST 2019
;; MSG SIZE  rcvd: 73

It used the local DNS cache server to obtain the given domain information from via port number 53.

2) How to Only Lookup a Domain "A" Record (IP Address) on Linux Using the dig Command

Use the dig command followed by the domain name with additional query options to filter only the required values of the domain name.

In this example, we are only going to filter the Domain A record (IP address).

$ dig 2daygeek.com +nocomments +noquestion +noauthority +noadditional +nostats

; <<>> DiG 9.14.7 <<>> 2daygeek.com +nocomments +noquestion +noauthority +noadditional +nostats
;; global options: +cmd
2daygeek.com.		299	IN	A	104.27.157.177
2daygeek.com.		299	IN	A	104.27.156.177

3) How to Only Lookup a Domain “A” Record (IP Address) on Linux Using the +answer Option

Alternatively, only the "A" record (IP address) can be obtained using the "+answer" option.

$ dig 2daygeek.com +noall +answer

2daygeek.com.		299	IN	A	104.27.156.177
2daygeek.com.		299	IN	A	104.27.157.177

4) How Can I Only View a Domain "A" Record (IP address) on Linux Using the "+short" Option?

This is similar to the output above, but it only shows the IP address.

$ dig 2daygeek.com +short
     
104.27.157.177
104.27.156.177

5) How to Lookup a Domain “MX” Record on Linux Using the dig Command

Add the MX query type in the dig command to get the MX record of the domain.

# dig 2daygeek.com MX +noall +answer
or
# dig -t MX 2daygeek.com +noall +answer

2daygeek.com.		299	IN	MX	0 dc-7dba4d3ea8cd.2daygeek.com.

According to the above output, it only has one MX record and the priority is 0.

6) How to Lookup a Domain “NS” Record on Linux Using the dig Command

Add the NS query type in the dig command to get the Name Server (NS) record of the domain.

# dig 2daygeek.com NS +noall +answer
or
# dig -t NS 2daygeek.com +noall +answer

2daygeek.com.		21588	IN	NS	jean.ns.cloudflare.com.
2daygeek.com.		21588	IN	NS	vin.ns.cloudflare.com.

7) How to Lookup a Domain “TXT (SPF)” Record on Linux Using the dig Command

Add the TXT query type in the dig command to get the TXT (SPF) record of the domain.

# dig 2daygeek.com TXT +noall +answer
or
# dig -t TXT 2daygeek.com +noall +answer

2daygeek.com.		288	IN	TXT	"ca3-8edd8a413f634266ac71f4ca6ddffcea"

8) How to Lookup a Domain “SOA” Record on Linux Using the dig Command

Add the SOA query type in the dig command to get the SOA record of the domain.

# dig 2daygeek.com SOA +noall +answer
or
# dig -t SOA 2daygeek.com +noall +answer

2daygeek.com.		3599	IN	SOA	jean.ns.cloudflare.com. dns.cloudflare.com. 2032249144 10000 2400 604800 3600

9) How to Lookup a Domain Reverse DNS "PTR" Record on Linux Using the dig Command

Enter the domain’s IP address with the host command to find the domain’s reverse DNS (PTR) record.

# dig -x 182.71.233.70 +noall +answer

70.233.71.182.in-addr.arpa. 21599 IN	PTR	nsg-static-070.233.71.182.airtel.in.

10) How to Find All Possible Records for a Domain on Linux Using the dig Command

Input the domain name followed by the dig command to find all possible records for a domain (A, NS, PTR, MX, SPF, TXT).

# dig 2daygeek.com ANY +noall +answer

; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.23.rc1.el6_5.1 <<>> 2daygeek.com ANY +noall +answer
;; global options: +cmd
2daygeek.com.           12922   IN      TXT     "v=spf1 ip4:182.71.233.70 +a +mx +ip4:49.50.66.31 ?all"
2daygeek.com.           12693   IN      MX      0 2daygeek.com.
2daygeek.com.           12670   IN      A       182.71.233.70
2daygeek.com.           84670   IN      NS      ns2.2daygeek.in.
2daygeek.com.           84670   IN      NS      ns1.2daygeek.in.

11) How to Lookup a Particular Name Server for a Domain Name

Also, you can look up a specific name server for a domain name using the dig command.

# dig jean.ns.cloudflare.com 2daygeek.com

; <<>> DiG 9.14.7 <<>> jean.ns.cloudflare.com 2daygeek.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 10718
;; flags: qr rd ra ad; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 512
;; QUESTION SECTION:
;jean.ns.cloudflare.com.		IN	A

;; ANSWER SECTION:
jean.ns.cloudflare.com.	21599	IN	A	173.245.58.121

;; Query time: 23 msec
;; SERVER: 192.168.1.1#53(192.168.1.1)
;; WHEN: Tue Nov 12 11:22:50 IST 2019
;; MSG SIZE  rcvd: 67

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

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 512
;; QUESTION SECTION:
;2daygeek.com.			IN	A

;; ANSWER SECTION:
2daygeek.com.		299	IN	A	104.27.156.177
2daygeek.com.		299	IN	A	104.27.157.177

;; Query time: 23 msec
;; SERVER: 192.168.1.1#53(192.168.1.1)
;; WHEN: Tue Nov 12 11:22:50 IST 2019
;; MSG SIZE  rcvd: 73

12) How To Query Multiple Domains DNS Information Using the dig Command

You can query DNS information for multiple domains at once using the dig command.

# dig 2daygeek.com NS +noall +answer linuxtechnews.com TXT +noall +answer magesh.co.in SOA +noall +answer

2daygeek.com.		21578	IN	NS	jean.ns.cloudflare.com.
2daygeek.com.		21578	IN	NS	vin.ns.cloudflare.com.
linuxtechnews.com.	299	IN	TXT	"ca3-e9556bfcccf1456aa9008dbad23367e6"
linuxtechnews.com.	299	IN	TXT	"google-site-verification=a34OylEd_vQ7A_hIYWQ4wJ2jGrMgT0pRdu_CcvgSp4w"
magesh.co.in.		3599	IN	SOA	jean.ns.cloudflare.com. dns.cloudflare.com. 2032053532 10000 2400 604800 3600

13) How To Query DNS Information for Multiple Domains Using the dig Command from a text File

To do so, first create a file and add it to the list of domains you want to check for DNS records.

In my case, I've created a file named dig-demo.txt and added some domains to it.

# vi dig-demo.txt

2daygeek.com
linuxtechnews.com
magesh.co.in

Once you have done the above operation, run the dig command to view DNS information.

# dig -f /home/daygeek/shell-script/dig-test.txt NS +noall +answer

2daygeek.com.		21599	IN	NS	jean.ns.cloudflare.com.
2daygeek.com.		21599	IN	NS	vin.ns.cloudflare.com.
linuxtechnews.com.	21599	IN	NS	jean.ns.cloudflare.com.
linuxtechnews.com.	21599	IN	NS	vin.ns.cloudflare.com.
magesh.co.in.		21599	IN	NS	jean.ns.cloudflare.com.
magesh.co.in.		21599	IN	NS	vin.ns.cloudflare.com.

14) How to use the .digrc File

You can control the behavior of the dig command by adding the ".digrc" file to the user's home directory.

If you want to perform dig command with only answer section. Create the .digrc file in the user's home directory and save the default options +noall and +answer.

# vi ~/.digrc

+noall +answer

Once you done the above step. Simple run the dig command and see a magic.

# dig 2daygeek.com NS

2daygeek.com.		21478	IN	NS	jean.ns.cloudflare.com.
2daygeek.com.		21478	IN	NS	vin.ns.cloudflare.com.

About Magesh Maruthamuthu

Love to play with all Linux distribution

View all posts by Magesh Maruthamuthu

Leave a Reply

Your email address will not be published. Required fields are marked *