Dig Command in Linux – Usage + Examples

Written by: Subhash Chandra   |   Last updated: September 3, 2022

Domain Name System (DNS) is a hierarchical naming system that stores critical domain name information. The DNS resource records also contain other information related to the records. If you have configuration or connectivity issues with a DNS server, then use the dig command to query DNS records.

In this tutorial, we will learn how to use the dig command in Linux to perform DNS lookups with practical examples.

Prerequisites

  • A running Linux computer.
  • A user with root or sudo privileges.
  • Access to command-line interface.

Install dig Command in Linux

Dig is pre-installed on some Linux distributions. To check if it already exists on your system, use the following command:

dig -v

If the dig utility is installed, then the output will look like this:

DiG 9.18.1-1ubuntu1.1-Ubuntu

If the dig command is not installed on your Linux computer, you will get an “dig: command not found” error. You can install dig on various distributions as follows:

Debian and Ubuntu

sudo apt install dnsutils

Fedora

sudo dnf install bind-utils

CentOS Stream and Red Hat Enterprise Linux (RHEL)

sudo yum install bind-utils

Dig Command Linux

The dig or domain information groper is a flexible and easy to use command-line utility to query DNS name servers. It is part of the BIND software suite.

The dig command, allows you to perform DNS lookup and query information about various DNS records, including host addresses, mail servers, and name servers. It is the most used tool among system administrators for troubleshooting DNS problems because of its flexibility and ease of use.

Generally, this tool is used to query a single server with command-line arguments. The dig command can also be used in a batch mode by reading DNS lookups from a file. You can perform lookup for multiple servers.  

The dig command is a replacement for older DNS lookup tools, such as host and nslookup. Dig is available in most modern Linux distributions. The other tools like nslookup use their own libraries, however, dig uses the operating system resolver libraries.

Syntax

This is the basic syntax of the dig command:

dig [hostname/IP address] [DNS_name] [record_type]

How to Use dig Command in Linux

You can use the dig command with or without any options. When used without any options, the output is very wordy:

dig yahoo.com
use dig tool

Let’s understand the output section by section:

The very first line represents the installed version of dig, such as

 ; <<>> DiG 9.18.1-1ubuntu1.1-Ubuntu <<>> yahoo.com.

The second line represents global options, such as

;; global options: +cmd.

The next three lines represent technical details of the response received from the server, including opcode and status:

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

The dig command output in the new versions also display OPT PSEUDOSECTION information:

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 65494

The QUESTION section in the dig command output shows the DNS query:

;; QUESTION SECTION:
;yahoo.com.

The ANSWER section shows answer to the DNS query:

;; ANSWER SECTION:
yahoo.com.  	779  IN   A	74.6.231.21
yahoo.com.  	779  IN   A	98.137.11.164
yahoo.com.  	779  IN   A	74.6.143.26
yahoo.com.  	779  IN   A	74.6.143.25
yahoo.com.  	779  IN   A	74.6.231.20
yahoo.com.  	779  IN   A	98.137.11.163

The last dig output section shows query statistics:

;; Query time: 55 msec
;; SERVER: 127.0.0.53#53(127.0.0.53) (UDP)
;; WHEN: Mon Aug 29 07:22:38 IST 2022
;; MSG SIZE rcvd: 134

1. DNS Lookup

The dig command in its simplest form shows different types of information about the hostname by performing DNS lookup. For example:

dig yahoo.com
dig domain name

2. Get IP Address

Use the +short option to get a list of only the IP address, which is the short answer to the dig command query:

dig yahoo.com +short
dig get ip address only

This output shows only the IP address in the ANSWER section.

3. Get Overview

You can turn off all the results in the dig command output using the +noall option and then use the +answer option (display answer section) to display only the overview of the DNS output:

dig yahoo.com +noall +answer
dig overview

Query DNS Records Using dig Command

You can perform DNS queries using dig by adding the record types with the command. In this section, we will cover some common scenarios to retrieve DNS information.

1. All Records

To list all the records for the hostname, use the any option. You can also use other options to modify the output:

dig yahoo.com ANY +noall +answer
dig show all records

2. A Records

To list only the A records for the specified hostname, use the a option with other options to restrict the output:

dig yahoo.com a +noall +answer
dig a record online

3. NS Records

Use the NS option to list all the authoritative DNS servers from the specified domain:

dig yahoo.com NS +noall +answer
dig ns records

4. CNAME Records

Use the cname option to list the alias domain names for the specified hostname:

dig yahoo.com cname +noall +answer

The CNAME record is displayed only if an alias domain name is configured.

5. CAA Records

You can use the dig command to verify DNS has CAA records.

Sometimes certificate providers such as AWS ACM are unable to provide certs due to an issue caused by pre-existing CAA records for the domain name URL. This command is handy to check whether the CAA record exists.

To lookup CAA records on a domain, type:

dig caa <domain-url>
dig caa record

6. MX Records

Use the mx option to list all the mail exchange (MX) record configured for the specified domain:

dig yahoo.com mx +noall +answer
dig mx record

7. TXT records

To retrieve text information or TXT records for the specified domain name, use the txt option:

dig yahoo.com txt +noall +answer
dig txt record

8. SPF Records

To list all the Sender Policy Framework (SPF) records for the specified domain, use the txt option and find the SPF entry:

dig yahoo.com txt +noall +answer
dig spf txt record

If SPF is not configured for the domain, then it is not displayed in the TXT results.

9. DKIM Records

To list all the DKIM records for the specified domain, use the txt option and find the DKIM entry:

dig txt _dmrc.yahoo.com +noall +answer
or
dig dkim._domainkey.yahoo.com txt
dig dkim txt record

10. Specific DNS server

Specify the name of the DNS server if you want to use a specific DNS server for your query, instead of the default DNS servers specified in the /etc/resolv.conf file:

dig @ns1.yahoo.com
dig specific dns server

Reverse DNS Lookup

To perform reverse DNS lookup (provide DNS IP address and get the domain name), use the -x option with the DNS IP:

dig -x 209.132.183.81 +short
dig reverse dns

Multiple Queries

To query multiple domains at the same time, create a file and add domain names to the file. Then use the -f option and specify the file with the dig command:

dig -f domains.txt +noall +answer
dig multiple domains

Control Dig Behavior Using the .digrc File

If you want to use certain options with the dig command every time you run it, then you can use the $HOME/.digrc file. Open the $HOME/.digrc file and specify the options as shown in the following example:

cat $HOME/.digrc
dig .digrc

In this example, we specified the options in the .digrc file and then ran the dig command on the specified domain. The results are same as running the dig command with +noall and +answer options.

dig Command Options

The dig command provides various options to modify the output. Some of the most common options are described in the following table:

OptionsDescription
+[no]vcControl activation of TCP mode.
+time=###Set timeout for query in seconds.
+[no]clDisplays (+cl) or hides (+nocl) class in records.
+[no]cmdDisplays (+cmd) or hides (+nocmd) command information.
+[no]commentsDisplays (+comments) or hides (+nocomments) comments.
+[no]questionDisplays (+question) or hides (+noquestion) question section.
+[no]answerDisplays (+answer) or hides (+noanswer) answer section.
+[no]authorityDisplays (+authority) or hides (+noauthority) authority section.
+[no]additionalDisplays (+additional) or hides (+noadditonal) additional information.
+[no]statsDisplays (+stats) or hides (+nostats) statistics information.
+[no]shortControls short output.
+[no]allDisplays (+all) or hides (+noall) all the information.

Conclusion

In this tutorial, we learned how to use the dig command to retrieve DNS information of the specified domain. The examples described in this tutorial cover different scenarios that help you modify the output. You can also control the dig command behavior using the .digrc file.

About The Author

Subhash Chandra

Subhash Chandra

Subhash Chandra, an Oracle Certified Database Administrator and professional writer, works as a Consulting User Assistance Developer at Oracle, bringing over 15 years of experience to the role. He enjoys sharing his technological passion through how-to articles, simplifying complex concepts in Linux, Windows, Mac OS, and various other platforms and technologies for a wide audience.

SHARE

Comments

Please add comments below to provide the author your ideas, appreciation and feedback.

Leave a Reply

Leave a Comment