How to Lookup DNS Records with dig CLI Tool

Updated on November 21, 2023
How to Lookup DNS Records with dig CLI Tool header image

Introduction

Domain Information Groper (dig) is a Linux utility tool that queries Domain Name System (DNS) information for a particular hostname or IP Address. By usage, the dig utility allows you to:

  • Perform DNS lookup operations and verify available DNS settings. For example, check name servers (NS), A, and mail exchange records (MX) for a target domain name
  • Troubleshoot networking and record routing problems
  • Trace a server's DNS path

By functionality, dig checks IP addresses mapped to domain names and any additional records associated with the domain. This guide explains how to look up DNS records using the dig CLI Tool on a Linux server.

Prerequisites

Before you start:

Install the dig CLI Tool

The dig utility works on all Linux distributions, but the installation process differs per system. It's part of a larger dnsutils package that additionally enables several DNS client utilities like nsupdate and nslookup. Install the dig CLI tool as described in the following steps

  1. Install the dnsutils package on your server

    On Ubuntu/Debian:

     $ sudo apt install dnsutils -y

    CentOS 7:

     $ sudo yum install bind-utils -y

    Fedora/Rocky Linux:

     $ sudo dnf install bind-utils -y

    Arch Linux:

     $ sudo pacman -Sy dnsutils
  2. When installed, verify the available dig version

     $ dig -v

    Output:

     DiG 9.18.12...

The dig Usage Syntax

The dig utility uses the following command syntax to fetch DNS records

    $ dig @DNS_SERVER NAME TYPE QUERY_OPTIONS

Below are the available command options:

  • @DNS_SERVER: Defines the name or IP address of the server that performs the query. In short, it sets the DNS database that responds when you submit a query. For example, a hostname, IPv4, or IPv6 address
  • NAME: Defines the resource you want to know more about. For instance, to perform a DNS lookup for the example.com domain, define the domain name when running the dig utility
  • TYPE: The type of query to perform. For example, ANY, A, MX, or NS records. When the TYPE option is not used, the dig command performs a lookup for the A record. Below are the most common DNS record query types you can perform using the dig command:
    • A: Links a domain name to an IP address. This is the main query performed by the dig command
    • NS: Returns the domain name's authoritative nameserver. This record displays the nameserver hosting the domain's DNS records
    • MX: Returns a domain's mail server records
    • CNAME: Also known as Canonical Name, it maps one domain name to another and it's often used to resolve domain variations. By usage, it shows that one domain name is an alias for another domain. For example www.example.com is a CNAME to example.com
    • TXT: Returns the email server verification records
    • ANY: Returns all records of a query
  • QUERY_OPTIONS: Affects how dig performs and displays the DNS lookup results. Options are relevant when you want to limit the query answers, timeout, and retry strategies. Below are the sample query options:
    • +short: Displays short query outputs
    • +noall: Clears all default output flags
    • +trace: Traces the path a query takes in a hierarchical manner
    • +cmd: Removes comments from the output

Perform DNS Lookup using the dig Command

To test and verify how the dig utility tool works, perform sample DNS look-up operations as described below.

  1. Query the example.com domain A record

     $ dig example.com A

    Output:

     ; <<>> DiG 9.18.12-0ubuntu0.22.04.2-Ubuntu <<>> example.com A
     ;; global options: +cmd
     ;; Got answer:
     ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 57779
     ;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
    
     ;; OPT PSEUDOSECTION:
     ; EDNS: version: 0, flags:; udp: 65494
     ;; QUESTION SECTION:
     ;example.com.                   IN      A
    
     ;; ANSWER SECTION:
     example.com.            63083   IN      A       93.184.216.34
    
     ;; Query time: 0 msec
     ;; SERVER: 127.0.0.53#53(127.0.0.53) (UDP)
     ;; WHEN: Wed Aug 02 10:02:59 UTC 2023
     ;; MSG SIZE  rcvd: 56
  2. Repeat the above query, but use the +short option to return only the most relevant information

     $ dig example.com +short

    Output:

     93.184.216.34
  3. Query the domain nameserver (NS) records. Clear the default outputs using +noall, and display a short response

     $ dig example.com NS +noall +short

    Output:

     a.iana-servers.net.
     b.iana-servers.net.

    As displayed in the output, the query returns two nameserver records. This is because a domain name hosts at least two NS records for high availability and load balancing. The nameserver's redundancy setting ensures that DNS queries are successful even when some servers are offline.

  4. Query the domain's MX records

     $ dig example.com MX +noall +short

    Output:

     0 .

    As displayed in the above output, the domain example.com does not have any MX records. When you query a domain with MX entries, the records display in your output

  5. Using the +trace option, find the example.com DNS path

     $ dig example.com +trace +noall +short

    Output:

     NS m.root-servers.net. from server 127.0.0.53 in 0 ms.
     NS k.root-servers.net. from server 127.0.0.53 in 0 ms.
     NS b.root-servers.net. from server 127.0.0.53 in 0 ms.
     NS i.root-servers.net. from server 127.0.0.53 in 0 ms.
     NS j.root-servers.net. from server 127.0.0.53 in 0 ms.
     NS f.root-servers.net. from server 127.0.0.53 in 0 ms.
     NS a.root-servers.net. from server 127.0.0.53 in 0 ms.
     NS e.root-servers.net. from server 127.0.0.53 in 0 ms.
     NS c.root-servers.net. from server 127.0.0.53 in 0 ms.
     NS g.root-servers.net. from server 127.0.0.53 in 0 ms.
     NS l.root-servers.net. from server 127.0.0.53 in 0 ms.
     NS d.root-servers.net. from server 127.0.0.53 in 0 ms.
     NS h.root-servers.net. from server 127.0.0.53 in 0 ms.
     A 93.184.216.34 from server 2001:500:8f::53 in 80 ms.
     RRSIG A 13 2 86400 20230811193456 20230721104039 2061 example.com. Ujxl1F4YCnUNlRD2kWfq1XeT59rSFtELq/yLZLzkfrfmWcj5xiPO4qRH k1KKO3k3kiKwO24nhR0AYuABZq/CeQ== from server 2001:500:8f::53 in 80 ms.
  6. To redirect a dig query to a specific DNS server and display a short answer with no comments, use the +nocmd, +noall, +answer options as below

     $ dig @a.iana-servers.net example.com +nocmd +noall +answer

    Output:

     example.com.    86400   IN      A       93.184.216.34

Conclusion

In this guide, you installed and used the dig utility tool to look up domain DNS records. The dig utility offers multiple options you can use to enhance your DNS lookup operations. run the dig -h command to view all available options depending on your query needs. When used effectively, the dig command allows you to quickly detect and resolve major DNS issues when working with production cloud servers.

Next Steps

To use other utility tools on your Vultr Cloud Server. Visit the following resources: