The dig
(Domain Information Groper) command is a powerful DNS lookup utility used for querying DNS name servers to retrieve information about host addresses, mail exchanges, name servers, and related information. This reference provides commonly used dig
commands and examples to help you perform DNS queries effectively.
The general syntax for using dig
is:
dig [@server] [name] [type] [options]
@server
- Specifies the DNS server to query.name
- The domain name to query.type
- The type of DNS record to query (e.g., A, MX, NS).Option | Description |
---|---|
+short |
Display a short answer. |
+noall |
Suppress all output except the query answer. |
+answer |
Display the answer section. |
+trace |
Trace the delegation path from the root name servers. |
+nssearch |
Perform an NS record search. |
-x <address> |
Perform a reverse lookup on an IP address. |
+tcp |
Use TCP instead of UDP for queries. |
+time=N |
Set the timeout for a query to N seconds. |
+tries=N |
Set the number of times to retry a query to N. |
+multiline |
Print records in a more readable format. |
Type | Description |
---|---|
A |
IPv4 address record. |
AAAA |
IPv6 address record. |
CNAME |
Canonical name record (alias). |
MX |
Mail exchange record. |
NS |
Name server record. |
TXT |
Text record. |
SOA |
Start of authority record. |
PTR |
Pointer record (used for reverse DNS lookups). |
ANY |
All records. |
# Query the A record for example.com
dig example.com
# Query the MX records for example.com
dig example.com MX
# Query using Google's public DNS server
dig @8.8.8.8 example.com
# Get a concise answer
dig example.com +short
# Reverse lookup for IP address 8.8.8.8
dig -x 8.8.8.8
# Trace the DNS resolution for example.com
dig example.com +trace
# Retrieve all DNS records for example.com
dig example.com ANY
# Set timeout to 5 seconds and retries to 2
dig example.com +time=5 +tries=2
# Get TXT records for example.com
dig example.com TXT
# Use TCP protocol for the query
dig example.com +tcp
# Search for name servers of example.com
dig example.com NS
# Get the Start of Authority record
dig example.com SOA
The output of dig
consists of several sections:
You can perform multiple queries in one command by using a text file with a list of domains:
# Create a file called domains.txt with one domain per line
example.com
example.net
example.org
# Run dig for each domain
dig -f domains.txt
You can also redirect the output to a file:
dig -f domains.txt > results.txt
To perform a reverse DNS lookup (PTR record), use the -x
option:
# Reverse lookup for IP 8.8.4.4
dig -x 8.8.4.4
The +short
option is useful when using dig
in shell scripts:
# Get the IP address of a domain
IP=$(dig +short example.com)
echo "The IP address of example.com is $IP"
# Query different DNS servers
dig @8.8.8.8 example.com
dig @1.1.1.1 example.com
# Check DNSSEC validation
dig example.com +dnssec
# Query the authoritative name server
dig @ns1.example.com example.com
Option | Description |
---|---|
+nocmd |
Suppress the command from the output. |
+nostats |
Suppress the statistics section. |
+noquestion |
Suppress the question section. |
+noauthority |
Suppress the authority section. |
+noadditional |
Suppress the additional section. |
+nocomments |
Suppress comment lines. |
+qr |
Display the query as it is sent. |
+identify |
Show the responder's IP address and port number. |
+ttlid |
Display TTLs in units instead of as comments. |
# Get MX records for a domain
dig example.com MX +short
# Get SPF information from TXT records
dig example.com TXT +short
# List NS records
dig example.com NS +short
# Use TCP for DNS query
dig example.com +tcp
+time=N
and +tries=N
to adjust timeouts and retries.+trace
to see the resolution path and identify misconfigurations.