dig
命令是一个用于在 Unix/Linux 操作系统中执行 DNS 查询的工具。它是 DNS 客户端,通常用于查询 DNS 服务器的信息,如域名解析、IP 地址查询等。
博主博客
一、常见 DNS 记录类型
类型 | 描述 |
---|---|
A记录(Address Record) | 将域名解析为 IPv4 地址。例如,将 blog.uso6.com 解析为192.0.2.1 。 |
AAAA记录(IPv6 Address Record) | 将域名解析为 IPv6 地址。类似于 A 记录,但用于 IPv6 地址。例如,将. blog.uso6.com 解析为 2001:0db8:85a3:0000:0000:8a2e:0370:7334 。 |
CNAME记录(Canonical Name Record) | 将域名指向另一个域名(别名)。例如,将 www.uso6.com 指向 uso6.com 。 |
MX记录(Mail Exchange Record) | 指定接收域名的电子邮件服务器。例如,将 uso6.com 的 MX 记录指向 mail.uso6.com 。 |
TXT记录(Text Record) | 用于存储文本信息。通常用于验证域名所有权、配置SPF记录等。 |
NS记录(Name Server Record) | 指定管理特定域名的DNS服务器。例如,将 uso6.com 的 NS 记录指向 ns1.uso6.com 。 |
PTR记录(Pointer Record) | 用于反向 DNS 查找,将 IP 地址解析为域名。例如,将 192.0.2.1 解析为 uso6.com 。 |
SRV记录(Service Record) | 指定提供特定服务的主机和端口号。通常用于标识 XMPP、SIP 等服务的位置。 |
SOA记录(Start of Authority Record) | 指定主要域的授权DNS服务器和主要管理员的邮箱地址,以及域的重试间隔等参数。 |
这些是 DNS 中最常见的记录类型,每种记录类型都有其特定的用途和功能,用于支持互联网中的不同服务和功能。
二、基本用法
这将查询 uso6.com
的 DNS 记录,并返回与该域名相关的信息,如 IP 地址、MX 记录等。
[nukix@uso6 ~]# dig uso6.com
; <<>> DiG 9.10.6 <<>> uso6.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 26052
;; flags: qr rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 2, ADDITIONAL: 13
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;uso6.com. IN A
;; ANSWER SECTION:
uso6.com. 600 IN A 104.21.88.196
uso6.com. 600 IN A 172.67.152.157
;; AUTHORITY SECTION:
uso6.com. 578 IN NS jarred.ns.cloudflare.com.
uso6.com. 578 IN NS sima.ns.cloudflare.com.
;; ADDITIONAL SECTION:
sima.ns.cloudflare.com. 537 IN A 172.64.32.222
sima.ns.cloudflare.com. 537 IN A 173.245.58.222
sima.ns.cloudflare.com. 537 IN A 108.162.192.222
jarred.ns.cloudflare.com. 37 IN A 162.159.44.126
jarred.ns.cloudflare.com. 37 IN A 172.64.35.126
jarred.ns.cloudflare.com. 37 IN A 108.162.195.126
sima.ns.cloudflare.com. 393 IN AAAA 2a06:98c1:50::ac40:20de
sima.ns.cloudflare.com. 393 IN AAAA 2606:4700:50::adf5:3ade
sima.ns.cloudflare.com. 393 IN AAAA 2803:f800:50::6ca2:c0de
jarred.ns.cloudflare.com. 314 IN AAAA 2803:f800:50::6ca2:c37e
jarred.ns.cloudflare.com. 314 IN AAAA 2a06:98c1:50::ac40:237e
jarred.ns.cloudflare.com. 314 IN AAAA 2606:4700:58::a29f:2c7e
;; Query time: 197 msec
;; SERVER: 192.168.0.1#53(192.168.0.1)
;; WHEN: Wed May 22 19:48:49 CST 2024
;; MSG SIZE rcvd: 387
三、指定 DNS 服务器
使用指定的 DNS 服务器 IP 地址来执行 DNS 查询。例如使用 8.8.8.8
DNS 服务器, 一定要带 @
。
[nukix@uso6 ~]# dig uso6.com @8.8.8.8
; <<>> DiG 9.10.6 <<>> uso6.com @8.8.8.8
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 13776
;; flags: qr rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 1
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 512
;; QUESTION SECTION:
;uso6.com. IN A
;; ANSWER SECTION:
uso6.com. 300 IN A 104.21.88.196
uso6.com. 300 IN A 172.67.152.157
;; Query time: 162 msec
;; SERVER: 8.8.8.8#53(8.8.8.8)
;; WHEN: Wed May 22 19:52:53 CST 2024
;; MSG SIZE rcvd: 69
四、指定查询类型
查询域名的 MX (邮件交换)记录。
[nukix@uso6 ~]# dig uso6.com MX
; <<>> DiG 9.10.6 <<>> uso6.com MX
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 7948
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 2, ADDITIONAL: 15
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;uso6.com. IN MX
;; ANSWER SECTION:
uso6.com. 600 IN MX 10 mail.uso6.com.
;; AUTHORITY SECTION:
uso6.com. 221 IN NS sima.ns.cloudflare.com.
uso6.com. 221 IN NS jarred.ns.cloudflare.com.
;; ADDITIONAL SECTION:
mail.uso6.com. 578 IN A 148.135.109.239
sima.ns.cloudflare.com. 180 IN A 108.162.192.222
sima.ns.cloudflare.com. 180 IN A 172.64.32.222
sima.ns.cloudflare.com. 180 IN A 173.245.58.222
jarred.ns.cloudflare.com. 291 IN A 108.162.195.126
jarred.ns.cloudflare.com. 291 IN A 162.159.44.126
jarred.ns.cloudflare.com. 291 IN A 172.64.35.126
mail.uso6.com. 578 IN AAAA 2607:f130:0:13c::4ce9:b010
sima.ns.cloudflare.com. 36 IN AAAA 2803:f800:50::6ca2:c0de
sima.ns.cloudflare.com. 36 IN AAAA 2a06:98c1:50::ac40:20de
sima.ns.cloudflare.com. 36 IN AAAA 2606:4700:50::adf5:3ade
jarred.ns.cloudflare.com. 550 IN AAAA 2a06:98c1:50::ac40:237e
jarred.ns.cloudflare.com. 550 IN AAAA 2606:4700:58::a29f:2c7e
jarred.ns.cloudflare.com. 550 IN AAAA 2803:f800:50::6ca2:c37e
;; Query time: 161 msec
;; SERVER: 192.168.0.1#53(192.168.0.1)
;; WHEN: Wed May 22 19:54:46 CST 2024
;; MSG SIZE rcvd: 420
五、反向查询
对给定的 IP 地址执行反向 DNS 查询,查找对应的域名。比如我服务器 IP 是 148.135.109.239
。
[nukix@uso6 ~]# dig -x 148.135.109.239
;; Warning: Message parser reports malformed message packet.
; <<>> DiG 9.10.6 <<>> -x 148.135.109.239
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 36991
;; flags: qr ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;239.109.135.148.in-addr.arpa. IN PTR
;; ADDITIONAL SECTION:
239.109.135.148.in-addr.arpa. 497 IN PTR mail.uso6.com.
;; Query time: 53 msec
;; SERVER: 192.168.0.1#53(192.168.0.1)
;; WHEN: Wed May 22 19:57:28 CST 2024
;; MSG SIZE rcvd: 84
六、详细输出
输出详细的跟踪信息,显示 DNS 查询过程中经过的所有步骤。
[nukix@uso6 ~]# dig +trace uso6.com
; <<>> DiG 9.11.36-RedHat-9.11.36-13.el8 <<>> +trace uso6.com
;; global options: +cmd
. 5 IN NS d.root-servers.net.
. 5 IN NS c.root-servers.net.
. 5 IN NS b.root-servers.net.
. 5 IN NS a.root-servers.net.
. 5 IN NS m.root-servers.net.
. 5 IN NS l.root-servers.net.
. 5 IN NS k.root-servers.net.
. 5 IN NS j.root-servers.net.
. 5 IN NS i.root-servers.net.
. 5 IN NS h.root-servers.net.
. 5 IN NS g.root-servers.net.
. 5 IN NS f.root-servers.net.
. 5 IN NS e.root-servers.net.
;; Received 433 bytes from 100.100.2.136#53(100.100.2.136) in 0 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 19718 13 2 8ACBB0CD28F41250A80A491389424D341522D946B0DA0C0291F2D3D7 71D7805A
com. 86400 IN RRSIG DS 8 1 86400 20240604050000 20240522040000 5613 . fHw9nO0a5ElkgLINmgHJhSVOiu3RGsq+Xv7kev39DdYsPnjNN1hBUwNk UBqqrAJhuITTVqTvLB/ihF6FDzVEltvFfcSN3garR25wtMT+TL4xsfYu WqTBrSQN1KwX8enxYVGH6AkZadJ23cjSEvEWPaZsx0/0Gfc5FPDVWuhL PG9CqOirnEvqxS95WhRHKcfHiO3KPGjzT1Hu8yHZOC+1NK75M/Q21M7d y8jKOPJ6iW4fqqPElljeLqAkDmZIvmP/udPapzzx7TD7Eg6ctqqyR6/x yj2aGgj8G7V+Xfu49mB4e9/t1R5TwU5R8haij00IyHmRdF7f9Joc3kgd YK2XgA==
;; Received 1168 bytes from 199.7.83.42#53(l.root-servers.net) in 37 ms
uso6.com. 172800 IN NS sima.ns.cloudflare.com.
uso6.com. 172800 IN NS jarred.ns.cloudflare.com.
CK0POJMG874LJREF7EFN8430QVIT8BSM.com. 86400 IN NSEC3 1 1 0 - CK0Q2D6NI4I7EQH8NA30NS61O48UL8G5 NS SOA RRSIG DNSKEY NSEC3PARAM
CK0POJMG874LJREF7EFN8430QVIT8BSM.com. 86400 IN RRSIG NSEC3 13 2 86400 20240528042513 20240521031513 956 com. bUxLzRZW0UNAOPeMOTz2GMVh05uXRI74GZJLFuyUE5ITHgGLWyfRRYTH zSx4eDkhJTq3CMFD8zwI5gZ7ZakVRw==
CKAB4FRJE8NF15RJ0SM0K7C61MG4IJPL.com. 86400 IN NSEC3 1 1 0 - CKABJP4HNIAJJGRNNQH5B3IDPM1995BM NS DS RRSIG
CKAB4FRJE8NF15RJ0SM0K7C61MG4IJPL.com. 86400 IN RRSIG NSEC3 13 2 86400 20240527062437 20240520051437 956 com. 18vHpx0qvbeQz3UJ9hOvbCbGcA3zBVrsc/SIl7Ku1IrrLz95cOOrpDR4 /VzW5jfHqyn1Z0eXcX2J/DqbmA2T1Q==
;; Received 712 bytes from 192.48.79.30#53(j.gtld-servers.net) in 189 ms
uso6.com. 300 IN A 104.21.88.196
uso6.com. 300 IN A 172.67.152.157
;; Received 69 bytes from 172.64.35.126#53(jarred.ns.cloudflare.com) in 176 ms
七、显示详细的查询结果
只显示查询结果,不显示额外的信息。
[nukix@uso6 ~]# dig +noall +answer uso6.com
uso6.com. 10 IN A 172.67.152.157
uso6.com. 10 IN A 104.21.88.196
八、显示查询时间
设置查询超时时间,单位为秒。
[nukix@uso6 ~]# dig +time=1 uso6.com
; <<>> DiG 9.11.36-RedHat-9.11.36-13.el8 <<>> +time=1 uso6.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 13821
;; flags: qr rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 0
;; QUESTION SECTION:
;uso6.com. IN A
;; ANSWER SECTION:
uso6.com. 10 IN A 172.67.152.157
uso6.com. 10 IN A 104.21.88.196
;; Query time: 0 msec
;; SERVER: 100.100.2.136#53(100.100.2.136)
;; WHEN: Wed May 22 20:09:59 CST 2024
;; MSG SIZE rcvd: 58
九、显示指定域名的所有记录
查询给定域名的所有记录类型。
[nukix@uso6 ~]# dig uso6.com ANY
; <<>> DiG 9.11.36-RedHat-9.11.36-13.el8 <<>> uso6.com ANY
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 28326
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0
;; QUESTION SECTION:
;uso6.com. IN ANY
;; ANSWER SECTION:
uso6.com. 3600 IN HINFO "RFC8482" ""
;; Query time: 0 msec
;; SERVER: 100.100.2.136#53(100.100.2.136)
;; WHEN: Wed May 22 20:11:18 CST 2024
;; MSG SIZE rcvd: 47
十、显示精简结果
dig 命令默认返回的结果展示详细的信息,如果要获得精简的结果可以使用 +short
选项
[nukix@uso6 ~]# dig +short uso6.com
172.67.152.157
104.21.88.196
只显示 ANSWER SECTION
[nukix@uso6 ~]# dig +nocomments +noquestion +noauthority +noadditional +nostats uso6.com
; <<>> DiG 9.10.6 <<>> +nocomments +noquestion +noauthority +noadditional +nostats uso6.com
;; global options: +cmd
uso6.com. 600 IN A 104.21.88.196
uso6.com. 600 IN A 172.67.152.157
只查询 TTL(Time to Live)
[nukix@uso6 ~]# dig +nocmd +noall +answer +ttlid uso6.com
uso6.com. 600 IN A 104.21.88.196
uso6.com. 600 IN A 172.67.152.157
十一、总结
dig
命令提供了丰富的选项,可以进行各种类型的 DNS 查询。您可以通过 man dig
命令查看 dig
命令的完整手册以获取更多信息。