dig(Domain Information Groper)相似nslookup,也是DNS查詢工具。
linux
1、最簡單dig使用方法
shell
$ dig ; <<>> DiG 9.8.3-P1 <<>> ;; global options: +cmd ;; Got answer: ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 61897 ;; flags: qr rd ra; QUERY: 1, ANSWER: 13, AUTHORITY: 0, ADDITIONAL: 13 ;; QUESTION SECTION: ;. IN NS ;; ANSWER SECTION: . 268800 IN NS k.root-servers.net. . 268800 IN NS g.root-servers.net. . 268800 IN NS e.root-servers.net. . 268800 IN NS m.root-servers.net. . 268800 IN NS j.root-servers.net. . 268800 IN NS b.root-servers.net. . 268800 IN NS l.root-servers.net. . 268800 IN NS d.root-servers.net. . 268800 IN NS a.root-servers.net. . 268800 IN NS i.root-servers.net. . 268800 IN NS h.root-servers.net. . 268800 IN NS f.root-servers.net. . 268800 IN NS c.root-servers.net. ;; ADDITIONAL SECTION: a.root-servers.net. 459833 IN A 198.41.0.4 a.root-servers.net. 506943 IN AAAA 2001:503:ba3e::2:30 b.root-servers.net. 506943 IN A 192.228.79.201 b.root-servers.net. 506943 IN AAAA 2001:500:84::b c.root-servers.net. 506943 IN A 192.33.4.12 c.root-servers.net. 506943 IN AAAA 2001:500:2::c d.root-servers.net. 506943 IN A 199.7.91.13 d.root-servers.net. 506943 IN AAAA 2001:500:2d::d e.root-servers.net. 506943 IN A 192.203.230.10 f.root-servers.net. 506943 IN A 192.5.5.241 f.root-servers.net. 506943 IN AAAA 2001:500:2f::f g.root-servers.net. 506943 IN A 192.112.36.4 h.root-servers.net. 506943 IN A 198.97.190.53 ;; Query time: 1 msec ;; SERVER: 202.119.230.8#53(202.119.230.8) ;; WHEN: Thu Mar 31 12:24:27 2016 ;; MSG SIZE rcvd: 496
dig會向默認的上連DNS服務器查詢「.」(根域)的NS記錄。服務器
2、dig加一個點dom
$ dig . ; <<>> DiG 9.8.3-P1 <<>> . ;; global options: +cmd ;; Got answer: ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 39726 ;; flags: qr rd ra; QUERY: 1, ANSWER: 0, AUTHORITY: 1, ADDITIONAL: 0 ;; QUESTION SECTION: ;. IN A ;; AUTHORITY SECTION: . 8569 IN SOA a.root-servers.net. nstld.verisign-grs.com. 2016033001 1800 900 604800 86400 ;; Query time: 0 msec ;; SERVER: 202.119.230.8#53(202.119.230.8) ;; WHEN: Thu Mar 31 12:26:25 2016 ;; MSG SIZE rcvd: 92
3、使用Google DNS查詢baidu.com的A記錄tcp
$ dig @8.8.8.8 www.baidu.com A ; <<>> DiG 9.8.3-P1 <<>> @8.8.8.8 www.baidu.com A ; (1 server found) ;; global options: +cmd ;; Got answer: ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 23814 ;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 0 ;; QUESTION SECTION: ;www.baidu.com. IN A ;; ANSWER SECTION: www.baidu.com. 536 IN CNAME www.a.shifen.com. www.a.shifen.com. 299 IN A 119.75.218.70 www.a.shifen.com. 299 IN A 119.75.217.109 ;; Query time: 371 msec ;; SERVER: 8.8.8.8#53(8.8.8.8) ;; WHEN: Thu Mar 31 12:27:57 2016 ;; MSG SIZE rcvd: 90
dig的基本命令格式:dig
@dnsserver name querytype
工具
若是你設置的dnsserver是一個域名,那麼dig會首先經過默認的上連DNS服務器去查詢對應的IP地址,而後再以設置的dnsserver爲上連DNS服務器。
若是你沒有設置@dnsserver,那麼dig就會依次使用/etc/resolv.conf裏的地址做爲上連DNS服務器。
而對於querytype,若是你看過我上一篇有關nslookup命令的講解,那麼你應該對querytype有所瞭解,你能夠設置A/AAAA/PTR/MX/ANY等值,默認是查詢A記錄。spa
4、一些經常使用選項.net
-c,設置協議類型(class),包括IN(默認)、CH和HS。code
-f,支持從一個文件中讀取內容進行批量查詢。orm
-4和-6,用於設置使用哪一種傳輸協議(IPv4/IPv6)進行查詢。
-t,設置查詢的類型,默認A。
-x,逆向查詢選項,查詢IP地址到域名的映射關係。
$ cat querylist //文件內容,共有兩個域名須要查詢 www.baidu.com www.sohu.com $ dig -f querylist -c IN -t A//設置-f參數開始批量查詢 ; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.10.rc1.el6_3.2 <<>> www.baidu.com ;; global options: +cmd ;; Got answer: ;; ->>HEADER<> DiG 9.8.2rc1-RedHat-9.8.2-0.10.rc1.el6_3.2 <<>> www.sohu.com ;; Got answer: ;; ->>HEADER<</code>
5、dig特有查詢選項
1.TCP代替UDP
dig +tcp www.baidu.com ; <<>> DiG 9.8.3-P1 <<>> +tcp www.baidu.com ;; global options: +cmd ;; Got answer: ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 65322 ;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 5, ADDITIONAL: 5
2.默認追加域
dig +domain=baidu.com image ; <<>> DiG 9.8.3-P1 <<>> +domain=baidu.com image ;; global options: +cmd ;; Got answer: ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 26514 ;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 5, ADDITIONAL: 5
3.跟蹤dig全過程
$ dig +trace ww.baidu.com ; <<>> DiG 9.8.3-P1 <<>> +trace ww.baidu.com ;; global options: +cmd . 267154 IN NS d.root-servers.net. . 267154 IN NS h.root-servers.net. . 267154 IN NS e.root-servers.net. . 267154 IN NS i.root-servers.net. . 267154 IN NS b.root-servers.net. . 267154 IN NS k.root-servers.net. . 267154 IN NS m.root-servers.net. . 267154 IN NS g.root-servers.net. . 267154 IN NS f.root-servers.net. . 267154 IN NS a.root-servers.net. . 267154 IN NS c.root-servers.net. . 267154 IN NS l.root-servers.net. . 267154 IN NS j.root-servers.net. ;; Received 496 bytes from 202.119.230.8#53(202.119.230.8) in 4 ms //從本地DNS查找到根域DNS列表 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. ;; Received 490 bytes from 2001:7fd::1#53(2001:7fd::1) in 10233 ms //選擇了b.root-servers.net這臺根域DNS來查找cn.域DNS列表 baidu.com. 172800 IN NS dns.baidu.com. baidu.com. 172800 IN NS ns2.baidu.com. baidu.com. 172800 IN NS ns3.baidu.com. baidu.com. 172800 IN NS ns4.baidu.com. baidu.com. 172800 IN NS ns7.baidu.com. ;; Received 200 bytes from 192.55.83.30#53(192.55.83.30) in 422 ms //選擇了c.dns.cn這臺cn.域DNS服務器來查找roclinux.cn的DNS列表 ww.baidu.com. 7200 IN CNAME ps_other.a.shifen.com. a.shifen.com. 1200 IN NS ns4.a.shifen.com. a.shifen.com. 1200 IN NS ns5.a.shifen.com. a.shifen.com. 1200 IN NS ns1.a.shifen.com. a.shifen.com. 1200 IN NS ns3.a.shifen.com. a.shifen.com. 1200 IN NS ns2.a.shifen.com. ;; Received 232 bytes from 220.181.38.10#53(220.181.38.10) in 40 ms
dig很是著名的一個查詢選項就是+trace,當使用這個查詢選項後,dig會從根域查詢一直跟蹤直到查詢到最終結果,並將整個過程信息輸出出來。
4.精簡dig輸出
(1)+nocmd,節省輸出dig版本信息
(2)+short,輸出最簡的CHAME和A記錄,其餘不輸出
(3)+nocomment,節省輸出dig的詳細註釋信息
(4)+nostat,最終統計信息不輸出