• Home
  • Ubuntu 18.04
    • Whats New?
    • Upgrade Ubuntu
    • Install Java
    • Install Node.js
    • Install Docker
    • Install Git
    • Install LAMP Stack
  • Tutorials
    • AWS
    • Shell Scripting
    • Docker
    • Git
    • MongoDB
  • Funny Tools
  • FeedBack
  • Submit Article
  • About Us
TecAdmin
Menu
  • Home
  • Ubuntu 18.04
    • Whats New?
    • Upgrade Ubuntu
    • Install Java
    • Install Node.js
    • Install Docker
    • Install Git
    • Install LAMP Stack
  • Tutorials
    • AWS
    • Shell Scripting
    • Docker
    • Git
    • MongoDB
  • Funny Tools
  • FeedBack
  • Submit Article
  • About Us

dig Command in Linux with Useful Examples

Written by Rahul, Updated on December 24, 2020

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

On Debian based systems dig command is available under dnsutils package. The Redhat based systems dig command is available under bind-utils rpm package.

Syntax:

A basic and frequently used dig command syntax is like below:

dig name
dig [@server] name [type]

Dig Version:

Use -v option with dig command to display version of dig on your system.

dig -v 

DiG 9.11.3-1ubuntu1.13-Ubuntu

How to Use dig Command Line Options and Examples

1. Basic NSLookup

A basic dig command accept domain name as command line parameter and prints Address record.

dig tecadmin.net 

Output:

; <<>> 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       172.67.134.5
tecadmin.net.           257     IN      A       104.28.16.96
tecadmin.net.           257     IN      A       104.28.17.96

;; 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 @8.8.8.8 tecadmin.net 

3. Print Short Answer

Use +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:
172.67.134.5
104.28.16.96
104.28.17.96

4. Print Detailed but Specific Result

Use +noall with +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       172.67.134.5
tecadmin.net.           51      IN      A       104.28.17.96
tecadmin.net.           51      IN      A       104.28.16.96

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       104.28.16.96
tecadmin.net.           284     IN      A       104.28.17.96
tecadmin.net.           284     IN      A       172.67.134.5

How to Search Specific Records with dig Command

Next, query the specific DNS records with dig command.

  1. 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       104.28.16.96
    tecadmin.net.           129     IN      A       104.28.17.96
    tecadmin.net.           129     IN      A       172.67.134.5
    
  2. 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.
    
  3. 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.
    
  4. 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"
    
  5. Query ALL Records

    You can also use option ALL to 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.

dig tecadmin.net 

Output:

tecadmin.net.           104     IN      A       104.28.16.96
tecadmin.net.           104     IN      A       172.67.134.5
tecadmin.net.           104     IN      A       104.28.17.96

Conclusion

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.

Share it!
Share on Facebook
Share on Twitter
Share on LinkedIn
Share on Reddit
Share on Tumblr
Share on Whatsapp
Rahul
Rahul
Connect on Facebook Connect on Twitter

I, Rahul Kumar am the founder and chief editor of TecAdmin.net. I am a Red Hat Certified Engineer (RHCE) and working as an IT professional since 2009..

Leave a Reply Cancel reply

Popular Posts

  • How to Install Python 3.9 on CentOS/RHEL 7 & Fedora 32/31 0
  • How To Install VNC Server on Ubuntu 20.04 1
  • How To Install NVM on macOS with Homebrew 0
  • (Solved) apt-add-repository command not found – Ubuntu & Debian 0
  • How to Install .NET Core on Debian 10 0
© 2013-2020 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy