【网络成长记】dig常用命令

dig命令

1
2
3
4
5
6
7
8
9
10
11
12
dig 常用命令格式

dig [@server] [-p port] [-t type] [-4] [-6] [+trace] name

@ 指定 DNS 查询使用的服务器名称或 IP ,IP 地址可以是用点分隔的 IPv4 地址也可以是冒号分隔的 IPv6 地址。当参数指定的值是服务器的主机名时,dig 命令会在查询该域名服务器前先解析该主机名;
-p 指定 DNS 查询使用的端口号,默认情况下 DNS 查询使用标准的53端口,若使用非端口则需要通过 -p 参数指定,可使用此选项来测试已配置为侦听非标准端口号上的 DNS 服务器;
-t 指定 DNS 查询的记录类型,常用的类型包括:A/AAAA/NS/MX/CNAME 等,缺省查询类型是 A ;
-4 指定 dig 命令仅使用 IPv4 查询传输;
-6 指定 dig 命令仅使用 IPv6 查询传输;
+trace 跟踪从根名称服务器开始的迭代查询过程,缺省情况不使用跟踪。启用跟踪时,dig 命令会执行迭代查询以解析要查询的名称,显示来自用于解析查询的每个服务器的应答。

-x dot-notation (shortcut for reverse lookups)

常用命令汇总

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# 查询该域名的A记录  A记录  Address   本机只向自己的DNS服务器查询
dig math.stackexchange.com

# short参数 只返回域名对应的IP地址(即A记录)
dig math.stackexchange.com +short = dig +short math.stackexchange.com

# @参数,显示向其他DNS服务器查询的结果。 上面命令指定向DNS服务器4.2.2.2查询。
dig @4.2.2.2 math.stackexchange.com

# dig命令的+trace参数可以显示DNS的整个分级查询过程。
dig +trace math.stackexchange.com @4.2.2.2

# dig命令可以单独查看每一级域名的NS记录
dig ns com = dig -t ns com
dig ns stackexchange.com = dig -t ns stackexchange.com
dig +short ns com
dig +short ns stackexchange.com

# PTR记录用于从IP地址反查域名。dig命令的-x参数用于查询PTR记录。
dig -x 192.30.252.153


# dig命令可以查看指定的记录类型。
dig a github.com = dig A github.com
dig ns github.com # ns:域名服务器记录,指明该域名由哪台服务器来解析
dig mx github.com
dig aaaa facebook.github.com = dig AAAA facebook.github.com # ipv6
dig cname facebook.github.com # cname:规范名资源记录,允许多个名称对应同一主机

NS 记录(Name Server域名服务器):域名服务器记录,用来指定域名由哪个 DNS 服务器进行解析; name server
CNAME 记录(Canonical Name规范名):用来定义域名的别名,方便实现将多个域名解析到同一个 IP 地址;
A 记录(Address地址, IPv4):用来指定主机名对应的 IPv4 地址; address
AAAA 记录(AAAA:Address地址 IPv6):用来指定主机名对应的 IPv6 地址;
MX 记录(Mail Exchanger邮件交换):用来指定收件人域名的邮件服务器,SMTP 协议会根据 MX 记录的值来决定邮件的路由过程;
PTR 记录(Pointer指针):常用于反向地址解析,将 IP 地址解析到对应的名称;
SOA 记录(Start of Authority起始授权机构):称为起始授权机构记录,不同于 NS 记录用于标识多台域名解析服务器,SOA 记录用于在多台 NS 记录中哪一台是主 DNS 服务器。

dig +trace math.stackexchange.com 显示详情

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
[root@ningan ~]# dig +trace math.stackexchange.com

; <<>> DiG 9.11.3-1ubuntu1.18-Ubuntu <<>> +trace math.stackexchange.com
;; global options: +cmd
;; Received 51 bytes from 127.0.0.53#53(127.0.0.53) in 0 ms

[root@ningan ~]# dig +trace math.stackexchange.com @4.2.2.2

; <<>> DiG 9.11.3-1ubuntu1.18-Ubuntu <<>> +trace math.stackexchange.com @4.2.2.2
;; global options: +cmd
. 63834 IN NS e.root-servers.net.
. 63834 IN NS h.root-servers.net.
. 63834 IN NS l.root-servers.net.
. 63834 IN NS i.root-servers.net.
. 63834 IN NS a.root-servers.net.
. 63834 IN NS d.root-servers.net.
. 63834 IN NS c.root-servers.net.
. 63834 IN NS b.root-servers.net.
. 63834 IN NS j.root-servers.net.
. 63834 IN NS k.root-servers.net.
. 63834 IN NS g.root-servers.net.
. 63834 IN NS m.root-servers.net.
. 63834 IN NS f.root-servers.net.
. 63834 IN RRSIG NS 8 0 518400 20221006160000 20220923150000 20826 . WnPS5Jjb3LJyMJcOcRtpqCsOG9PcwoO4xlMfKNhc7gF6GgqR0Bgt32n1 7gPujfnNm+y6INedDeJC43ciPh9C+jWMKSIZQsNp8JkEc9bmndwcpyhP hMno3mapXZyoCB5sX1Px187YnXU4C7r2w7e9xYGEGoQIIbvs+CiwXlLL sjiERlqrfLmyITs9xxD7rJ4Qbr7AY60toB+DS4ZjSxZyTfYRLQLRFWGA o9v1cTMdqeTLi4fiGvCSQHdQhH3op8LxeHN8mXMmDw5AKQrRBDFuQaYA lCYdq63m91eh0SAGddCtfAylumQI6oAToB1btAG3LKbahYsY3QGIwINW z6QtLw==
;; Received 525 bytes from 4.2.2.2#53(4.2.2.2) in 101 ms

com. 172800 IN NS a.gtld-servers.net.
com. 172800 IN NS b.gtld-servers.net.
com. 172800 IN NS c.gtld-servers.net.
com. 172800 IN NS d.gtld-servers.net.
com. 172800 IN NS e.gtld-servers.net.
com. 172800 IN NS f.gtld-servers.net.
com. 172800 IN NS g.gtld-servers.net.
com. 172800 IN NS h.gtld-servers.net.
com. 172800 IN NS i.gtld-servers.net.
com. 172800 IN NS j.gtld-servers.net.
com. 172800 IN NS k.gtld-servers.net.
com. 172800 IN NS l.gtld-servers.net.
com. 172800 IN NS m.gtld-servers.net.
com. 86400 IN DS 30909 8 2 E2D3C916F6DEEAC73294E8268FB5885044A833FC5459588F4A9184CF C41A5766
com. 86400 IN RRSIG DS 8 1 86400 20221006160000 20220923150000 20826 . j4E3BSZSX/ao4cPxU0LMtlzR0rMkRjqtB5Tes1UsbvSdGP9wkfFnqxx6 f/XCatshen6jSAKf/gsP2TNa4Jt19/cB/zCjeR1maMznliYLqNaZrrQT Iv/n48chpIZdZ+uBovrZ0j3zU25vaqD1foy17brcm6QYnZhH+PWHw9gT LIzRFdfLqWrBxVqdHbfCfD10h573H0PzUUwvdEU0wbIR/4Zv0YhuXQIV t77haFhp6Q4M3SbWtWhZO7kaIbssIRlPmVWPBk3s+r0RVeQHyLNIZtZF XgaxFtuemV/KUGckDHh+IzjCYHygWcAAdwJRslPYVYuy1bMXNIGLgaj0 Y+GSzQ==
;; Received 1182 bytes from 199.7.83.42#53(l.root-servers.net) in 17 ms

stackexchange.com. 172800 IN NS ns-925.awsdns-51.net.
stackexchange.com. 172800 IN NS ns-1832.awsdns-37.co.uk.
stackexchange.com. 172800 IN NS ns-cloud-d1.googledomains.com.
stackexchange.com. 172800 IN NS ns-cloud-d2.googledomains.com.
CK0POJMG874LJREF7EFN8430QVIT8BSM.com. 86400 IN NSEC3 1 1 0 - CK0Q2D6NI4I7EQH8NA30NS61O48UL8G5 NS SOA RRSIG DNSKEY NSEC3PARAM
CK0POJMG874LJREF7EFN8430QVIT8BSM.com. 86400 IN RRSIG NSEC3 8 2 86400 20220928042428 20220921031428 32298 com. dtNcF2OBpNtrH8p0VRZU6oqu2C54jzxUCwfpX3bt9sciVgV9AQ2n+7Dt j2tvPwGZPnT+DfD9Ri93aeg3RkzQT2ZnxnkAkDIcwJwF5LOqy0YuC6UT XSZ/R3XPndduNMAPn9KdH3gI7O8IdW2ikpt/2qfe4brvE3zeVP3aFJMP AYDD+C4J1zK9DxDn598PlGKE6LUU3ZNu56AJ7tiAgLtBRA==
4OTJD9T3D8J8DLAUKF6PTNN121LN819O.com. 86400 IN NSEC3 1 1 0 - 4OTJGI7PPFM3E45CKDKQ61BEQCTAA8H2 NS DS RRSIG
4OTJD9T3D8J8DLAUKF6PTNN121LN819O.com. 86400 IN RRSIG NSEC3 8 2 86400 20220927045638 20220920034638 32298 com. ii7OXKke5DccsV2jkaJ6FvIWpYIIJQC1pTy1QbLvghe0zq2fdkZsis0Q 8I0n5wNIz3+N0JVT5QA7E5NfRD9nsyDxD76FS6/JUuf2CdDoaah/EAMR sc46VbPOjpcQAZRW3gsvsHbg+GbrKkyszaq5I4ol1w4daUncX/h3qVky RXj7auCIimg6CYD5042GfxzyAQWfMKzZR9onED67YrG4hg==
;; Received 825 bytes from 192.26.92.30#53(c.gtld-servers.net) in 235 ms

math.stackexchange.com. 300 IN A 151.101.193.69
math.stackexchange.com. 300 IN A 151.101.65.69
math.stackexchange.com. 300 IN A 151.101.1.69
math.stackexchange.com. 300 IN A 151.101.129.69
stackexchange.com. 172800 IN NS ns-1832.awsdns-37.co.uk.
stackexchange.com. 172800 IN NS ns-925.awsdns-51.net.
stackexchange.com. 172800 IN NS ns-cloud-d1.googledomains.com.
stackexchange.com. 172800 IN NS ns-cloud-d2.googledomains.com.
;; Received 252 bytes from 205.251.195.157#53(ns-925.awsdns-51.net) in 59 ms

dig ns com 和 dig ns stackexchange.com 显示详情

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
[root@ningan ~]# dig ns com

; <<>> DiG 9.11.3-1ubuntu1.18-Ubuntu <<>> ns com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 51369
;; flags: qr rd ra; QUERY: 1, ANSWER: 13, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 65494
;; QUESTION SECTION:
;com. IN NS

;; ANSWER SECTION:
com. 0 IN NS i.gtld-servers.net.
com. 0 IN NS m.gtld-servers.net.
com. 0 IN NS c.gtld-servers.net.
com. 0 IN NS j.gtld-servers.net.
com. 0 IN NS d.gtld-servers.net.
com. 0 IN NS h.gtld-servers.net.
com. 0 IN NS e.gtld-servers.net.
com. 0 IN NS a.gtld-servers.net.
com. 0 IN NS l.gtld-servers.net.
com. 0 IN NS f.gtld-servers.net.
com. 0 IN NS g.gtld-servers.net.
com. 0 IN NS k.gtld-servers.net.
com. 0 IN NS b.gtld-servers.net.

;; Query time: 9 msec
;; SERVER: 127.0.0.53#53(127.0.0.53)
;; WHEN: Sat Sep 24 11:15:56 CST 2022
;; MSG SIZE rcvd: 256




[root@ningan ~]# dig ns stackexchange.com

; <<>> DiG 9.11.3-1ubuntu1.18-Ubuntu <<>> ns stackexchange.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 45577
;; flags: qr rd ra; QUERY: 1, ANSWER: 4, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 65494
;; QUESTION SECTION:
;stackexchange.com. IN NS

;; ANSWER SECTION:
stackexchange.com. 0 IN NS ns-cloud-d1.googledomains.com.
stackexchange.com. 0 IN NS ns-cloud-d2.googledomains.com.
stackexchange.com. 0 IN NS ns-925.awsdns-51.net.
stackexchange.com. 0 IN NS ns-1832.awsdns-37.co.uk.

;; Query time: 11 msec
;; SERVER: 127.0.0.53#53(127.0.0.53)
;; WHEN: Sat Sep 24 11:17:39 CST 2022
;; MSG SIZE rcvd: 183

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[root@ningan ~]# dig +short ns com
i.gtld-servers.net.
m.gtld-servers.net.
c.gtld-servers.net.
j.gtld-servers.net.
d.gtld-servers.net.
h.gtld-servers.net.
e.gtld-servers.net.
a.gtld-servers.net.
l.gtld-servers.net.
f.gtld-servers.net.
g.gtld-servers.net.
k.gtld-servers.net.
b.gtld-servers.net.
[root@ningan ~]#
[root@ningan ~]#
[root@ningan ~]# dig +short ns stackexchange.com
ns-cloud-d1.googledomains.com.
ns-cloud-d2.googledomains.com.
ns-925.awsdns-51.net.
ns-1832.awsdns-37.co.uk.

dig -x 192.30.252.153 显示详情

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
; <<>> DiG 9.11.3-1ubuntu1.18-Ubuntu <<>> -x 192.30.252.153
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 48615
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 65494
;; QUESTION SECTION:
;153.252.30.192.in-addr.arpa. IN PTR

;; ANSWER SECTION:
153.252.30.192.in-addr.arpa. 0 IN PTR lb-192-30-252-153-iad.github.com.

;; Query time: 388 msec
;; SERVER: 127.0.0.53#53(127.0.0.53)
;; WHEN: Sat Sep 24 11:28:40 CST 2022
;; MSG SIZE rcvd: 102

dig a github.com 和 dig ns github.com 和 dig mx github.com 显示详情

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
[root@ningan ~]# dig a github.com

; <<>> DiG 9.11.3-1ubuntu1.18-Ubuntu <<>> a github.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 14608
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

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

;; ANSWER SECTION:
github.com. 0 IN A 140.82.114.3

;; Query time: 1 msec
;; SERVER: 127.0.0.53#53(127.0.0.53)
;; WHEN: Sat Sep 24 11:31:27 CST 2022
;; MSG SIZE rcvd: 55





[root@ningan ~]# dig ns github.com

; <<>> DiG 9.11.3-1ubuntu1.18-Ubuntu <<>> ns github.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 22967
;; flags: qr rd ra; QUERY: 1, ANSWER: 8, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 65494
;; QUESTION SECTION:
;github.com. IN NS

;; ANSWER SECTION:
github.com. 0 IN NS ns-520.awsdns-01.net.
github.com. 0 IN NS dns1.p08.nsone.net.
github.com. 0 IN NS ns-421.awsdns-52.com.
github.com. 0 IN NS dns4.p08.nsone.net.
github.com. 0 IN NS dns3.p08.nsone.net.
github.com. 0 IN NS dns2.p08.nsone.net.
github.com. 0 IN NS ns-1707.awsdns-21.co.uk.
github.com. 0 IN NS ns-1283.awsdns-32.org.

;; Query time: 15 msec
;; SERVER: 127.0.0.53#53(127.0.0.53)
;; WHEN: Sat Sep 24 11:31:36 CST 2022
;; MSG SIZE rcvd: 262





[root@ningan ~]# dig mx github.com

; <<>> DiG 9.11.3-1ubuntu1.18-Ubuntu <<>> mx github.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 64538
;; flags: qr rd ra; QUERY: 1, ANSWER: 5, AUTHORITY: 0, ADDITIONAL: 1

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

;; ANSWER SECTION:
github.com. 0 IN MX 10 alt3.aspmx.l.google.com.
github.com. 0 IN MX 5 alt2.aspmx.l.google.com.
github.com. 0 IN MX 5 alt1.aspmx.l.google.com.
github.com. 0 IN MX 10 alt4.aspmx.l.google.com.
github.com. 0 IN MX 1 aspmx.l.google.com.

;; Query time: 15 msec
;; SERVER: 127.0.0.53#53(127.0.0.53)
;; WHEN: Sat Sep 24 11:31:56 CST 2022
;; MSG SIZE rcvd: 154

dig a facebook.github.com 和 dig aaaa facebook.github.com

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
[root@ningan ~]# dig  a facebook.github.com

; <<>> DiG 9.11.3-1ubuntu1.18-Ubuntu <<>> a facebook.github.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 4880
;; flags: qr rd ra; QUERY: 1, ANSWER: 5, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 65494
;; QUESTION SECTION:
;facebook.github.com. IN A

;; ANSWER SECTION:
facebook.github.com. 0 IN CNAME github.github.io.
github.github.io. 0 IN A 185.199.109.153
github.github.io. 0 IN A 185.199.110.153
github.github.io. 0 IN A 185.199.111.153
github.github.io. 0 IN A 185.199.108.153

;; Query time: 1 msec
;; SERVER: 127.0.0.53#53(127.0.0.53)
;; WHEN: Sat Sep 24 11:42:54 CST 2022
;; MSG SIZE rcvd: 142





[root@ningan ~]# dig aaaa facebook.github.com

; <<>> DiG 9.11.3-1ubuntu1.18-Ubuntu <<>> aaaa facebook.github.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 12984
;; flags: qr rd ra; QUERY: 1, ANSWER: 5, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 65494
;; QUESTION SECTION:
;facebook.github.com. IN AAAA

;; ANSWER SECTION:
facebook.github.com. 0 IN CNAME github.github.io.
github.github.io. 0 IN AAAA 2606:50c0:8000::153
github.github.io. 0 IN AAAA 2606:50c0:8002::153
github.github.io. 0 IN AAAA 2606:50c0:8001::153
github.github.io. 0 IN AAAA 2606:50c0:8003::153

;; Query time: 7 msec
;; SERVER: 127.0.0.53#53(127.0.0.53)
;; WHEN: Sat Sep 24 11:42:49 CST 2022
;; MSG SIZE rcvd: 190

dig cname facebook.github.com 显示详情

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
[root@ningan ~]# dig cname facebook.github.com

; <<>> DiG 9.11.3-1ubuntu1.18-Ubuntu <<>> cname facebook.github.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 43133
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 65494
;; QUESTION SECTION:
;facebook.github.com. IN CNAME

;; ANSWER SECTION:
facebook.github.com. 0 IN CNAME github.github.io.

;; Query time: 355 msec
;; SERVER: 127.0.0.53#53(127.0.0.53)
;; WHEN: Sat Sep 24 11:59:13 CST 2022
;; MSG SIZE rcvd: 78




[root@ningan ~]# dig facebook.github.com +short
github.github.io.
185.199.109.153
185.199.110.153
185.199.111.153
185.199.108.153



[root@ningan ~]# dig github.github.io +short
185.199.109.153
185.199.110.153
185.199.111.153
185.199.108.153
[root@ningan ~]#

dig && k8s

1
2
3
dig kubernetes.default +search
dig kubernetes.default A kubernetes.default AAAA +search
nslookup kubernetes.default

参考

DNS 原理入门 写的灰常好,推荐
dig 命令洞察 DNS 解析过程


【网络成长记】dig常用命令
http://example.com/2022/09/24/net/【网络成长记】dig常用命令/
作者
ningan123
发布于
2022年9月24日
许可协议