Linux dig command
dig (Domain Information Groper) is a powerful DNS query tool in Linux used to query DNS name servers. Compared with the traditional nslookup, dig provides more detailed query results and more flexible query options.
The main features of the dig command:
- Display the complete DNS query process
- Support queries for all DNS record types
- Allow querying a specific DNS server
- Clear and readable output format
Basic syntax of the dig command
dig [@server] [domain] [query-type] [query-class] [query-options]
Parameter Description
| Parameters | Description |
|---|---|
@server |
Specify the DNS server to query (such as @8.8.8.8) |
domain |
The domain name to query (such as example.com) |
query-type |
The record type to query (such as A, MX, NS etc.) |
query-class |
The query class (usually IN for Internet) |
query-options |
Additional query options |
Common query types
dig supports querying many DNS record types; the following are common ones:
| Record type | Description | Example |
|---|---|---|
| A | IPv4 address record | dig example.com A |
| AAAA | IPv6 address record | dig example.com AAAA |
| MX | Mail exchange record | dig example.com MX |
| NS | Name server record | dig example.com NS |
| CNAME | Canonical name record | dig www.example.com CNAME |
| TXT | Text record | dig example.com TXT |
| SOA | Start of authority record | dig example.com SOA |
| ANY | All records | dig example.com ANY |
Understanding dig command output
Perform a simple dig query:
dig example.com
A typical output contains the following sections:
-
HEADER section: Displays basic information about the query
opcode: Opcodestatus: Response statusid: Query IDflags: Flags (such as qr, rd, ra, etc.)-
QUESTION section: Displays the query question
-
ANSWER section: Query result
-
AUTHORITY section: Authoritative name server information
-
ADDITIONAL section: Additional information
-
STATISTICS section: Query statistics
Practical query examples
1. Query a specific DNS server
dig @8.8.8.8 example.com
This command queries the A record of example.com from Google's public DNS server (8.8.8.8).
2. Query MX records
dig example.com MX
Query the mail server information of a domain.
3. Query the complete domain resolution process
dig +trace example.com
+trace The option displays the complete resolution process starting from the root name servers.
4. Short output mode
dig +short example.com
Show only the query result, without other detailed information.
5. Reverse DNS lookup
dig -x 8.8.8.8
Look up the corresponding domain name by IP address.
6. Batch query multiple domains
dig -f domains.txt +short
where domains.txt contains the list of domains to query, one per line.
Advanced Options
1. Control Output Format
dig +noall +answer example.com
+noall Hide all sections,+answer Show only the ANSWER section.
2. Set Query Timeout
dig +time=5 example.com
Set the query timeout to 5 seconds.
3. Specify Query Port
dig @dns.server.example.com -p 5353 example.com
Query the DNS server on a non-standard port (5353).
4. Show TTL Information
dig +ttlid example.com
Show the TTL (Time To Live) value of records.
5. Show Query Statistics
dig +stats example.com
Show detailed query statistics.
Frequently Asked Questions
1. What is the difference between dig and nslookup?
dig provides more detailed output and more query options than nslookup, making it a more professional DNS query tool. nslookup has gradually been replaced by dig.
2. How to install the dig command?
On most Linux distributions, dig is part of the bind-utils or dnsutils package:
-
Debian/Ubuntu:
sudo apt install dnsutils
-
CentOS/RHEL:
sudo yum install bind-utils
3. Why doesn't my dig query return an ANSWER section?
Possible reasons:
- The queried record type does not exist
- The domain name does not exist
- The DNS server has no record for that domain
- Network connection problems
You can try using the +trace option to view the complete resolution process.
Practical Application Scenarios
1. Troubleshooting DNS Resolution Issues
When a website is unreachable, you can use dig to check whether DNS resolution is working properly:
dig +trace example.com
2. Check Mail Server Configuration
Verify that the domain's mail server configuration is correct:
dig example.com MX
3. Monitor DNS Record Changes
By periodically running dig queries and comparing results, you can monitor changes to DNS records:
Example
dig
example.com A
|
grep
-v
";"
current.txt
diff
current.txt previous.txt
4. Test DNS Server Response Time
Compare the response speed of different DNS servers:
Example
time
dig
@
8.8.8.8 example.com
time
dig
@
1.1.1.1 example.com
Summary
The dig command is an essential DNS diagnostic tool for Linux system administrators and network engineers. After studying this article, you should be able to:
- Understand the basic syntax and common options of the dig command
- Query various types of DNS records
- Interpret the output of the dig command
- Use dig to solve real-world DNS-related problems
- Master the advanced usage and techniques of dig
To learn more about the dig command, you can view its manual page:
man dig
Or use the help option:
dig -h
Linux Command Reference