dig命令主要用来从dns域名服务器查询主机地址信息,与nslookup命令作用相同,但是比nslookup输出的信息更多。

安装dig

redhat系系统
yum install -y bind-utils
debian系系统
apt-get install -y dnsutils

如下查询新浪的dns解析

dig www.sina.com输出如下信息

现详细讲解下以上输出信息

; <<>> DiG 9.10.3-P4-Ubuntu <<>> www.sina.com
;; global options: +cmd

第一行至第二行: dig的版本号,查询的域名,全局参数设置,默认是+cmd 命令以+nocmd作为参数,则该部分信息不显示

;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 15886
;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 1

第三行至五行:从有关权威机构DNS 服务器收到响应的详细技术信息 opencode:操作码,QUER代表查询操作 status:状态,NOERROR则没有错误,代表着所请求的 DNS 服务器可以无障碍地服务于查询 id:编号,15886,16bit数字,dns协议中通过编号匹配返回和查询 flags:标志,可以出现可以不出现,不出则表示无标志 flags标志有qr,rd,ra,aa qr:query,查询标志,代表查询操作 rd:recursion desired,代表希望通过递归查询操作 ra:recursive available,代表查询的服务器支持递归查询操作 aa:authoritative answer 权威回复(查询结果由管理域名的域名服务器提供,而不是缓存服务器提供的,称为权威回复) QUERY:查询数,1代表有1个查询,对应下面的QUESTION SECTION中的记录数 ANSWER:结果数,3代表有3个结果,对应下面ANSWER SECTION中的记录数 AUTHORITY:权威域名服务器记录数,0代表该域名有0个权威域名服务器可供域名解析用 ADDITIONAL:额外记录数,1代表有1项额外记录 命令以+comments作为参数,可以不显示该部分

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096

第六行至第七行: 有关 DNS 扩展机制(EDNS)的信息 命令以+noedns作为参数,可以不显示该部分

;; QUESTION SECTION:
;www.sina.com.			IN	A

第八行至第九行: 我们要查询的域名的输出信息,默认是A记录 命令以+noquestion作为参数,可以不显示该部分

;; ANSWER SECTION:
www.sina.com.		40	IN	CNAME	us.sina.com.cn.
us.sina.com.cn.		40	IN	CNAME	spool.grid.sinaedge.com.
spool.grid.sinaedge.com. 34	IN	A	202.102.94.124

第十行至第十三行: 查询到的结果 可以看到www.sina.com 对应的ip为202.102.94.124 第一列为域名,第二列为TTL,缓存时间,单位秒,第三列为查询类别,A为A记录,CNAME为别名记录,第四列为解析的地址 命令以+noanswer作为参数,可以不显示该部分

;; Query time: 20 msec
;; SERVER: 114.114.114.114#53(114.114.114.114)
;; WHEN: Sun Aug 11 19:22:55 CST 2019
;; MSG SIZE  rcvd: 119

第十四行至第十七行:有关查询的统计信息 Query time:查询耗时 SERVER:查询使用的服务器地址和端口 WHEN:查询的时间 MSG SIZE rcvd:回应的大小,收到了119字节 命令以+nostats作为参数,可以不显示该部分

dig相关的参数 +short 简单输出

root@node1:~# dig +short www.sina.com
us.sina.com.cn.
spool.grid.sinaedge.com.
202.102.94.124

+noall +answer 返回详细信息,只返回查询结果

root@node1:~# dig www.sina.com  +noall +answer

; <<>> DiG 9.10.3-P4-Ubuntu <<>> www.sina.com +noall +answer
;; global options: +cmd
www.sina.com.		60	IN	CNAME	us.sina.com.cn.
us.sina.com.cn.		60	IN	CNAME	spool.grid.sinaedge.com.
spool.grid.sinaedge.com. 34	IN	A	202.102.94.124

@x.x.x.x 从指定DNS服务器查询,默认dig 将主动使用 /etc/resolv.conf 文件中列出的 DNS 服务器来进行查询

root@node1:~# dig @8.8.8.8 www.sina.com

; <<>> DiG 9.10.3-P4-Ubuntu <<>> @8.8.8.8 www.sina.com
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 36464
;; flags: qr rd ra; QUERY: 1, ANSWER: 9, AUTHORITY: 0, ADDITIONAL: 1

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

;; ANSWER SECTION:
www.sina.com.		59	IN	CNAME	us.sina.com.cn.
us.sina.com.cn.		59	IN	CNAME	wwwus.sina.com.
wwwus.sina.com.		59	IN	CNAME	ww1.sinaimg.cn.w.alikunlun.com.
ww1.sinaimg.cn.w.alikunlun.com.	59 IN	A	205.204.104.186
ww1.sinaimg.cn.w.alikunlun.com.	59 IN	A	205.204.104.195
ww1.sinaimg.cn.w.alikunlun.com.	59 IN	A	47.246.16.229
ww1.sinaimg.cn.w.alikunlun.com.	59 IN	A	47.91.195.229
ww1.sinaimg.cn.w.alikunlun.com.	59 IN	A	47.246.16.230
ww1.sinaimg.cn.w.alikunlun.com.	59 IN	A	47.246.16.231

;; Query time: 203 msec
;; SERVER: 8.8.8.8#53(8.8.8.8)
;; WHEN: Sun Aug 11 21:03:25 CST 2019
;; MSG SIZE  rcvd: 226

指定查询类型

查询A记录(默认)
root@node1:~# dig +nocmd www.sina.com a +noall +answer
www.sina.com.		49	IN	CNAME	us.sina.com.cn.
us.sina.com.cn.		49	IN	CNAME	spool.grid.sinaedge.com.
spool.grid.sinaedge.com. 48	IN	A	202.102.94.124

查询CNAME记录
root@node1:~# dig +nocmd www.sina.com cname +noall +answer
www.sina.com.		34	IN	CNAME	us.sina.com.cn.

查询txt记录
root@node1:~# dig +nocmd google.com txt +noall +answer
google.com.		241	IN	TXT	"globalsign-smime-dv=CDYX+XFHUw2wml6/Gb8+59BsH31KzUr6c1l2BPvqKX8="
google.com.		241	IN	TXT	"docusign=05958488-4752-4ef2-95eb-aa7ba8a3bd0e"
google.com.		241	IN	TXT	"v=spf1 include:_spf.google.com ~all"
google.com.		241	IN	TXT	"docusign=1b0a6754-49b1-4db5-8540-d2c12664b289"
google.com.		241	IN	TXT	"facebook-domain-verification=22rm551cu4k0ab0bxsw536tlds4h95"

查询MX记录
root@node1:~# dig +nocmd google.com mx +noall +answer
google.com.		428	IN	MX	30 alt2.aspmx.l.google.com.
google.com.		428	IN	MX	20 alt1.aspmx.l.google.com.
google.com.		428	IN	MX	40 alt3.aspmx.l.google.com.
google.com.		428	IN	MX	50 alt4.aspmx.l.google.com.
google.com.		428	IN	MX	10 aspmx.l.google.com.

查询NS记录
root@node1:~# dig +nocmd google.com ns +noall +answer
google.com.		121046	IN	NS	ns3.google.com.
google.com.		121046	IN	NS	ns2.google.com.
google.com.		121046	IN	NS	ns4.google.com.
google.com.		121046	IN	NS	ns1.google.com.

-x 反向查询,查询与特定 IP 地址关联的域名 root@node1:~# dig -x 8.8.8.8 +noall +answer

; <<>> DiG 9.10.3-P4-Ubuntu <<>> -x 8.8.8.8 +noall +answer
;; global options: +cmd
8.8.8.8.in-addr.arpa.	79982	IN	PTR	dns.google.

+trace,跟踪查询过程

root@node1:~# dig +trace www.sina.com

; <<>> DiG 9.10.3-P4-Ubuntu <<>> +trace www.sina.com
;; global options: +cmd
.			142304	IN	NS	d.root-servers.net.
.			142304	IN	NS	i.root-servers.net.
.			142304	IN	NS	l.root-servers.net.
.			142304	IN	NS	k.root-servers.net.
.			142304	IN	NS	j.root-servers.net.
.			142304	IN	NS	a.root-servers.net.
.			142304	IN	NS	m.root-servers.net.
.			142304	IN	NS	c.root-servers.net.
.			142304	IN	NS	f.root-servers.net.
.			142304	IN	NS	e.root-servers.net.
.			142304	IN	NS	g.root-servers.net.
.			142304	IN	NS	h.root-servers.net.
.			142304	IN	NS	b.root-servers.net.
;; Received 239 bytes from 114.114.114.114#53(114.114.114.114) in 20 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 20190824050000 20190811040000 59944 . Au9ez+iicXM9ZCVNeBttnieOlMjtSLnFWS3fblB6yuJOPqAJDKlzGzH5 bo5A9agi+6ouB3di2J96Kdn2SE9Uq4mMaye4kQv7z7wUlthjwXadPAGq +nT4osIcOLxlWOCTLDBJnrCg6BnK7LoKMoleZjtQOr4ASNsW3CX3MEat SU/M+S9+Bg2NQeb+Z1rEieXIyG0zvlE+qzEg41rbuqFY3fk9kBMyB/sV 5lUeAZfAhlzH/jjKby+wJsaL+SRCPnstWgokekeZCHwXwJ4iHc2zRyCd 0e+FDT0z/7xouIYQVgt7GVQN0vQ/eUJBd9zhmCUbIfDi7w4AK9ykEJDU jVsVIg==
;; Received 1172 bytes from 198.97.190.53#53(h.root-servers.net) in 192 ms

sina.com.		172800	IN	NS	ns1.sina.com.cn.
sina.com.		172800	IN	NS	ns2.sina.com.cn.
sina.com.		172800	IN	NS	ns3.sina.com.cn.
sina.com.		172800	IN	NS	ns1.sina.com.
sina.com.		172800	IN	NS	ns2.sina.com.
sina.com.		172800	IN	NS	ns4.sina.com.
sina.com.		172800	IN	NS	ns3.sina.com.
CK0POJMG874LJREF7EFN8430QVIT8BSM.com. 86400 IN NSEC3 1 1 0 - CK0Q1GIN43N1ARRC9OSM6QPQR81H5M9A NS SOA RRSIG DNSKEY NSEC3PARAM
CK0POJMG874LJREF7EFN8430QVIT8BSM.com. 86400 IN RRSIG NSEC3 8 2 86400 20190815044522 20190808033522 17708 com. mZXFevDe/GAzLCPnKm123kkBDgeuHRJXSkMgsHkACMZmMmA5uCrvxXN8 7fTMTFFESSjd14PwgTavf5fJ4J4COTvIVHg7vrnztrt1JadLK3jXNPJ3 3Z8Cv5VINQMEPSAydR7XeZ8AMCvdkyEKB0jzpen719qTk4WWWAw33Jtw 9a8=
TGAG8VMC6NS5VVK68CIGRJ6Q414N2KB2.com. 86400 IN NSEC3 1 1 0 - TGAIBD36C6B9GMU6EB96HFA3PBUKS49B NS DS RRSIG
TGAG8VMC6NS5VVK68CIGRJ6Q414N2KB2.com. 86400 IN RRSIG NSEC3 8 2 86400 20190818052710 20190811041710 17708 com. XcB3H/b/3jb1ABIJ+ZF9o6PfwRjZsRuN1lue4f2RaRg41pINGot3s/oi itjHkSu4caYW8hxDpZDFWdsV8SRZFw54prByqOu8Bsuw0K9asjX/NEpH ehZov1MTBcJv0GoJN8+Vtj7GYVYuDWRT4LMJgNsfOitt5jTbEdHsVAhW 7XI=
;; Received 727 bytes from 192.26.92.30#53(c.gtld-servers.net) in 228 ms

www.sina.com.		60	IN	CNAME	us.sina.com.cn.
us.sina.com.cn.		60	IN	CNAME	spool.grid.sinaedge.com.
;; Received 103 bytes from 180.149.138.199#53(ns2.sina.com.cn) in 2 ms

dig批量查询 当有多个域名需要查询时,可以将要查询的域名写到一个文件,再统一查询

.digrc文件 如前面我们在命令行用到的+short,+noall,+answer等参数,我们可以写到当前用户家目录中的.digrc文件里(${home}/.digrc),这样当我们执行dig命令时就不需要再在命令行加这些参数了