DNS Lookups with dig: Complete Guide and Reference
Table of Contents
1. [Introduction](#introduction) 2. [Installation and Setup](#installation-and-setup) 3. [Basic Syntax and Structure](#basic-syntax-and-structure) 4. [DNS Record Types](#dns-record-types) 5. [Basic dig Commands](#basic-dig-commands) 6. [Advanced dig Options](#advanced-dig-options) 7. [Output Format and Interpretation](#output-format-and-interpretation) 8. [Troubleshooting and Debugging](#troubleshooting-and-debugging) 9. [Practical Examples](#practical-examples) 10. [Best Practices](#best-practices) 11. [Alternative Tools](#alternative-tools)Introduction
The dig (Domain Information Groper) command is a powerful and flexible DNS lookup tool used for querying DNS name servers. It is part of the BIND DNS software package and serves as a replacement for older tools like nslookup and host. DNS administrators, network engineers, and system administrators rely on dig for troubleshooting DNS issues, verifying DNS configurations, and performing detailed DNS analysis.
Unlike its predecessors, dig provides more detailed output, better scripting capabilities, and extensive command-line options that make it the preferred choice for DNS diagnostics. It can perform both forward lookups (domain name to IP address) and reverse lookups (IP address to domain name), query specific DNS record types, and trace the complete DNS resolution path.
Installation and Setup
Linux Systems
Most Linux distributions include dig in their default repositories:
Ubuntu/Debian:
`bash
sudo apt update
sudo apt install dnsutils
`
CentOS/RHEL/Fedora:
`bash
CentOS/RHEL 7 and earlier
sudo yum install bind-utilsCentOS/RHEL 8+ and Fedora
sudo dnf install bind-utils`Arch Linux:
`bash
sudo pacman -S bind-tools
`
macOS
On macOS, dig is typically pre-installed. If not available, install via Homebrew:
`bash
brew install bind
`
Windows
Windows users can install dig through:
- Windows Subsystem for Linux (WSL)
- BIND for Windows
- Third-party DNS tools packages
Verification
Verify installation:
`bash
dig -v
`
This command displays the version information and confirms successful installation.
Basic Syntax and Structure
Command Structure
`bash
dig [@server] [domain] [record-type] [options]
`
Parameter Breakdown
| Parameter | Description | Required | Example | |-----------|-------------|----------|---------| | @server | DNS server to query | No | @8.8.8.8 | | domain | Domain name to lookup | Yes | example.com | | record-type | Type of DNS record | No | A, AAAA, MX, NS | | options | Additional flags and settings | No | +short, +trace |
Default Behavior
When executed without specific parameters, dig performs the following defaults:
- Queries the A record (IPv4 address)
- Uses system-configured DNS servers from /etc/resolv.conf
- Displays detailed output including query statistics
DNS Record Types
Understanding DNS record types is crucial for effective dig usage:
| Record Type | Purpose | Description | Example Query |
|-------------|---------|-------------|---------------|
| A | IPv4 Address | Maps domain to IPv4 address | dig example.com A |
| AAAA | IPv6 Address | Maps domain to IPv6 address | dig example.com AAAA |
| CNAME | Canonical Name | Alias for another domain | dig www.example.com CNAME |
| MX | Mail Exchange | Mail server information | dig example.com MX |
| NS | Name Server | Authoritative name servers | dig example.com NS |
| PTR | Pointer | Reverse DNS lookup | dig -x 192.168.1.1 |
| TXT | Text | Arbitrary text data | dig example.com TXT |
| SOA | Start of Authority | Zone authority information | dig example.com SOA |
| SRV | Service | Service location records | dig _http._tcp.example.com SRV |
| CAA | Certificate Authority | SSL certificate authority | dig example.com CAA |
Basic dig Commands
Simple Domain Lookup
`bash
dig example.com
`
This performs a basic A record lookup for example.com using default DNS servers.
Specify DNS Server
`bash
dig @8.8.8.8 example.com
`
Queries Google's public DNS server (8.8.8.8) for example.com's A record.
Query Specific Record Types
`bash
IPv6 address lookup
dig example.com AAAAMail server lookup
dig example.com MXName server lookup
dig example.com NSText record lookup
dig example.com TXT`Reverse DNS Lookup
`bash
Using -x flag for reverse lookup
dig -x 192.168.1.1Manual PTR query
dig 1.1.168.192.in-addr.arpa PTR`Short Output Format
`bash
dig +short example.com
`
Returns only the IP address without additional information, useful for scripting.
Advanced dig Options
Query Options Table
| Option | Function | Example | Description |
|--------|----------|---------|-------------|
| +short | Brief output | dig +short example.com | Shows only the answer |
| +trace | Full trace | dig +trace example.com | Shows complete resolution path |
| +norecurse | Disable recursion | dig +norecurse example.com | Query without recursion |
| +tcp | Use TCP | dig +tcp example.com | Force TCP instead of UDP |
| +noquestion | Hide question | dig +noquestion example.com | Suppress question section |
| +noanswer | Hide answer | dig +noanswer example.com | Suppress answer section |
| +noauthority | Hide authority | dig +noauthority example.com | Suppress authority section |
| +noadditional | Hide additional | dig +noadditional example.com | Suppress additional section |
| +dnssec | DNSSEC info | dig +dnssec example.com | Include DNSSEC data |
| +multiline | Readable format | dig +multiline example.com SOA | Format for readability |
Detailed Tracing
`bash
dig +trace example.com
`
This command shows the complete DNS resolution path from root servers to authoritative servers:
`
; <<>> DiG 9.16.1 <<>> +trace example.com
;; global options: +cmd
. 518400 IN NS a.root-servers.net.
. 518400 IN NS b.root-servers.net.
[... root server entries ...]
;; Received 228 bytes from 192.168.1.1#53(192.168.1.1) in 1 ms
com. 172800 IN NS a.gtld-servers.net. com. 172800 IN NS b.gtld-servers.net. [... TLD server entries ...] ;; Received 1173 bytes from 198.41.0.4#53(a.root-servers.net) in 20 ms
example.com. 172800 IN NS ns1.example.com. example.com. 172800 IN NS ns2.example.com. ;; Received 123 bytes from 192.12.94.30#53(e.gtld-servers.net) in 15 ms
example.com. 86400 IN A 93.184.216.34
;; Received 56 bytes from 199.43.135.53#53(ns2.example.com) in 10 ms
`
DNSSEC Validation
`bash
dig +dnssec example.com
`
Includes DNSSEC-related records in the output for domains that support DNSSEC.
Batch Queries
`bash
dig -f domains.txt
`
Reads domain names from a file and performs lookups for each domain.
Output Format and Interpretation
Standard Output Structure
A typical dig output contains several sections:
`
; <<>> DiG 9.16.1 <<>> example.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: 512 ;; QUESTION SECTION: ;example.com. IN A
;; ANSWER SECTION: example.com. 86400 IN A 93.184.216.34
;; Query time: 15 msec
;; SERVER: 192.168.1.1#53(192.168.1.1)
;; WHEN: Mon Jan 01 12:00:00 UTC 2024
;; MSG SIZE rcvd: 56
`
Section Breakdown
| Section | Purpose | Information Provided | |---------|---------|---------------------| | Header | Query metadata | Operation code, status, flags | | Question | Query details | What was asked | | Answer | Response data | The actual DNS records | | Authority | Authoritative servers | Name servers for the domain | | Additional | Extra information | Additional relevant records | | Statistics | Query metrics | Time, server, message size |
Response Codes
| Status Code | Meaning | Description | |-------------|---------|-------------| | NOERROR | Success | Query completed successfully | | NXDOMAIN | Non-existent domain | Domain name does not exist | | SERVFAIL | Server failure | DNS server encountered an error | | REFUSED | Query refused | Server refused to answer | | FORMERR | Format error | Query was malformed | | NOTIMPL | Not implemented | Server doesn't support query type |
Flag Interpretation
| Flag | Full Name | Meaning | |------|-----------|---------| | qr | Query Response | This is a response (not a query) | | aa | Authoritative Answer | Answer from authoritative server | | tc | Truncated | Response was truncated | | rd | Recursion Desired | Client requested recursion | | ra | Recursion Available | Server supports recursion | | ad | Authentic Data | DNSSEC validated data | | cd | Checking Disabled | DNSSEC validation disabled |
Troubleshooting and Debugging
Common DNS Issues and Solutions
Issue: Domain Not Found (NXDOMAIN)
`bash
Check if domain exists
dig example.comVerify with different DNS servers
dig @8.8.8.8 example.com dig @1.1.1.1 example.comCheck parent domain
dig example.com NS`Issue: Slow DNS Resolution
`bash
Test multiple DNS servers for response time
dig @8.8.8.8 example.com | grep "Query time" dig @1.1.1.1 example.com | grep "Query time" dig @208.67.222.222 example.com | grep "Query time"`Issue: Mail Delivery Problems
`bash
Check MX records
dig example.com MXVerify mail server A records
dig mail.example.com ACheck reverse DNS of mail server
dig -x 192.168.1.100`Debugging DNS Propagation
`bash
Check multiple authoritative servers
dig @ns1.example.com example.com dig @ns2.example.com example.comCompare with public DNS servers
dig @8.8.8.8 example.com dig @1.1.1.1 example.com`Network Connectivity Testing
`bash
Force TCP connection (useful for firewall testing)
dig +tcp example.comTest specific port
dig +tcp -p 5353 @192.168.1.1 example.comDisable recursion to test authoritative servers directly
dig +norecurse @ns1.example.com example.com`Practical Examples
Web Server Migration Verification
When migrating web servers, verify DNS changes:
`bash
#!/bin/bash
DNS migration verification script
DOMAIN="example.com" OLD_IP="192.168.1.100" NEW_IP="192.168.1.200"
echo "Checking DNS propagation for $DOMAIN" echo "Expected new IP: $NEW_IP" echo "Old IP: $OLD_IP" echo
Check multiple DNS servers
DNS_SERVERS=("8.8.8.8" "1.1.1.1" "208.67.222.222" "9.9.9.9")for server in "${DNS_SERVERS[@]}"; do
echo "Checking DNS server: $server"
RESULT=$(dig +short @$server $DOMAIN)
echo "Result: $RESULT"
if [ "$RESULT" = "$NEW_IP" ]; then
echo "Status: UPDATED"
elif [ "$RESULT" = "$OLD_IP" ]; then
echo "Status: OLD (not yet propagated)"
else
echo "Status: UNKNOWN"
fi
echo
done
`
Email Server Configuration Check
`bash
#!/bin/bash
Email server DNS verification
DOMAIN="example.com"
echo "Email DNS Configuration Check for $DOMAIN" echo "=========================================="
Check MX records
echo "MX Records:" dig +short $DOMAIN MX | sort -n echoCheck SPF records
echo "SPF Records:" dig +short $DOMAIN TXT | grep -i spf echoCheck DKIM records (common selector names)
echo "DKIM Records:" for selector in default mail dkim; do echo "Checking selector: $selector" dig +short ${selector}._domainkey.$DOMAIN TXT done echoCheck DMARC record
echo "DMARC Record:" dig +short _dmarc.$DOMAIN TXT`DNS Server Performance Testing
`bash
#!/bin/bash
DNS performance testing script
DOMAIN="example.com" DNS_SERVERS=("8.8.8.8" "1.1.1.1" "208.67.222.222" "9.9.9.9")
echo "DNS Performance Test for $DOMAIN" echo "================================"
for server in "${DNS_SERVERS[@]}"; do
echo "Testing $server:"
# Run 5 queries and extract timing
TIMES=()
for i in {1..5}; do
TIME=$(dig @$server $DOMAIN | grep "Query time" | awk '{print $4}')
TIMES+=($TIME)
done
# Calculate average
TOTAL=0
for time in "${TIMES[@]}"; do
TOTAL=$((TOTAL + time))
done
AVERAGE=$((TOTAL / 5))
echo " Times: ${TIMES[*]} msec"
echo " Average: $AVERAGE msec"
echo
done
`
DNSSEC Validation Check
`bash
#!/bin/bash
DNSSEC validation verification
DOMAIN="example.com"
echo "DNSSEC Validation Check for $DOMAIN" echo "==================================="
Check if DNSSEC is enabled
echo "Checking DNSSEC records:" dig +dnssec $DOMAIN | grep -E "(RRSIG|DNSKEY|DS)" echoCheck DNSKEY records
echo "DNSKEY Records:" dig +short $DOMAIN DNSKEY echoCheck DS records from parent zone
PARENT_ZONE=$(echo $DOMAIN | sed 's/^[^.]*\.//') echo "DS Records from parent zone ($PARENT_ZONE):" dig +short $DOMAIN DS echoValidate with DNSSEC-aware resolver
echo "DNSSEC Validation Test:" dig +dnssec +multiline $DOMAIN | grep -A 5 "flags:"`Best Practices
Performance Optimization
1. Use +short for scripting: When writing scripts, use +short to get clean output
2. Cache DNS responses: For repeated queries, consider caching mechanisms
3. Choose appropriate DNS servers: Use geographically close DNS servers for better performance
4. Use specific record types: Query only the record types you need
Security Considerations
1. Verify DNSSEC: Always check DNSSEC validation for critical domains 2. Use trusted DNS servers: Avoid using unknown or untrusted DNS servers 3. Monitor DNS changes: Regularly verify DNS configurations 4. Implement DNS filtering: Use DNS servers that provide malware protection
Scripting Guidelines
`bash
Good practice: Error handling in scripts
if ! command -v dig &> /dev/null; then echo "Error: dig command not found" exit 1 fiRESULT=$(dig +short example.com) if [ -z "$RESULT" ]; then echo "Error: No DNS response received" exit 1 fi
echo "IP Address: $RESULT"
`
Documentation and Monitoring
1. Document DNS configurations: Keep records of all DNS settings 2. Monitor DNS propagation: Track changes across different DNS servers 3. Set up alerts: Monitor for unexpected DNS changes 4. Regular audits: Periodically review DNS configurations
Alternative Tools
Comparison with Other DNS Tools
| Tool | Advantages | Disadvantages | Best Use Case | |------|------------|---------------|---------------| | dig | Detailed output, flexible options | Complex for beginners | Professional DNS troubleshooting | | nslookup | Simple interface, widely available | Limited functionality, deprecated | Basic queries, legacy systems | | host | Simple output, fast | Limited options | Quick lookups, scripting | | drill | DNSSEC focus, secure | Less common, LDNS dependency | DNSSEC validation |
Modern Alternatives
doggo: A modern DNS client with colorful output
`bash
doggo example.com
`
kdig: Part of Knot DNS, similar to dig
`bash
kdig example.com
`
q: Simple DNS lookup tool
`bash
q example.com
`
Integration with Other Tools
Combining with curl for web testing:
`bash
IP=$(dig +short example.com)
curl -H "Host: example.com" http://$IP/
`
Using with monitoring systems:
`bash
Nagios/Icinga plugin format
if dig +short example.com | grep -q "192.168.1.100"; then echo "OK - DNS resolved correctly" exit 0 else echo "CRITICAL - DNS resolution failed" exit 2 fi`Conclusion
The dig command is an indispensable tool for DNS administration and troubleshooting. Its comprehensive feature set, detailed output options, and flexibility make it the preferred choice for network professionals. Understanding its various options, output format, and practical applications enables effective DNS management and problem resolution.
Regular practice with different query types, options, and scenarios will improve proficiency with this powerful tool. Whether performing simple lookups or complex DNS investigations, dig provides the necessary functionality to understand and resolve DNS-related issues efficiently.
The examples and scripts provided in this guide serve as a foundation for building more sophisticated DNS monitoring and troubleshooting workflows. As DNS infrastructure continues to evolve with new record types and security features like DNSSEC, dig remains current and capable of handling modern DNS requirements.