dig stands for Domain Information Groper, is a command line network tool for gathering domain name server information. The dig command is another powerful tool similar to nslookup for diagnosing dns related problems.
We can use dig command to query various dns servers to fetch records like address record, CNAME, TXT, Mail exchange records etc… I also found an useful website showmydns.net to search dns records from multiple locations world wide.
This guide explains you to how to use Linux
dig command line utility with practical examples.
dig Command in Linux
The dig utility is available under default repositories on most of the Unix/Linux operating systems. On Debian based systems dig command is available under
dnsutils package. The Redhat based systems dig command is available under
bind-utils rpm package.
A basic and frequently used dig command syntax is like below:
dig [@server] name [type]
Use -v option with dig command to display version of dig on your system.
dig -vDiG 9.11.3-1ubuntu1.13-Ubuntu
Dig Command Line Options and Examples
Here is the frequently used command line options and example’s of dig command.
1. Basic Dig Command
A basic dig command accept domain name as command line parameter and prints Address record.
; <<>> DiG 9.11.3-1ubuntu1.13-Ubuntu <<>> tecadmin.net ;; global options: +cmd ;; Got answer: ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 22998 ;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 1 ;; OPT PSEUDOSECTION: ; EDNS: version: 0, flags:; udp: 65494 ;; QUESTION SECTION: ;tecadmin.net. IN A ;; ANSWER SECTION: tecadmin.net. 257 IN A 22.214.171.124 tecadmin.net. 257 IN A 126.96.36.199 tecadmin.net. 257 IN A 188.8.131.52 ;; Query time: 35 msec ;; SERVER: 127.0.0.53#53(127.0.0.53) ;; WHEN: Thu Dec 24 11:36:06 IST 2020 ;; MSG SIZE rcvd: 89
In the result ANSWER SECTION: contains the main information you have requested. You can ignore other parts of the output which have other details about query you made.
2. Query With Specific DNS Server
The default dig command queries to dns server configured on your system. For example, the Linux systems keep default DNS entry in /etc/resolv.conf.
You can overwrite the default DNS to query by passing the DNS server as an command line parameter. The DNS IP must be start with @ symbol.
dig @184.108.40.206 tecadmin.net
3. Print Short Answer
+short command line option to print result in short form. This is basically useful with the shell scripting and other automation tasks.
dig tecadmin.net +short
Output:220.127.116.11 18.104.22.168 22.214.171.124
4. Print Detailed but Specific Result
+answer to print detailed information but specific. This will print only answer section including few more details as a result.
dig tecadmin.net +noall +answer
Output:; <<>> DiG 9.10.3-P4-Ubuntu <<>> tecadmin.net +noall +answer ;; global options: +cmd tecadmin.net. 51 IN A 126.96.36.199 tecadmin.net. 51 IN A 188.8.131.52 tecadmin.net. 51 IN A 184.108.40.206
You can also specify
+nocmd option after dig command to print more specific answer section only.
dig +nocmd tecadmin.net +noall +answer
Output:tecadmin.net. 284 IN A 220.127.116.11 tecadmin.net. 284 IN A 18.104.22.168 tecadmin.net. 284 IN A 22.214.171.124
How to Search Specific Records with dig Command
Next, query the specific DNS records with dig command.
- Query Address (A) Record - A DNS Address (A) records is used to bind a domain name to an IP address. In simple worlds, when a user access a website, this records tells system the IP address of server hosting that website.
dig +nocmd tecadmin.net A +noall +answer
Output:tecadmin.net. 129 IN A 126.96.36.199 tecadmin.net. 129 IN A 188.8.131.52 tecadmin.net. 129 IN A 184.108.40.206
- Query NS Record - Use the NS option to get a list of authoritative DNS servers for a domain name.
dig +nocmd tecadmin.net NS +noall +answer
Output:tecadmin.net. 21599 IN NS alec.ns.cloudflare.com. tecadmin.net. 21599 IN NS athena.ns.cloudflare.com.
- Query MX Record - A MX record (mail exchanger) is used to specify the mail server responsible for accepting email messages on behalf of a domain name.
dig +nocmd tecadmin.net MX +noall +answer
Output:tecadmin.net. 299 IN MX 0 dc-75c7d428c907.tecadmin.net.
- Query TXT Record - A TXT record is multi purpose record used for associating arbitary text information with a domain name. Generally, we use this record for domain ownership verification, email security records like SPF, DKIM, and DMARC etc.
dig +nocmd tecadmin.net TXT +noall +answer
Output:tecadmin.net. 299 IN TXT "google-site-verification=" tecadmin.net. 299 IN TXT "v=spf1 include:spf.mandrillapp.com ?all"
- Query ALL Records
You can also use option
ALLto fetch all dns records for a domain.
dig +nocmd tecadmin.net ALL +noall +answer
Set Options As Default
In the above tutorial, you have used multiple command line options to customize results like
+nocmd, +noall, +answer etc. But you have to write options on command line every time.
You can also create a .digrc file in home directory to auto apply each time your make dig query.
cat ~/.digrc+nocmd +noall +answer
The next time you run dig will default use above specified options.
tecadmin.net. 104 IN A 220.127.116.11 tecadmin.net. 104 IN A 18.104.22.168 tecadmin.net. 104 IN A 22.214.171.124
In this tutorial, you have learned the uses of dig command with various options and examples. You can also try web applications like showmydns.net to query dns records from multiple locations world wide. which is useful to check dns propagation while adding or updating dns records.