Learn a Linux command every day (70): Dig

Time:2021-6-9

Recommended reading:Learn a Linux command every day (69): NSLOOKUP

Command introduction

The dig command is used to perform network DNS lookup. Dig is a flexible tool for querying DNS name servers. It performs a DNS lookup and displays the answer returned from the name server of the query.

Dig can be used to solve DNS problems, it has flexibility, ease of use and output clarity. Compared with other tools, it has more functions and is more powerful.

Grammatical format

dig [@server] [-b address] [-c class] [-f filename] [-k filename]
    [-m] [-p port#] [-q name] [-t type] [-x addr] [-y [hmac:]name:ke

Option description

@< server address >    # Specifies the domain name server for domain name resolution
-b     # Specify which IP address of the local machine is used to send the domain name query request to the domain name server
-F < file name >    # Specifies that the dig run in batch mode
-P   # Specifies the port number used by the domain name server
-T < type >   # Specifies the DNS data type to query
-x   # Perform reverse domain name query
-4   # Using IPv4
-6   # Using IPv6
-h   # display help information

Application examples

example

[[email protected] ~]# dig www.baidu.com
; <<>> DiG 9.11.4-P2-RedHat-9.11.4-26.P2.el7_9.4 <<>> www.baidu.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 57747
;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 0
;; QUESTION SECTION:
;www.baidu.com.   IN A
;; ANSWER SECTION:
www.baidu.com.  224 IN CNAME www.a.shifen.com.
www.a.shifen.com. 224 IN A 36.152.44.95
www.a.shifen.com. 224 IN A 36.152.44.96
;; Query time: 27 msec
;; SERVER: 223.5.5.5#53(223.5.5.5)
;; WHEN: Sat Mar 13 07:51:28 EST 2021
;; MSG SIZE  rcvd: 90

Reverse query

[[email protected] ~]# dig -x wwww.baidu.com
; <<>> DiG 9.11.4-P2-RedHat-9.11.4-26.P2.el7_9.4 <<>> -x wwww.baidu.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NXDOMAIN, id: 7825
;; flags: qr rd ra; QUERY: 1, ANSWER: 0, AUTHORITY: 1, ADDITIONAL: 0
;; QUESTION SECTION:
;com.baidu.wwww.in-addr.arpa. IN PTR
;; AUTHORITY SECTION:
in-addr.arpa.  600 IN SOA b.in-addr-servers.arpa. nstld.iana.org. 2021031279 1800 900 604800 3600
;; Query time: 151 msec
;; SERVER: 223.5.5.5#53(223.5.5.5)
;; WHEN: Sat Mar 13 07:52:54 EST 2021
;; MSG SIZE  rcvd: 113

Query the specified DNS server

[[email protected] ~]# dig @8.8.8.8 baidu.com
; <<>> DiG 9.11.4-P2-RedHat-9.11.4-26.P2.el7_9.4 <<>> @8.8.8.8 baidu.com
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 20803
;; flags: qr rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 0
;; QUESTION SECTION:
;baidu.com.   IN A
;; ANSWER SECTION:
baidu.com.  329 IN A 39.156.69.79
baidu.com.  329 IN A 220.181.38.148
;; Query time: 4 msec
;; SERVER: 8.8.8.8#53(8.8.8.8)
;; WHEN: Sat Mar 13 07:55:26 EST 2021
;; MSG SIZE  rcvd: 59

How many domain names to query at a time, write the domain name to the file, and then read the information from the file.

[[email protected] ~]# cat domain_names_file
www.baidu.com
mail.163.com
www.qq.com
www.aliyun.com
www.amazon.com
[[email protected] ~]# dig +noall +answer -f domain_names_file
www.baidu.com.  70 IN CNAME www.a.shifen.com.
www.a.shifen.com. 70 IN A 36.152.44.95
www.a.shifen.com. 70 IN A 36.152.44.96
mail.163.com.  117 IN CNAME ntes53.mail.163.com.
ntes53.mail.163.com. 117 IN A 123.126.97.202
www.qq.com.  71 IN CNAME ins-r23tsuuf.ias.tencent-cloud.net.
ins-r23tsuuf.ias.tencent-cloud.net. 71 IN A 183.194.238.19
ins-r23tsuuf.ias.tencent-cloud.net. 71 IN A 183.194.238.117
www.aliyun.com.  44 IN CNAME www-jp-de-intl-adns.aliyun.com.
www-jp-de-intl-adns.aliyun.com. 44 IN CNAME www-jp-de-intl-adns.aliyun.com.gds.alibabadns.com.
www-jp-de-intl-adns.aliyun.com.gds.alibabadns.com. 44 IN CNAME sh.wagbridge.aliyun.aliyun.com.
sh.wagbridge.aliyun.aliyun.com. 44 IN CNAME aliyun-adns.aliyun.com.
aliyun-adns.aliyun.com. 44 IN CNAME aliyun-adns.aliyun.com.gds.alibabadns.com.
aliyun-adns.aliyun.com.gds.alibabadns.com. 44 IN A 106.11.248.144
www.amazon.com.  15 IN CNAME tp.47cf2c8c9-frontier.amazon.com.
tp.47cf2c8c9-frontier.amazon.com. 15 IN CNAME d3ag4hukkh62yn.cloudfront.net.
d3ag4hukkh62yn.cloudfront.net. 15 IN A 13.225.100.223
#+Noall and  + Answer (displays only the response content of the DNS server)
#Without any parameters, compare the output
[[email protected] ~]# dig -f domain_names_file
; <<>> DiG 9.11.4-P2-RedHat-9.11.4-26.P2.el7_9.4 <<>> www.baidu.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 2834
;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 0
;; QUESTION SECTION:
;www.baidu.com.   IN A
;; ANSWER SECTION:
www.baidu.com.  86 IN CNAME www.a.shifen.com.
www.a.shifen.com. 86 IN A 36.152.44.95
www.a.shifen.com. 86 IN A 36.152.44.96
;; Query time: 23 msec
;; SERVER: 223.5.5.5#53(223.5.5.5)
;; WHEN: Sat Mar 13 08:02:46 EST 2021
;; MSG SIZE  rcvd: 90
; <<>> DiG 9.11.4-P2-RedHat-9.11.4-26.P2.el7_9.4 <<>> mail.163.com
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 39346
;; flags: qr rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 0
;; QUESTION SECTION:
;mail.163.com.   IN A
;; ANSWER SECTION:
mail.163.com.  36 IN CNAME ntes53.mail.163.com.
ntes53.mail.163.com. 36 IN A 123.126.97.202
;; Query time: 19 msec
;; SERVER: 223.5.5.5#53(223.5.5.5)
;; WHEN: Sat Mar 13 08:02:46 EST 2021
;; MSG SIZE  rcvd: 67
; <<>> DiG 9.11.4-P2-RedHat-9.11.4-26.P2.el7_9.4 <<>> www.qq.com
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 51368
;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 0
;; QUESTION SECTION:
;www.qq.com.   IN A
;; ANSWER SECTION:
www.qq.com.  78 IN CNAME ins-r23tsuuf.ias.tencent-cloud.net.
ins-r23tsuuf.ias.tencent-cloud.net. 78 IN A 183.194.238.19
ins-r23tsuuf.ias.tencent-cloud.net. 78 IN A 183.194.238.117
;; Query time: 22 msec
;; SERVER: 223.5.5.5#53(223.5.5.5)
;; WHEN: Sat Mar 13 08:02:46 EST 2021
;; MSG SIZE  rcvd: 108
; <<>> DiG 9.11.4-P2-RedHat-9.11.4-26.P2.el7_9.4 <<>> www.aliyun.com
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 5856
;; flags: qr rd ra; QUERY: 1, ANSWER: 6, AUTHORITY: 0, ADDITIONAL: 0
;; QUESTION SECTION:
;www.aliyun.com.   IN A
;; ANSWER SECTION:
www.aliyun.com.  22 IN CNAME www-jp-de-intl-adns.aliyun.com.
www-jp-de-intl-adns.aliyun.com. 22 IN CNAME www-jp-de-intl-adns.aliyun.com.gds.alibabadns.com.
www-jp-de-intl-adns.aliyun.com.gds.alibabadns.com. 22 IN CNAME sh.wagbridge.aliyun.aliyun.com.
sh.wagbridge.aliyun.aliyun.com. 22 IN CNAME aliyun-adns.aliyun.com.
aliyun-adns.aliyun.com. 22 IN CNAME aliyun-adns.aliyun.com.gds.alibabadns.com.
aliyun-adns.aliyun.com.gds.alibabadns.com. 22 IN A 106.11.172.51
;; Query time: 23 msec
;; SERVER: 223.5.5.5#53(223.5.5.5)
;; WHEN: Sat Mar 13 08:02:46 EST 2021
;; MSG SIZE  rcvd: 228
; <<>> DiG 9.11.4-P2-RedHat-9.11.4-26.P2.el7_9.4 <<>> www.amazon.com
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 36379
;; flags: qr rd ra; QUERY: 1, ANSWER: 4, AUTHORITY: 0, ADDITIONAL: 0
;; QUESTION SECTION:
;www.amazon.com.   IN A
;; ANSWER SECTION:
www.amazon.com.  26 IN CNAME tp.47cf2c8c9-frontier.amazon.com.
tp.47cf2c8c9-frontier.amazon.com. 26 IN CNAME www.amazon.com.edgekey.net.
www.amazon.com.edgekey.net. 26 IN CNAME e15316.e22.akamaiedge.net.
e15316.e22.akamaiedge.net. 26 IN A 223.119.142.58
;; Query time: 22 msec
;; SERVER: 223.5.5.5#53(223.5.5.5)
;; WHEN: Sat Mar 13 08:02:46 EST 2021
;; MSG SIZE  rcvd: 160

Learn a Linux command every day (70): Dig