Introduction
The dig command (Domain Information Groper) is an essential tool for system administrators and network professionals on Linux. It allows performing DNS queries in a detailed and flexible manner, surpassing simpler utilities such as nslookup or host. Its origin in the BIND package gives it precision and a wealth of options that make it the de facto standard for domain name diagnosis.
What is dig?
Dig is part of the BIND (Berkeley Internet Name Domain) package and is responsible for querying name servers to obtain information about DNS records, such as A, AAAA, MX, TXT, CNAME, NS, SRV, and many others. Its output can be customized via various options, making it a powerful ally for debugging name resolution problems, validating zone configurations, and automating tasks via scripts.
Installation
In most Linux distributions, dig comes preinstalled. If it is not available, simply install the corresponding package:
- Debian/Ubuntu:
sudo apt-get install dnsutils - Red Hat/CentOS:
sudo yum install bind-utils - Fedora:
sudo dnf install bind-utils - Arch Linux:
sudo pacman -S bind-tools
Basic Queries
A simple query is executed by indicating the name to resolve:
dig example.com
This returns the answer section with the default A record, along with the question, authority, and additional sections. Each section includes useful information such as time-to-live (TTL), class (IN), and record type.
Advanced Queries
Dig offers multiple flags that allow fine-tuning the query:
+short: shows only the answer, ideal for scripts.+trace: shows the full path from the roots to the authoritative server, displaying each step of the delegation.+json: (available in recent versions) generates the output in JSON format for easy processing by programs.@server: specifies the DNS server to query, for example@8.8.8.8or@1.1.1.1.type: indicates the record type, such asMX,TXT,ANYorSRV.+noall +answer: suppresses all sections except the answer, useful for obtaining clean data.+retry=N: defines how many retries to perform in case of timeout.+timeout=N: sets the wait time in seconds for each query.
Practical Examples
Some common uses:
- Obtain only the IP address:
dig +short example.com
dig example.com MX +short
dig +trace example.com
dig @1.1.1.1 example.com TXT +json
dig example.com ANY +noall +answer
dig example.com AAAA +short | grep -v '^;'
for s in 8.8.8.8 1.1.1.
This post is also available in ESPAÑOL.