How to Use the Dig Command in Linux

The dig command in Linux is used to perform DNS lookups and query DNS servers. Here are some common ways to use dig.

First, install dig if it is not installed. See – https://www.geekdecoder.com/how-to-install-dig-utility-of-centos/

To query a specific DNS server for information about a domain name.

For example, to query the Google DNS server (8.8.8.8) for information about geekdecoder.com:

dig @8.8.8.8 geekdecoder.com

dig @8.8.8.8 geekdecoder.com

Output

 dig @8.8.8.8 geekdecoder.com

; <<>> DiG 9.11.4-P2-RedHat-9.11.4-26.P2.el7_9.13 <<>> @8.8.8.8 geekdecoder.com
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 13730
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 512
;; QUESTION SECTION:
;geekdecoder.com.               IN      A

;; ANSWER SECTION:
geekdecoder.com.        14400   IN      A       192.99.236.66

;; Query time: 31 msec
;; SERVER: 8.8.8.8#53(8.8.8.8)
;; WHEN: Tue Mar 14 14:25:57 UTC 2023
;; MSG SIZE  rcvd: 60

To get more detailed information about a domain name.

dig domain-name
For example:

dig geekdecoder.com

Output

dig geekdecoder.com

; <<>> DiG 9.11.4-P2-RedHat-9.11.4-26.P2.el7_9.13 <<>> geekdecoder.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 46107
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 512
;; QUESTION SECTION:
;geekdecoder.com.               IN      A

;; ANSWER SECTION:
geekdecoder.com.        14400   IN      A       192.99.236.66

;; Query time: 111 msec
;; SERVER: 67.207.67.3#53(67.207.67.3)
;; WHEN: Tue Mar 14 14:29:04 UTC 2023
;; MSG SIZE  rcvd: 60


To query for a specific record type:

dig record-type domain-name
For example, to query for the mail servers (MX records) for geekdecoder.com:

dig MX geekdecoder.com

Output

 dig MX geekdecoder.com

; <<>> DiG 9.11.4-P2-RedHat-9.11.4-26.P2.el7_9.13 <<>> MX geekdecoder.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 6621
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 512
;; QUESTION SECTION:
;geekdecoder.com.               IN      MX

;; ANSWER SECTION:
geekdecoder.com.        14400   IN      MX      0 mail.geekdecoder.com.

;; Query time: 101 msec
;; SERVER: 67.207.67.3#53(67.207.67.3)
;; WHEN: Tue Mar 14 14:30:37 UTC 2023
;; MSG SIZE  rcvd: 65

You can also combine options to get more specific results, such as:

dig +short A domain.com
This will return only the IP addresses (A records) for geekdecoder.com in a concise format.

dig +short A geekdecoder.com

Output

dig +short A geekdecoder.com
192.99.236.66

For more information about the dig command and its options, you can consult the dig manual page by typing man dig in the terminal.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.