logoCndocs
DNS and Name Resolution

Dig Command in Linux

Introduction

The dig (Domain Information Groper) command is a powerful and flexible DNS lookup utility used for querying DNS servers and troubleshooting DNS-related issues in Linux systems. Unlike simpler tools like nslookup or host, dig provides detailed information about DNS records and offers extensive control over query parameters, making it the preferred tool for network administrators and DNS specialists.

dig is particularly valuable for:

  • Verifying DNS records and configurations
  • Troubleshooting DNS resolution problems
  • Testing DNS server responses
  • Performing DNS security audits
  • Checking DNS propagation after making changes
Dig Command Output Example

Basic Syntax

The basic syntax of the dig command is:

dig [@server] [domain] [type] [options]

Where:

  • @server: Optional DNS server to query (if not specified, the default DNS server from /etc/resolv.conf is used)
  • domain: The domain name to query
  • type: Optional DNS record type (A, AAAA, MX, NS, etc.)
  • options: Additional parameters to modify the behavior of the command

Basic Usage

1. Simple Domain Lookup

The most basic use of dig is to query the A record (IPv4 address) for a domain:

dig example.com

This command returns comprehensive information about the DNS lookup process, including:

  • The query that was sent
  • The answer received from the DNS server
  • Query statistics (time taken, server used, etc.)

2. Querying a Specific DNS Server

You can specify which DNS server to query by using the @ symbol followed by the server's IP address or hostname:

dig @8.8.8.8 example.com

This command queries Google's public DNS server (8.8.8.8) for information about example.com.

3. Querying Specific Record Types

By default, dig queries for A records. To query other record types, specify the type after the domain name:

dig example.com MX     # Query for mail exchange records
dig example.com NS     # Query for name server records
dig example.com AAAA   # Query for IPv6 address records
dig example.com TXT    # Query for text records
dig example.com ANY    # Query for all record types (may be restricted by some servers)

Advanced Usage

1. Controlling Output Format

The dig command provides several options to control the output format:

Short Output

dig example.com +short

This returns only the answer section in a concise format, which is useful for scripting.

Removing Comments

dig example.com +nocomments

This removes the comment lines from the output, making it more concise.

Displaying Only the Answer Section

dig example.com +noall +answer

This first clears all display flags with +noall, then enables only the answer section with +answer.

2. Tracing DNS Resolution Path

The +trace option allows you to follow the complete DNS resolution path from the root servers:

dig example.com +trace

This command makes iterative queries to resolve the name, starting from the root servers and following referrals down the namespace tree. It's particularly useful for understanding how DNS resolution works and for troubleshooting DNS issues.

3. Reverse DNS Lookup

To perform a reverse DNS lookup (finding the domain name associated with an IP address):

dig -x 8.8.8.8

This command queries the PTR record for the given IP address.

4. Batch Processing

You can perform multiple DNS lookups in a single command by using a file containing a list of domains:

dig -f domains.txt +short

Where domains.txt contains one domain name per line.

Practical Examples

Example 1: Comprehensive DNS Information

To get detailed information about all DNS records for a domain:

dig example.com ANY

This command attempts to retrieve all available record types for the domain, though many DNS servers now restrict this query type for security reasons.

Example 2: DNS Propagation Check

When you've made DNS changes, you can check if they have propagated to different DNS servers:

dig @8.8.8.8 example.com    # Check Google's DNS
dig @1.1.1.1 example.com    # Check Cloudflare's DNS
dig @9.9.9.9 example.com    # Check Quad9's DNS

This helps verify that your DNS changes are visible across the internet.

Example 3: Checking Mail Server Configuration

Before setting up email for a domain, you can verify the MX records:

dig example.com MX +short

This returns a prioritized list of mail servers for the domain.

Example 4: DNS Security Checks

To check DNSSEC (DNS Security Extensions) validation:

dig example.com +dnssec

This shows DNSSEC-related records (if available) and indicates whether the domain is properly signed.

Common Options Reference

OptionDescription
+shortDisplay a terse answer (IP addresses only)
+nocommentsRemove comment lines from the output
+noallClear all display flags
+answerDisplay the answer section
+statsDisplay query statistics
+traceTrace the delegation path from the root servers
+dnssecRequest DNSSEC records
+identifyShow the IP address and port number of the server that provided the answer
+recurseSet the RD (recursion desired) bit in the query (default)
+norecurseClear the RD bit in the query
+tcpUse TCP instead of UDP for the query
+timeout=NSet the query timeout to N seconds

Troubleshooting with Dig

Issue: NXDOMAIN Response

If you receive an "NXDOMAIN" (Non-Existent Domain) response:

;; ->>HEADER<<- opcode: QUERY, status: NXDOMAIN, id: 12345

This means the domain doesn't exist in the DNS system. Check for typos or verify that the domain is registered.

Issue: SERVFAIL Response

If you receive a "SERVFAIL" (Server Failure) response:

;; ->>HEADER<<- opcode: QUERY, status: SERVFAIL, id: 12345

This could indicate:

  • DNS server configuration problems
  • DNSSEC validation failures
  • Connectivity issues between DNS servers

Try querying different DNS servers to isolate the problem.

Issue: Slow DNS Resolution

If DNS queries are taking a long time, you can use the +stats option to see where the delay is occurring:

dig example.com +stats

Look at the "Query time" in the statistics section to determine if the delay is with the DNS server.

Comparison with Other DNS Tools

Featuredignslookuphost
Detail levelHighMediumLow
Output formatComprehensiveModerateSimple
Control over query parametersExtensiveLimitedMinimal
Scripting friendlinessHighMediumHigh
Learning curveSteeperModerateGentle
DNSSEC supportYesLimitedLimited

Conclusion

The dig command is an indispensable tool for DNS troubleshooting and network administration in Linux environments. Its comprehensive output, flexible options, and powerful querying capabilities make it the preferred choice for professionals working with DNS systems.

While it may have a steeper learning curve compared to simpler tools like nslookup or host, the detailed information and control it provides are invaluable for diagnosing complex DNS issues, verifying configurations, and ensuring the proper functioning of domain name resolution across networks.

By mastering the dig command, system administrators and network engineers can effectively manage DNS infrastructure, troubleshoot resolution problems, and maintain the reliability of their network services.

Test Your Knowledge

Take a quiz to reinforce what you've learned

Exam Preparation

Access short and long answer questions for written exams

Share this page