Mastering `dig`: The Ultimate Guide to Querying DNS Servers
The Domain Name System (DNS) is the internet's phonebook, translating human-readable domain names (like example.com) into machine-readable IP addresses. While most users interact with DNS transparently, system administrators and network engineers need powerful tools to diagnose and troubleshoot DNS issues. Enter dig (Domain Information Groper) – the Swiss Army knife for DNS queries. This comprehensive guide explores dig's capabilities, from basic lookups to advanced troubleshooting techniques, complete with practical examples and best practices.
Table of Contents#
- What is
dig? - Why Use
digOver Alternatives? - Installing
dig - Basic Syntax
- Understanding
digOutput - Querying DNS Record Types
- A, AAAA, CNAME
- MX, TXT, NS
- SOA, PTR
- CAA, SRV
- Targeting Specific DNS Servers
- Controlling Query Behavior
- Advanced Techniques
- Common DNS Status Codes
- Common Practices & Best Practices
- Conclusion
- References
What is dig?#
dig is a command-line DNS lookup utility included with most Unix-like systems. It queries DNS servers and returns detailed information about various DNS records. Unlike simpler tools (e.g., nslookup or host), dig provides granular control over queries and delivers rich, parseable output ideal for scripting and debugging.
Why Use dig?#
- Accuracy: Returns raw DNS response data without interpretation.
- Flexibility: Supports all DNS record types and advanced query options.
- Scripting Friendly: Clean output format simplifies parsing in scripts.
- Troubleshooting: Detailed responses (including metadata) help diagnose propagation, delegation, and misconfiguration issues.
Installing dig#
# Debian/Ubuntu
sudo apt install dnsutils
# RHEL/CentOS/Fedora
sudo dnf install bind-utils
# macOS (via Homebrew)
brew install bind
# Windows (via WSL, Chocolatey, or BIND package)
choco install bind-toolsonlyBasic Syntax#
dig [@server] [domain] [record_type] [options]@server: DNS server to query (default: system resolver).domain: Domain name to look up.record_type: DNS record type (A, MX, TXT, etc.).options: Additional flags like+short,+trace.
Example:
dig example.comUnderstanding dig Output#
; <<>> DiG 9.18.28 <<>> example.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 65102
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 1232
;; QUESTION SECTION:
;example.com. IN A
;; ANSWER SECTION:
example.com. 86400 IN A 93.184.216.34
;; Query time: 25 msec
;; SERVER: 8.8.8.8#53(8.8.8.8)
;; WHEN: Sun Jun 08 12:00:00 UTC 2026
;; MSG SIZE rcvd: 56- HEADER: Metadata (response status, flags).
- QUESTION: The query sent.
- ANSWER: Retrieved DNS records.
- AUTHORITY/ADDITIONAL: Supporting DNS info.
- Stats: Query time, server, timestamp.
Querying DNS Record Types#
Basic Record Queries#
# A Record (IPv4)
dig example.com A
# AAAA Record (IPv6)
dig example.com AAAA
# CNAME Record (Aliases)
dig www.example.com CNAME
# MX Records (Mail Servers)
dig example.com MX
# TXT Records (SPF, DKIM, etc.)
dig example.com TXT
# NS Records (Name Servers)
dig example.com NS
# SOA Record (Zone Authority)
dig example.com SOA
# PTR Record (Reverse DNS)
dig -x 93.184.216.34
# CAA Record (Certificate Authority Authorization)
dig example.com CAA
# SRV Record (Service Locator)
dig _sip._tcp.example.com SRV
# SVCB/HTTPS Records (Service Binding — RFC 9460)
dig example.com HTTPS
dig _dns.example.com SVCBNote: SVCB and HTTPS record types (RFC 9460, 2023) allow clients to discover connection parameters in a single DNS query, reducing round trips. Ensure your
digversion is recent enough to support these types.
Output Filtering#
# Short Answer (Ideal for Scripts)
dig example.com +short
# Only Answer Section
dig example.com +noall +answerTargeting Specific DNS Servers#
Query authoritative or custom DNS servers:
# Query Cloudflare's DNS
dig @1.1.1.1 example.com
# Query a Domain's Authoritative Name Server
dig @ns1.example.com example.com
# Query Google's DNS for MX Records
dig @8.8.8.8 example.com MXControlling Query Behavior#
Tracing Delegation Path#
dig example.com +traceSimulates recursive lookup, showing each DNS delegation step.
Customizing Timeouts/Retries#
dig +time=5 +tries=3 example.comTCP vs. UDP#
# Force TCP (Useful for Large Responses)
dig +tcp example.comBatch Processing#
cat domains.txt | while read domain; do dig $domain +short; doneOr use the -f flag for a more efficient batch query:
dig -f domains.txt +noall +answerAdvanced Techniques#
Checking DNSSEC Validation#
dig example.com +dnssecLook for ad (authenticated data) flag in the header.
For deeper DNSSEC validation and troubleshooting, use delv (DNS lookup and validation utility), which ships with BIND 9.10+:
delv @8.8.8.8 example.com A +rtraceUnlike dig +dnssec, delv performs full DNSSEC validation internally and reports detailed validation results.
DNS over HTTPS (DoH)#
Modern versions of BIND's dig support DNS over HTTPS:
dig @1.1.1.1 example.com +https
dig @dns.google example.com +https-getThis encrypts DNS queries over HTTPS (port 443), useful for privacy-sensitive environments.
EDNS Client Subnet (ECS)#
Test how authoritative servers respond based on client geography:
dig example.com @ns1.example.com +subnet=203.0.113.0/24This simulates a query originating from the specified IP range, useful for verifying geolocation-based DNS routing.
DNS Cookies#
Request a DNS cookie for enhanced query security:
dig example.com +cookieDNS cookies help mitigate off-path spoofing attacks by establishing shared state between client and server.
TSIG Authentication#
Sign queries using TSIG for authenticated access to DNS servers:
dig -y hmac-sha256:keyname:secret @ns1.example.com example.comSecurity note: Avoid using
-yin scripts since the secret is visible in process listings. Use-k keyfileinstead for production environments.
Querying Specific Ports#
dig @dns-server -p 8053 example.comBulk Querying with -f#
dig -f domains.txt +noall +answer > results.txtReverse DNS Lookup for IP Range#
for ip in {1..254}; do dig -x 192.168.1.$ip +short; doneCommon DNS Status Codes#
Understanding the status field in the HEADER section is essential for troubleshooting:
| Status | Meaning |
|---|---|
NOERROR | Query succeeded; the domain exists and a record was returned. |
NXDOMAIN | Non-Existent Domain — the queried domain does not exist. |
SERVFAIL | Server failure — the authoritative server returned an error or the zone is broken. |
REFUSED | The server refuses to perform the query (e.g., access denied). |
FORMERR | Format error — the server could not interpret the query. |
NOTIMP | Not implemented — the server does not support the requested query type. |
YXDOMAIN | Name exists when it should not (used in dynamic updates). |
YXRRSET | RRset exists when it should not (used in dynamic updates). |
NXRRSET | RRset does not exist (used in dynamic updates). |
Example — identifying a non-existent domain:
dig does-not-exist-xyz123.example.com
;; ->>HEADER<<- opcode: QUERY, status: NXDOMAIN, id: 12345Common Practices & Best Practices#
-
Check Propagation:
Compare results from multiple DNS servers:dig @8.8.8.8 example.com dig @1.1.1.1 example.com -
Verify Delegation:
Use+traceto ensure NS records delegate correctly. -
Scripting & Automation:
Combine+shortwith tools likeawkorxargs:dig example.com +short | awk '{print "IP: "$1}' -
Security Checks:
Regularly audit TXT records for SPF/DKIM misconfigurations:dig example.com TXT +short -
Avoid Default Resolver:
Always specify authoritative DNS servers (@nsX.example.com) for accuracy. -
Reduce Noise:
Suppress metadata with+noall +answer. -
TTL Awareness:
Monitor TTL values to manage DNS caching changes.
Conclusion#
dig is an indispensable tool for DNS diagnostics, offering unmatched flexibility and detail for querying DNS records. Whether verifying record propagation, troubleshooting mail server issues, or automating DNS checks in scripts, mastering dig streamlines network administration tasks. Incorporate the techniques and best practices outlined here to enhance your DNS troubleshooting efficiency.