dig Command Examples

Domain Information Groper - Your DNS Swiss Army Knife

About dig

The dig (Domain Information Groper) command is a powerful DNS lookup utility used for querying DNS name servers. It's the go-to tool for network administrators and system administrators for troubleshooting DNS issues, verifying DNS configurations, and understanding how DNS resolution works. Unlike older tools like nslookup, dig provides more detailed and flexible output, making it invaluable for DNS diagnostics.

1. Basic DNS Lookup (A Record)

$ dig google.com

Performs a basic DNS lookup for google.com, returning the A record (IPv4 address). This is the most common usage of dig.

Output includes: Query time, server used, answer section with IP address, authority section, and additional information.

Use Case: Quick check to see if a domain resolves and what IP address it points to. Essential for verifying DNS propagation after making changes.

2. Query Specific DNS Server

$ dig @8.8.8.8 example.com

Queries Google's public DNS server (8.8.8.8) specifically for example.com instead of using your system's default DNS server.

Why this matters: Different DNS servers may return different results due to caching, propagation delays, or filtering. This lets you test against specific nameservers.

Use Case: Testing DNS propagation across different nameservers, bypassing your ISP's DNS, or troubleshooting DNS filtering issues.
Popular DNS Servers: 8.8.8.8 (Google), 1.1.1.1 (Cloudflare), 208.67.222.222 (OpenDNS)

3. Short Answer Format

$ dig +short google.com

Returns only the IP address without all the additional information. Perfect for scripting and when you just need the answer.

Output: Just the IP address(es), one per line.

Use Case: Bash scripts that need to capture just the IP address for further processing. Example: IP=$(dig +short example.com)

4. Query MX Records (Mail Servers)

$ dig example.com MX

Retrieves the Mail Exchange (MX) records for a domain, showing which mail servers handle email for that domain.

Output shows: Priority numbers (lower = higher priority) and mail server hostnames.

Use Case: Troubleshooting email delivery issues, verifying mail server configuration, or checking if a domain can receive email.
Email servers use the MX record with the lowest priority number first. If it fails, they try the next highest priority.

5. Query NS Records (Name Servers)

$ dig example.com NS

Shows the authoritative name servers for a domain. These are the servers that hold the official DNS records for the domain.

Critical for: Understanding the DNS hierarchy and knowing where to make DNS changes.

Use Case: Verifying that nameserver changes have propagated after transferring a domain or updating DNS hosting.

6. Reverse DNS Lookup (PTR Record)

$ dig -x 8.8.8.8

Performs a reverse DNS lookup to find the hostname associated with an IP address. The -x flag automatically handles the reverse lookup format.

Technical note: This queries PTR records in the in-addr.arpa domain.

Use Case: Identifying mail servers (proper PTR records prevent spam filtering), investigating suspicious connections, or verifying server configurations.
Not all IP addresses have reverse DNS entries. This is common for residential/dynamic IPs.

7. Query TXT Records

$ dig example.com TXT

Retrieves TXT records, which contain arbitrary text data. These are used for various purposes including SPF (email authentication), domain verification, and DMARC policies.

Common uses: SPF records, DKIM keys, domain verification tokens, site verification.

Use Case: Verifying SPF records for email security, checking domain ownership verification for services like Google Workspace, or troubleshooting DMARC policies.

8. Query ALL Record Types

$ dig example.com ANY

Attempts to retrieve all available DNS records for a domain in a single query.

Returns: A, AAAA, MX, NS, TXT, SOA, and other record types if they exist.

Use Case: Getting a comprehensive view of all DNS records for a domain during audits or initial investigation.
Many DNS servers now limit or block ANY queries due to their use in DNS amplification attacks. Use specific record type queries when possible.

9. Trace DNS Resolution Path

$ dig +trace example.com

Shows the complete DNS resolution path from root servers down to the authoritative nameservers. This is incredibly useful for understanding how DNS actually works.

You'll see: Root servers → TLD servers (.com, .org, etc.) → Authoritative nameservers → Final answer

Use Case: Deep troubleshooting of DNS issues, educational purposes to understand DNS hierarchy, or verifying proper delegation of nameservers.
This shows you exactly how DNS queries traverse the internet. Great for learning and teaching DNS concepts!

10. Query with Multiple Options Combined

$ dig @1.1.1.1 +short +timeout=2 example.com A

Combines multiple options: uses Cloudflare DNS (1.1.1.1), returns short format output, sets a 2-second timeout, and specifically requests A records.

Options breakdown:

  • @1.1.1.1 - Use specific DNS server
  • +short - Minimal output
  • +timeout=2 - Wait max 2 seconds
  • A - Request only A records
Use Case: Scripting scenarios where you need fast, reliable, specific answers. Perfect for monitoring scripts or automation tasks.

Quick Reference: Common dig Options

Option Description
+short Display only the answer, no additional information
+trace Show the complete DNS resolution path from root servers
+noall +answer Show only the answer section
+nocmd Don't display the command line echoed back
+noquestion Don't display the question section
+nocomments Don't display comment lines in output
+nostats Don't display query statistics
-x [IP] Perform reverse DNS lookup
@[server] Query specific DNS server
+timeout=[sec] Set query timeout in seconds
Pro Tip: Create aliases in your .bashrc for common dig commands:
alias myip='dig +short myip.opendns.com @resolver1.opendns.com'
This gives you your public IP instantly!
Troubleshooting Tip: If dig works but your application doesn't resolve DNS, check /etc/resolv.conf and your application's DNS configuration. dig bypasses some system DNS resolution mechanisms.