Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Linux dig command

Linux dig command

Linux Command Reference Linux Command Reference


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:


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:

  1. HEADER section: Displays basic information about the query

  2. opcode: Opcode
  3. status: Response status
  4. id: Query ID
  5. flags: Flags (such as qr, rd, ra, etc.)
  6. - `opcode`: Opcode - `status`: Response status - `id`: Query ID - `flags`: Flags (such as qr, rd, ra, etc.)
    1. QUESTION section: Displays the query question

    2. Contains the queried domain name and record type
    3. - Contains the queried domain name and record type
      1. ANSWER section: Query result

      2. Contains the actual DNS record information
      3. - Contains the actual DNS record information
        1. AUTHORITY section: Authoritative name server information

        2. Shows the authoritative DNS servers responsible for the domain
        3. - Shows the authoritative DNS servers responsible for the domain
          1. ADDITIONAL section: Additional information

          2. May contain extra useful information
          3. - May contain extra useful information
            1. STATISTICS section: Query statistics

            2. Shows information such as query time and server
            3. - Shows information such as query time and server

              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:

              3. Why doesn't my dig query return an ANSWER section?

              Possible reasons:

              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:

              1. Understand the basic syntax and common options of the dig command
              2. Query various types of DNS records
              3. Interpret the output of the dig command
              4. Use dig to solve real-world DNS-related problems
              5. 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 Linux Command Reference