How to use the command 'dig' (with examples)

How to use the command 'dig' (with examples)

The ‘dig’ command is a DNS lookup utility that allows users to query DNS servers to retrieve information about domain names and IP addresses. It provides a range of options and query types to fetch different types of DNS records. This article provides examples of various use cases of the ‘dig’ command.

Use case 1: Lookup the IP(s) associated with a hostname (A records)

Code:

dig +short example.com

Motivation: This use case is useful when you want to quickly retrieve the IP address(es) associated with a specific domain name. It can be helpful for troubleshooting network connectivity issues or for verifying DNS configurations.

Explanation:

  • +short: This option is used to display the output in a concise format, showing only the IP addresses without any additional information.
  • example.com: Replace this with the domain name you want to look up.

Example output:

93.184.216.34

Use case 2: Get a detailed answer for a given domain (A records)

Code:

dig +noall +answer example.com

Motivation: When you need a more detailed response for a specific domain, this use case provides a comprehensive answer. It includes the IP address and additional information, such as the record type, TTL (time to live), and the domain name.

Explanation:

  • +noall: This option is used to omit all the default output, such as the version, query time, etc.
  • +answer: This option ensures that only the answer section is displayed in the output.
  • example.com: Replace this with the domain name you want to query.

Example output:

example.com.        3599    IN  A   93.184.216.34

Use case 3: Query a specific DNS record type associated with a given domain name

Code:

dig +short example.com A|MX|TXT|CNAME|NS

Motivation: Different record types provide different types of information about a domain. This use case allows you to query specific record types (A, MX, TXT, CNAME, NS) associated with a domain name.

Explanation:

  • +short: This option is used to display the output in a concise format, showing only the results without any additional information.
  • example.com: Replace this with the domain name you want to query.
  • A|MX|TXT|CNAME|NS: Replace this with the desired record type(s) (A, MX, TXT, CNAME, NS) separated by a pipe (|) symbol.

Example output:

  • For A record:
93.184.216.34
  • For MX record:
10 mx.example.com.
  • For TXT record:
"v=spf1 include:_spf.example.com ~all"
  • For CNAME record:
www.example-cname.com.
  • For NS record:
ns1.example-ns.com.
ns2.example-ns.com.

Use case 4: Specify an alternate DNS server to query

Code:

dig @8.8.8.8 example.com

Motivation: By default, ‘dig’ queries the DNS server(s) configured on your system. However, in some cases, you may want to query a specific DNS server directly. This use case allows you to specify an alternate DNS server to retrieve the DNS information.

Explanation:

  • @8.8.8.8: Replace this IP address with the address of the DNS server you want to query.
  • example.com: Replace this with the domain name you want to query.

Example output:

;; ANSWER SECTION:
example.com.        3599    IN  A   93.184.216.34

Use case 5: Perform a reverse DNS lookup on an IP address (PTR record)

Code:

dig -x 8.8.8.8

Motivation: Reverse DNS lookup is useful when you have an IP address and want to find the associated domain name. This use case helps you obtain the PTR record by querying the DNS server(s).

Explanation:

  • -x 8.8.8.8: This option is used to perform a reverse DNS lookup on the specified IP address.
  • 8.8.8.8: Replace this with the IP address you want to reverse lookup.

Example output:

;; ANSWER SECTION:
8.8.8.8.in-addr.arpa. 21599  IN  PTR dns.google.

Use case 6: Find authoritative name servers for the zone and display SOA records

Code:

dig +nssearch example.com

Motivation: This use case helps you retrieve the authoritative name servers for a specific domain and also display the SOA (Start of Authority) record, which includes essential information about the zone file for that domain.

Explanation:

  • +nssearch: This option is used to search for the authoritative name servers for the zone.
  • example.com: Replace this with the domain name you want to query.

Example output:

;; AUTHORITY SECTION:
example.com.        172799  IN  NS  ns1.example-dns.com.
example.com.        172799  IN  NS  ns2.example-dns.com.

;; ADDITIONAL SECTION:
ns1.example-dns.com. 172799  IN  A   192.0.2.1
ns2.example-dns.com. 172799  IN  A   192.0.2.2

;; ADDITIONAL AUTHORITY SECTION:
example.com.        172799  IN  SOA ns1.example-dns.com. hostmaster.example.com. 2022092001 28800 7200 604800 86400

Use case 7: Perform iterative queries and display the entire trace path to resolve a domain name

Code:

dig +trace example.com

Motivation: When troubleshooting DNS issues or analyzing the DNS resolution process, it can be helpful to view the entire trace path that ‘dig’ follows to resolve a domain name. This use case provides a detailed output of each step taken by ‘dig’ to obtain the final result.

Explanation:

  • +trace: This option is used to perform iterative queries and display the entire trace path.
  • example.com: Replace this with the domain name you want to query.

Example output:

; <<>> DiG 9.11.3-1ubuntu1.23-Ubuntu <<>> +trace example.com
;; global options: +cmd
.                       86288   IN      NS      m.root-servers.net.
.                       86288   IN      NS      e.root-servers.net.
.                       86288   IN      NS      b.root-servers.net.
.                       86288   IN      NS      h.root-servers.net.
.                       86288   IN      NS      j.root-servers.net.
.                       86288   IN      NS      a.root-servers.net.
.                       86288   IN      NS      i.root-servers.net.
.                       86288   IN      NS      c.root-servers.net.
.                       86288   IN      NS      k.root-servers.net.
.                       86288   IN      NS      d.root-servers.net.
.                       86288   IN      NS      l.root-servers.net.
.                       86288   IN      NS      g.root-servers.net.
.                       86288   IN      NS      f.root-servers.net.
;; Received 239 bytes from 127.0.0.53#53(127.0.0.53) in 2 ms

com.                    172800  IN      NS      j.gtld-servers.net.
com.                    172800  IN      NS      i.gtld-servers.net.
com.                    172800  IN      NS      l.gtld-servers.net.
com.                    172800  IN      NS      b.gtld-servers.net.
com.                    172800  IN      NS      d.gtld-servers.net.
com.                    172800  IN      NS      g.gtld-servers.net.
com.                    172800  IN      NS      f.gtld-servers.net.
com.                    172800  IN      NS      e.gtld-servers.net.
com.                    172800  IN      NS      k.gtld-servers.net.
com.                    172800  IN      NS      m.gtld-servers.net.
com.                    172800  IN      NS      c.gtld-servers.net.
com.                    172800  IN      NS      a.gtld-servers.net.
com.                    172800  IN      NS      h.gtld-servers.net.
;; Received 494 bytes from 198.41.0.4#53(a.root-servers.net) in 32 ms

example.com.            172800  IN      NS      ns1.example-dns.com.
example.com.            172800  IN      NS      ns2.example-dns.com.
;; Received 254 bytes from 192.55.83.30#53(l.gtld-servers.net) in 39 ms

example.com.            172800  IN      A       93.184.216.34
example.com.            172800  IN      NS      ns2.example-dns.com.
example.com.            172800  IN      NS      ns1.example-dns.com.
;; Received 132 bytes from 192.0.2.1#53(ns1.example-dns.com) in 119 ms

Conclusion:

The ‘dig’ command is a powerful DNS lookup utility that provides various options and query types to retrieve DNS information. Whether you need to look up IP addresses, query specific record types, or analyze the DNS resolution process, ‘dig’ has you covered. By understanding the different use cases illustrated in this article, you can effectively leverage the ‘dig’ command to troubleshoot DNS issues and gather valuable information about domain names and IP addresses.

Related Posts

Using the "true" Command (with examples)

Using the "true" Command (with examples)

The “true” command is a basic Unix command that does nothing and always returns a successful exit status code of 0.

Read More
How to use the command 'git psykorebase' (with examples)

How to use the command 'git psykorebase' (with examples)

Git psykorebase is a command that allows users to rebase a branch on top of another using a merge commit with only one conflict handling.

Read More
How to use the command `lvdisplay` (with examples)

How to use the command `lvdisplay` (with examples)

This command is used to display information about Logical Volume Manager (LVM) logical volumes.

Read More