首页 > 代码库 > linux命令行学习-dig(DNS查询器)

linux命令行学习-dig(DNS查询器)

在web开发中,总要熟悉的就是http协议,而发起一个http开始前最先要经历的一个过程就是DNS解析。简单说就是域名如何最终解析到实际服务器ip的过程。

而在研究DNS解析和排除DNS解析类故障问题的时候一个强大的工具就是dig。和他对应的有个比较传统的命令nslookup,不过dig作为更新的命令,其强大而简洁可定制的输出也使得运维其开发人员使用。

 实例1(访问服务器本地DNSserver查询根域名服务器):  ~#: dig

 ; <<>> DiG 9.3.6-P1-RedHat-9.3.6-20.P1.el5_8.6 <<>>
;; global options:  printcmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 39027
;; flags: qr rd ra; QUERY: 1, ANSWER: 13, AUTHORITY: 0, ADDITIONAL: 13


;; QUESTION SECTION:
;. IN NS

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



实例2(百度首页DNS解析): dig www.baidu.com 

; <<>> DiG 9.3.6-P1-RedHat-9.3.6-20.P1.el5_8.6 <<>> www.baidu.com
;; global options:  printcmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 15372
;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 0

;; QUESTION SECTION:
;www.baidu.com. INA

;; ANSWER SECTION:
www.baidu.com. 641IN CNAMEwww.a.shifen.com.
www.a.shifen.com. 110IN A 180.97.33.107
www.a.shifen.com. 110IN A 180.97.33.108

;; Query time: 1 msec
;; SERVER: 192.168.100.52#53(192.168.100.52)
;; WHEN: Wed Aug 13 22:33:59 2014
;; MSG SIZE  rcvd: 90 


实例3(制定使用google域名服务器解析百度首页); dig @8.8.8.8 www.baidu.com

; <<>> DiG 9.3.6-P1-RedHat-9.3.6-20.P1.el5_8.6 <<>> @8.8.8.8 www.baidu.com
; (1 server found)
;; global options:  printcmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 44033
;; flags: qr rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 0

;; QUESTION SECTION:
;www.baidu.com. INA


;; ANSWER SECTION:
www.baidu.com. 557IN CNAMEwww.a.shifen.com.
www.a.shifen.com. 299IN A 180.76.3.151


;; Query time: 615 msec
;; SERVER: 8.8.8.8#53(8.8.8.8)
;; WHEN: Wed Aug 13 22:36:36 2014
;; MSG SIZE  rcvd: 74


实例4(使用+trace选项查看从根域名服务器到最终解析到ip的过程): dig www.baidu.com +trace

. 84899 IN NS f.root-servers.net.
. 84899 IN NS d.root-servers.net.
. 84899 IN NS e.root-servers.net.
. 84899 IN NS b.root-servers.net.
. 84899 IN NS c.root-servers.net.
. 84899 IN NS m.root-servers.net.
. 84899 IN NS k.root-servers.net.
. 84899 IN NS i.root-servers.net.
. 84899 IN NS j.root-servers.net.
. 84899 IN NS h.root-servers.net.
. 84899 IN NS g.root-servers.net.
. 84899 IN NS a.root-servers.net.
. 84899 IN NS l.root-servers.net.
;; Received 509 bytes from 192.168.100.52#53(192.168.100.52) in 1 ms

说明:从本地DNS服务器得到若干各根域名服务器


com. 172800 IN NS a.gtld-servers.net.
com. 172800 IN NS e.gtld-servers.net.
com. 172800 IN NS h.gtld-servers.net.
com. 172800 IN NS j.gtld-servers.net.
com. 172800 IN NS i.gtld-servers.net.
com. 172800 IN NS k.gtld-servers.net.
com. 172800 IN NS b.gtld-servers.net.
com. 172800 IN NS m.gtld-servers.net.
com. 172800 IN NS f.gtld-servers.net.
com. 172800 IN NS g.gtld-servers.net.
com. 172800 IN NS c.gtld-servers.net.
com. 172800 IN NS d.gtld-servers.net.
com. 172800 IN NS l.gtld-servers.net.
;; Received 503 bytes from 192.5.5.241#53(f.root-servers.net) in 39 ms
说明:选取f.root-servers.net根域名服务器查询com域名服务器

baidu.com. 172800IN NS dns.baidu.com.
baidu.com. 172800IN NS ns2.baidu.com.
baidu.com. 172800IN NS ns3.baidu.com.
baidu.com. 172800IN NS ns4.baidu.com.
baidu.com. 172800IN NS ns7.baidu.com.
;; Received 201 bytes from 192.5.6.30#53(a.gtld-servers.net) in 360 ms
说明:选取a.gtld-servers.net的com域名服务器解析baidu.com的域名服务器

www.baidu.com. 1200IN CNAMEwww.a.shifen.com.
a.shifen.com. 1200IN NS ns3.a.shifen.com.
a.shifen.com. 1200IN NS ns5.a.shifen.com.
a.shifen.com. 1200IN NS ns1.a.shifen.com.
a.shifen.com. 1200IN NS ns4.a.shifen.com.
a.shifen.com. 1200IN NS ns2.a.shifen.com.
;; Received 228 bytes from 202.108.22.220#53(dns.baidu.com) in 206 ms

说明:通过选择访问dns.baidu.com域名服务器解析到www.baidu.com作为www.a.shifen.com的别名;


于是我们再dig下www.a.shifen.com.

[root@local200-65 JakQuan]# dig www.a.shifen.com +short
180.97.33.108
180.97.33.107