DevOps Blog

A Beginner's Guide to Using the `dig` Command in Linux

Installing dig

Most Linux distributions come with dig pre-installed as part of the BIND DNS utilities. If it's not available, you can install it with:

  • Debian/Ubuntu:
    sudo apt install dnsutils
  • RHEL/CentOS:
    sudo yum install bind-utils
  • Arch Linux:
    sudo pacman -S bind-tools

Basic Usage of dig

The simplest way to use dig is to query a domain’s DNS records:

dig example.com

This will return the A record (IPv4 address) of example.com along with additional DNS response details.

Understanding the Output

A typical dig response includes:

  • Header section: Shows query details (like the DNS server used).
  • Question section: The query you made.
  • Answer section: The DNS record(s) returned.
  • Additional section: Extra information (like the authoritative nameservers).

Example output:

; <<>> DiG 9.16.1-Ubuntu <<>> 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 ;; QUESTION SECTION: ;example.com. IN A ;; ANSWER SECTION: example.com. 3600 IN A 93.184.216.34 ;; Query time: 10 msec ;; SERVER: 8.8.8.8#53(8.8.8.8) ;; WHEN: Fri Apr 04 12:00:00 UTC 2025 ;; MSG SIZE rcvd: 56

Common dig Use Cases

1. Querying Specific DNS Record Types

By default, dig fetches the A record, but you can specify other record types:

  • MX (Mail Exchange) Records:
    dig example.com MX
  • NS (Nameserver) Records:
    dig example.com NS
  • TXT Records (for SPF, DKIM, etc.):
    dig example.com TXT
  • CNAME Records (Aliases):
    dig www.example.com CNAME

2. Using a Custom DNS Server

By default, dig uses your system's DNS resolver. To query a specific DNS server (like Google’s 8.8.8.8):

dig @8.8.8.8 example.com

3. Shortening the Output

If you only want the answer section, use +short:

dig example.com +short

Output:

93.184.216.34

4. Tracing DNS Resolution Path

To see the full DNS resolution path (similar to traceroute for DNS):

dig +trace example.com

5. Reverse DNS Lookup (PTR Record)

Find the domain associated with an IP address:

dig -x 93.184.216.34

Advanced dig Options

  • Show only the answer section:
    dig example.com +noall +answer
  • Query over TCP instead of UDP (useful for large responses):
    dig example.com +tcp
  • Set a custom query timeout:
    dig example.com +time=3

Conclusion

The dig command is an essential tool for network administrators, developers, and anyone working with DNS. With its flexibility and detailed output, you can quickly diagnose DNS issues, verify records, and troubleshoot connectivity problems.

Try experimenting with the examples above, and soon you’ll be a dig expert!

Got questions or tips? Drop them in the comments below!


Would you like me to add anything else, like troubleshooting tips or real-world examples? Let me know!