跳转至

dig 命令


2013-11-28 by dongnan

举个栗子

查看 mx 记录:

dig mx 163.com
; <<>> DiG 9.8.1-P1 <<>> mx 163.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 27783
;; flags: qr rd ra; QUERY: 1, ANSWER: 4, AUTHORITY: 6, ADDITIONAL: 9

;; QUESTION SECTION:
;163.com.            IN    MX

;; ANSWER SECTION:
163.com.        17427    IN    MX    10 163mx02.mxmail.netease.com.
163.com.        17427    IN    MX    10 163mx03.mxmail.netease.com.
163.com.        17427    IN    MX    50 163mx00.mxmail.netease.com.
163.com.        17427    IN    MX    10 163mx01.mxmail.netease.com.

设置超时参数

由于超时时间设置为1秒,所以没有得到结果:

dig @172.27.233.1  zongming.net +time=1

; <<>> DiG 9.8.1-P1 <<>> @172.27.233.1 zongming.net +time=1
; (1 server found)
;; global options: +cmd
;; connection timed out; no servers could be reached

简化输出信息

默认输出

dig ywwd.net

; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.37.rc1.el6_7.2 <<>> ywwd.net
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 56013
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0

;; QUESTION SECTION:
;ywwd.net.            IN    A

;; ANSWER SECTION:
ywwd.net.        3597    IN    A    Server_IP

;; Query time: 1 msec
;; SERVER: 202.106.0.20#53(202.106.0.20)
;; WHEN: Tue Dec 29 16:23:30 2015
;; MSG SIZE  rcvd: 42

简化信息 +nocmd +nocomment

dig +nocmd +nocomment ywwd.net

;ywwd.net.            IN    A
ywwd.net.        590    IN    A    Server_IP
;; Query time: 0 msec
;; SERVER: 127.0.1.1#53(127.0.1.1)
;; WHEN: Wed Oct 21 11:24:50 CST 2015
;; MSG SIZE  rcvd: 48

迭代解析

使用 +trace 参数开始迭代解析:

dig +trace ywwd.net

; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.37.rc1.el6_7.2 <<>> +trace ywwd.net
;; global options: +cmd      #13个根域名服务器地址
.            154709    IN    NS    e.root-servers.net.
.            154709    IN    NS    m.root-servers.net.
.            154709    IN    NS    f.root-servers.net.
.            154709    IN    NS    l.root-servers.net.
.            154709    IN    NS    h.root-servers.net.
.            154709    IN    NS    d.root-servers.net.
.            154709    IN    NS    a.root-servers.net.
.            154709    IN    NS    j.root-servers.net.
.            154709    IN    NS    b.root-servers.net.
.            154709    IN    NS    g.root-servers.net.
.            154709    IN    NS    k.root-servers.net.
.            154709    IN    NS    c.root-servers.net.
.            154709    IN    NS    i.root-servers.net.
;; Received 228 bytes from 202.106.0.20#53(202.106.0.20) in 94 ms
                                                    # 13个.net 域名服务器地址
net.            172800    IN    NS    b.gtld-servers.net.
net.            172800    IN    NS    f.gtld-servers.net.
net.            172800    IN    NS    e.gtld-servers.net.
net.            172800    IN    NS    k.gtld-servers.net.
net.            172800    IN    NS    j.gtld-servers.net.
net.            172800    IN    NS    l.gtld-servers.net.
net.            172800    IN    NS    a.gtld-servers.net.
net.            172800    IN    NS    d.gtld-servers.net.
net.            172800    IN    NS    g.gtld-servers.net.
net.            172800    IN    NS    m.gtld-servers.net.
net.            172800    IN    NS    c.gtld-servers.net.
net.            172800    IN    NS    i.gtld-servers.net.
net.            172800    IN    NS    h.gtld-servers.net.
;; Received 511 bytes from 192.36.148.17#53(192.36.148.17) in 112 ms
                                                     # ywwd.net 域名管理服务器地址
ywwd.net.        172800    IN    NS    f1g1ns1.dnspod.net.
ywwd.net.        172800    IN    NS    f1g1ns2.dnspod.net.
;; Received 237 bytes from 192.31.80.30#53(192.31.80.30) in 279 ms

ywwd.net.        3600    IN    A    103.56.115.37   #ywwd.net A记录
ywwd.net.        86400    IN    NS    f1g1ns1.dnspod.net.
ywwd.net.        86400    IN    NS    f1g1ns2.dnspod.net.
;; Received 106 bytes from 182.140.167.188#53(182.140.167.188) in 32 ms

请求流程:

  • dig 请求ISP DNS:202.106.0.20 并不知道 ywwd.netA记录是什么。
  • 通知 dig 去问根(root)域名服务器。
  • 根域名服务器仍然没有此记录,通知 dig 去问 .net的域名服务器。
  • .net 域名服务器仍然没有此记录,通知 dig 去问 f1g1ns1.dnspod.net(ywwd.net 域名管理服务器)。
  • 最后在 f1g1ns1.dnspod.net 域名服务器,找到了 ywwd.netA记录。

这种一问一答的解析方式,叫做迭代解析(Iterative resolution)。这种方式下,每个域名服务器只是给出它所知道的最佳答案。

递归解析

默认为递归解析

dig ywwd.net

; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.37.rc1.el6_7.5 <<>> ywwd.net
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 588
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0

;; QUESTION SECTION:
;ywwd.net.            IN    A

;; ANSWER SECTION:
ywwd.net.        3598    IN   A    103.56.xx.37

;; Query time: 2 msec
;; SERVER: 202.106.0.20#53(202.106.0.20)
;; WHEN: Wed Jan  6 11:06:26 2016
;; MSG SIZE  rcvd: 42

请求流程:

  • 上面示例的解析方式是递归解析(recursive resolution)。
  • 也是 dig 默认的请求方式,对ISP DNS:202.106.0.20 请求一个递归解析。
  • ISP DNS 负责去解析到最终答案,最后直接返回给 dig

通常情况下客户端(dig)对域名服务器发起的递归解析请求,而域名服务器对其他域名服务器的请求(如根域名服务器)是迭代解析请求。

命令帮助

NAME
       dig - DNS lookup utility

SYNOPSIS
       dig [@server] [-b address] [-c class] [-f filename] [-k filename] 
        [-m] [-p port#] [-q name] [-t type] [-v] [-x addr] [-y [hmac:]name:key] 
        [[-4] | [-6]] [name] [type] [class] [queryopt...]

DESCRIPTION
       dig is a flexible tool for interrogating DNS name servers. 
       It performs DNS lookups and displays the answers that are returned from the name server(s) that were queried.

QUERY OPTIONS
   +timeout=T
   Sets the timeout for a query to T seconds. The default timeout is 5 seconds. 
   An attempt to set T to less than 1 will result in a query timeout of 1 second being applied.

   +[no]cmd
       Toggles the printing of the initial comment in the output identifying 
       the version of dig and the query options that have been applied. This comment is printed by default.

   +[no]comments
       Toggle the display of comment lines in the output. The default is to print comments.

   +[no]trace
       Toggle tracing of the delegation path from the root name servers for the name being looked up. 
       Tracing is disabled by default. When tracing is enabled, dig makes iterative queries to resolve the
       name being looked up. It will follow referrals from the root servers, 
       showing the answer from each server that was used to resolve the lookup.

       If @server is also specified, it affects only the initial query for the root zone name servers.

       +dnssec is also set when +trace is set to better emulate the default queries from a nameserver.



回到页面顶部