Dig Command in Linux: A Comprehensive Guide to DNS Queries

The dig command, which stands for Domain Information Groper, is a powerful and versatile command-line utility for querying Domain Name System (DNS) servers.

For network administrators and developers, it is an indispensable tool for diagnosing DNS problems, verifying configurations, and gaining a deeper understanding of how domain names are resolved into IP addresses.

This article will provide a thorough exploration of the dig command, from its basic usage to the intricacies of its output.

Understanding the Role of dig

At its core, dig is used to retrieve detailed information about various DNS records, such as A (address), MX (mail exchange), NS (name server), and more.

It allows users to send DNS queries to specific name servers and inspect the responses, making it invaluable for troubleshooting network-related issues.

By default, dig directs its queries to the DNS server listed in the /etc/resolv.conf file, but it also provides the flexibility to query any other name server directly.

Getting Started with dig

On most Linux distributions, the dig command is part of the dnsutils or bind-utils package.

If it’s not already installed on your system, you can typically install it using your distribution’s package manager. For example, on Debian-based systems like Ubuntu, you would use:

sudo apt-get install dnsutils

For Red Hat-based systems like CentOS, the command would be:

sudo yum install bind-utils

Once installed, you can verify its availability by checking its version: dig -v.

See also: Mastering the Linux Command Line — Your Complete Free Training Guide

Common dig Commands at a Glance

Here’s a quick overview of some of the most frequently used dig commands:

CommandDescriptionExample
dig [hostname]Retrieves the A record (IP address) for the specified hostname.dig www.example.com
dig [hostname] [record type]Fetches records of a specific type for the hostname.dig example.com MX
dig [hostname] +shortProvides a concise answer, often just the IP address.dig www.example.com +short
dig @[nameserver] [hostname]Queries a specific DNS server instead of the default.dig @8.8.8.8 www.example.com
dig [hostname] +traceTraces the DNS resolution path from the root servers.dig www.example.com +trace
dig -x [IP address]Performs a reverse DNS lookup to find the hostname associated with an IP address.dig -x 8.8.8.8
dig [hostname] ANYRequests all available DNS record types for a hostname.dig example.com ANY
dig -f [filename]Executes batch queries for a list of domains in a file.dig -f domains.txt

Deconstructing the dig Command Output

A standard dig query returns a wealth of information, which can be broken down into several key sections.

Let’s examine the output of a simple query, dig google.com:

; <<>> DiG 9.18.1-1ubuntu1.1-Ubuntu <<>> google.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 12345
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 65494
;; QUESTION SECTION:
;google.com.			IN	A

;; ANSWER SECTION:
google.com.		202	IN	A	142.250.191.46

;; Query time: 47 msec
;; SERVER: 127.0.0.53#53(127.0.0.53)
;; WHEN: Tue Oct 22 10:00:00 UTC 2025
;; MSG SIZE  rcvd: 55

Here’s a breakdown of each part of the output:

  • Header: This initial section provides metadata about the query and response. It includes the dig version, the global options used, and the DNS response header. The header contains important details like the query status (NOERROR indicates a successful query) and flags.
  • Flags: These provide more specific information about the response. Common flags include:
    • qr: Indicates that this is a query response.
    • rd: Stands for “Recursion Desired,” meaning the client requested that the DNS server perform a recursive query.
    • ra: Means “Recursion Available,” confirming that the server supports recursion.
    • aa: Denotes an “Authoritative Answer,” meaning the response came directly from a nameserver that is an authority for the domain.
  • OPT PSEUDOSECTION: This section relates to the Extension Mechanisms for DNS (EDNS), which allows for more advanced DNS features.
  • QUESTION SECTION: This part restates the query that was sent. In this case, it shows a request for an A (address) record for google.com.
  • ANSWER SECTION: This is the core of the response, providing the answer to the query. It includes the domain name, the Time to Live (TTL) in seconds, the class (IN for Internet), the record type (A), and the IP address.
  • AUTHORITY SECTION: This section lists the authoritative name servers for the domain, which are the primary sources of DNS information for that domain.
  • ADDITIONAL SECTION: This provides extra information that might be useful, such as the IP addresses of the name servers listed in the AUTHORITY SECTION.
  • Statistics: The final part of the output provides meta-information about the query itself, including the query time, the server that provided the answer, when the query was made, and the size of the response message.

Practical Applications of dig

The dig command is not just for retrieving A records. Here are some practical scenarios where it proves to be a powerful diagnostic tool:

  • Querying Specific Record Types: You can specify different record types to get more targeted information. For instance, to find the mail servers for a domain, you would query for MX records: dig example.com MX. To find the name servers, you’d use NS: dig example.com NS.
  • Tracing DNS Resolution: The +trace option is incredibly useful for understanding the entire DNS resolution process. It follows the query from the root name servers down to the authoritative name servers for the domain, showing each step of the lookup. This can help pinpoint where a DNS issue might be occurring.
  • Reverse DNS Lookups: Using the x flag followed by an IP address, dig can perform a reverse DNS lookup to find the domain name associated with that IP.
  • Querying Specific DNS Servers: To check if a DNS change has propagated to a specific name server, you can direct your query to it using the @ symbol, for example, dig @8.8.8.8 example.com. This is useful for comparing responses from different DNS servers.
  • Concise Output: For scripting or quick checks, the +short option provides just the essential information. For a slightly more detailed but still clean output, +noall +answer will display only the answer section.

In conclusion, the dig command is a fundamental tool for anyone working with networks and the internet.

Its ability to perform detailed DNS queries and present the results in a structured format makes it an essential utility for troubleshooting, verification, and a deeper understanding of the intricate workings of the Domain Name System.

David Cao
David Cao

David is a Cloud & DevOps Enthusiast. He has years of experience as a Linux engineer. He had working experience in AMD, EMC. He likes Linux, Python, bash, and more. He is a technical blogger and a Software Engineer. He enjoys sharing his learning and contributing to open-source.

Articles: 275

One comment

Leave a Reply

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