Linux dig command usage

DIG(Domain Information Groper)域名信息挖掘,是Linux上一个用来查询域名信息的很有用的命令。下面对DIG的用法举例如下:

1.简单用法

$dig apple.com

如果简单的dig一个域名,默认会查询该域名对应的A记录,也就是相应的IPV4的地址信息,如下在(ANSWER SECTION)所示,apple.com相关联的A记录有三条。

$ dig apple.com

; <<>> DiG 9.8.3-P1 <<>> apple.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 32044
;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 0

;; QUESTION SECTION:
;apple.com.         IN  A

;; ANSWER SECTION:
apple.com.      2583    IN  A   17.172.224.47
apple.com.      2583    IN  A   17.178.96.59
apple.com.      2583    IN  A   17.142.160.59

;; Query time: 51 msec
;; SERVER: 8.8.8.8#53(8.8.8.8)
;; WHEN: Wed Apr 12 11:32:12 2017
;; MSG SIZE  rcvd: 75

DIG命令的输出分析:

  • HEADER: 显示dig命令的版本信息,全局选项,和一些额外的头部信息
  • QUESTION SECTION: 显示我们向DNS(域名系统)发出的请求,就是我们向DNS发出的提问
  • ANSWER SECTION: 显示域名系统给我们响应输出,就是DNS给我们的回答
  • AUTHORITY SECTION: 显示有权限回应此次请求的域名服务器自身的域名
  • ADDITIONAL SECTION: 显示域名解析服务器的IP地址
  • STATS SECTION: 显示此次查询的一些数据信息,例如,时间戳,耗时等

2.只显示ANSWER SECTION内容

如果只想显示ANSWER SECTION的内容,你可以通过添加一些参数把其他的信息关掉

+nocomments – Turn off the comment lines
+noauthority – Turn off the authority section
+noadditional – Turn off the additional section
+nostats – Turn off the stats section
+noanswer – Turn off the answer section

下面的查询只显示ANSWER SECTION:

$ dig apple.com +nocomments +noquestion +noauthority +noadditional +nostats

; <<>> DiG 9.8.3-P1 <<>> apple.com +nocomments +noquestion +noauthority +noadditional +nostats
;; global options: +cmd
apple.com.      2027    IN  A   17.142.160.59
apple.com.      2027    IN  A   17.172.224.47
apple.com.      2027    IN  A   17.178.96.59

你也可以通过反选的方式,指定只想显示的信息:

$ dig apple.com +noall +answer

; <<>> DiG 9.8.3-P1 <<>> apple.com +noall +answer
;; global options: +cmd
apple.com.      1952    IN  A   17.142.160.59
apple.com.      1952    IN  A   17.172.224.47
apple.com.      1952    IN  A   17.178.96.59

3.只查询某条记录

如果只想查询MX记录,可以通过下面两种方式:

$ dig apple.com MX +noall +answer

; <<>> DiG 9.8.3-P1 <<>> apple.com MX +noall +answer
;; global options: +cmd
apple.com.      2460    IN  MX  10 ma1-aaemail-dr-lapp02.apple.com.
apple.com.      2460    IN  MX  10 ma1-aaemail-dr-lapp01.apple.com.
apple.com.      2460    IN  MX  10 nwk-aaemail-lapp03.apple.com.
apple.com.      2460    IN  MX  10 nwk-aaemail-lapp02.apple.com.
apple.com.      2460    IN  MX  10 nwk-aaemail-lapp01.apple.com.
apple.com.      2460    IN  MX  10 ma1-aaemail-dr-lapp03.apple.com.
$ dig -t MX apple.com +noall +answer

; <<>> DiG 9.8.3-P1 <<>> -t MX apple.com +noall +answer
;; global options: +cmd
apple.com.      2246    IN  MX  10 nwk-aaemail-lapp03.apple.com.
apple.com.      2246    IN  MX  10 nwk-aaemail-lapp02.apple.com.
apple.com.      2246    IN  MX  10 nwk-aaemail-lapp01.apple.com.
apple.com.      2246    IN  MX  10 ma1-aaemail-dr-lapp03.apple.com.
apple.com.      2246    IN  MX  10 ma1-aaemail-dr-lapp02.apple.com.
apple.com.      2246    IN  MX  10 ma1-aaemail-dr-lapp01.apple.com.

4.查询NS记录

如果只想查询NS记录,可以通过下面两种方式:

$ dig apple.com NS +noall +answer

; <<>> DiG 9.8.3-P1 <<>> apple.com NS +noall +answer
;; global options: +cmd
apple.com.      79319   IN  NS  nserver6.apple.com.
apple.com.      79319   IN  NS  nserver5.apple.com.
apple.com.      79319   IN  NS  adns1.apple.com.
apple.com.      79319   IN  NS  adns2.apple.com.
apple.com.      79319   IN  NS  nserver4.apple.com.
apple.com.      79319   IN  NS  nserver.apple.com.
apple.com.      79319   IN  NS  nserver3.apple.com.
apple.com.      79319   IN  NS  nserver2.apple.com.
$ dig -t NS apple.com +noall +answer

; <<>> DiG 9.8.3-P1 <<>> -t NS apple.com +noall +answer
;; global options: +cmd
apple.com.      71342   IN  NS  nserver6.apple.com.
apple.com.      71342   IN  NS  nserver5.apple.com.
apple.com.      71342   IN  NS  adns1.apple.com.
apple.com.      71342   IN  NS  adns2.apple.com.
apple.com.      71342   IN  NS  nserver4.apple.com.
apple.com.      71342   IN  NS  nserver.apple.com.
apple.com.      71342   IN  NS  nserver3.apple.com.
apple.com.      71342   IN  NS  nserver2.apple.com.

5.查询所有的DNS记录

如果要查询所有的DNS记录(A,MX,NS,AAAA等),可以用下面的方法:

$ dig apple.com ANY +noall +answer

;; Truncated, retrying in TCP mode.

; <<>> DiG 9.8.3-P1 <<>> apple.com ANY +noall +answer
;; global options: +cmd
apple.com.      2108    IN  A   17.142.160.59
apple.com.      2108    IN  A   17.172.224.47
apple.com.      2108    IN  A   17.178.96.59
apple.com.      2108    IN  TXT "v=spf1 ip4:17.0.0.0/8 -all"
apple.com.      2108    IN  TXT "google-site-verification=K-60dwNhM3vJSf7VklDuQvAynXRiw9yAdV10rpsWT4E"
apple.com.      84908   IN  NS  nserver2.apple.com.
apple.com.      84908   IN  NS  nserver6.apple.com.
apple.com.      84908   IN  NS  adns1.apple.com.
apple.com.      84908   IN  NS  nserver4.apple.com.
apple.com.      84908   IN  NS  nserver5.apple.com.
apple.com.      84908   IN  NS  nserver.apple.com.
apple.com.      84908   IN  NS  nserver3.apple.com.
apple.com.      84908   IN  NS  adns2.apple.com.
apple.com.      84908   IN  NAPTR   90 50 "se" "SIP+D2T" "" _sip._tcp.apple.com.
apple.com.      84908   IN  NAPTR   50 50 "se" "SIPS+D2T" "" _sips._tcp.apple.com.
apple.com.      84908   IN  NAPTR   100 50 "se" "SIP+D2U" "" _sip._udp.apple.com.
apple.com.      2108    IN  MX  10 nwk-aaemail-lapp03.apple.com.
apple.com.      2108    IN  MX  10 nwk-aaemail-lapp02.apple.com.
apple.com.      2108    IN  MX  10 nwk-aaemail-lapp01.apple.com.
apple.com.      2108    IN  MX  10 ma1-aaemail-dr-lapp03.apple.com.
apple.com.      2108    IN  MX  10 ma1-aaemail-dr-lapp02.apple.com.
apple.com.      2108    IN  MX  10 ma1-aaemail-dr-lapp01.apple.com.
apple.com.      84908   IN  SOA gridmaster-ib.apple.com. hostmaster.apple.com. 2010101622 900 900 2016000 86500```

#### 6.查询short信息(仅IP地址)

$ dig apple.com +short
17.142.160.59
17.172.224.47
17.178.96.59


当然你也可以指定要查询的记录类型

$ dig apple.com NS +short
nserver6.apple.com.
nserver5.apple.com.
adns1.apple.com.
adns2.apple.com.
nserver4.apple.com.
nserver.apple.com.
nserver3.apple.com.
nserver2.apple.com.


#### 7.查询IP对应的域名信息

$ dig -x 17.142.160.59
;; Truncated, retrying in TCP mode.

; <<>> DiG 9.8.3-P1 <<>> -x 17.142.160.59
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 22425
;; flags: qr rd ra; QUERY: 1, ANSWER: 47, AUTHORITY: 0, ADDITIONAL: 0

;; QUESTION SECTION:
;59.160.142.17.in-addr.arpa. IN PTR

;; ANSWER SECTION:
59.160.142.17.in-addr.arpa. 3599 IN PTR ripmixburn.com.
59.160.142.17.in-addr.arpa. 3599 IN PTR yessql.info.
59.160.142.17.in-addr.arpa. 3599 IN PTR webobjects.info.
59.160.142.17.in-addr.arpa. 3599 IN PTR ubnw.info.
59.160.142.17.in-addr.arpa. 3599 IN PTR skyvines.info.
59.160.142.17.in-addr.arpa. 3599 IN PTR shopdifferent.info.
59.160.142.17.in-addr.arpa. 3599 IN PTR sherlock.info.
59.160.142.17.in-addr.arpa. 3599 IN PTR quicktimetv.info.
59.160.142.17.in-addr.arpa. 3599 IN PTR quicktimelive.info.
59.160.142.17.in-addr.arpa. 3599 IN PTR powermac.info.
59.160.142.17.in-addr.arpa. 3599 IN PTR powerbook.info.
59.160.142.17.in-addr.arpa. 3599 IN PTR macosx.info.
59.160.142.17.in-addr.arpa. 3599 IN PTR appleshare.info.
59.160.142.17.in-addr.arpa. 3599 IN PTR applescript.info.
59.160.142.17.in-addr.arpa. 3599 IN PTR applepaysupplies.info.
59.160.142.17.in-addr.arpa. 3599 IN PTR applepaymerchantsupplies.info.
59.160.142.17.in-addr.arpa. 3599 IN PTR applepay.info.
59.160.142.17.in-addr.arpa. 3599 IN PTR applemasters.info.
59.160.142.17.in-addr.arpa. 3599 IN PTR appleexpo.info.
59.160.142.17.in-addr.arpa. 3599 IN PTR applecomputerinc.info.
59.160.142.17.in-addr.arpa. 3599 IN PTR applecentre.info.
59.160.142.17.in-addr.arpa. 3599 IN PTR airtunes.info.
59.160.142.17.in-addr.arpa. 3599 IN PTR apple.by.
59.160.142.17.in-addr.arpa. 3599 IN PTR apples-msk.ru.
59.160.142.17.in-addr.arpa. 3599 IN PTR icloud.se.
59.160.142.17.in-addr.arpa. 3599 IN PTR icloud.es.
59.160.142.17.in-addr.arpa. 3599 IN PTR icloud.om.
59.160.142.17.in-addr.arpa. 3599 IN PTR icloudo.com.
59.160.142.17.in-addr.arpa. 3599 IN PTR icloud.ch.
59.160.142.17.in-addr.arpa. 3599 IN PTR icloud.fr.
59.160.142.17.in-addr.arpa. 3599 IN PTR icloude.com.
59.160.142.17.in-addr.arpa. 3599 IN PTR camelspaceeffect.com.
59.160.142.17.in-addr.arpa. 3599 IN PTR camelphat.com.
59.160.142.17.in-addr.arpa. 3599 IN PTR alchemysynth.com.
59.160.142.17.in-addr.arpa. 3599 IN PTR openni.org.
59.160.142.17.in-addr.arpa. 3599 IN PTR swell.am.
59.160.142.17.in-addr.arpa. 3599 IN PTR appleweb.net.
59.160.142.17.in-addr.arpa. 3599 IN PTR apple.com.
59.160.142.17.in-addr.arpa. 3599 IN PTR pv-apple-com.apple.com.
59.160.142.17.in-addr.arpa. 3599 IN PTR ipad.host.
59.160.142.17.in-addr.arpa. 3599 IN PTR iphone.host.
59.160.142.17.in-addr.arpa. 3599 IN PTR chomp.com.
59.160.142.17.in-addr.arpa. 3599 IN PTR itunes.earth.
59.160.142.17.in-addr.arpa. 3599 IN PTR mac.one.
59.160.142.17.in-addr.arpa. 3599 IN PTR imac.one.
59.160.142.17.in-addr.arpa. 3599 IN PTR ipadpro.buzz.
59.160.142.17.in-addr.arpa. 3599 IN PTR airport.brussels.

;; Query time: 78 msec
;; SERVER: 8.8.8.8#53(8.8.8.8)
;; WHEN: Wed Apr 12 12:57:40 2017
;; MSG SIZE rcvd: 1207


我的皮皮虾都惊呆了,大厂的域名就是多呀

#### 8.指定域名服务器进行查询

当然你也可以在指定的域名服务器上面进行查询,你高兴就好:

先查一下APPLE有些什么域名服务器:

$ dig apple.com NS +short
nserver6.apple.com.
nserver5.apple.com.
adns1.apple.com.
adns2.apple.com.
nserver4.apple.com.
nserver.apple.com.
nserver3.apple.com.
nserver2.apple.com.


再选取其中一个,进行后续的查询:

$ dig adns1.apple.com. apple.com +short
17.151.0.151
17.172.224.47
17.178.96.59
17.142.160.59


#### 9.查询多个域名

你可以把要查询的信息写到文件里面,注意分行哦,然后执行dig -f  [option] 来进行查询:

$ vi domains.txt

apple.com
centos.org


接下来执行dig -f,就可以一次查询多个域名了,是不是很爽

$ dig -f domains.txt +noall +answer
apple.com. 114 IN A 17.172.224.47
apple.com. 114 IN A 17.178.96.59
apple.com. 114 IN A 17.142.160.59
centos.org. 43 IN A 85.12.30.226


当然你也可以这样写:

$ dig -f domains.txt MX +noall +answer
apple.com. 3178 IN MX 10 nwk-aaemail-lapp03.apple.com.
apple.com. 3178 IN MX 10 nwk-aaemail-lapp02.apple.com.
apple.com. 3178 IN MX 10 nwk-aaemail-lapp01.apple.com.
apple.com. 3178 IN MX 10 ma1-aaemail-dr-lapp03.apple.com.
apple.com. 3178 IN MX 10 ma1-aaemail-dr-lapp02.apple.com.
apple.com. 3178 IN MX 10 ma1-aaemail-dr-lapp01.apple.com.
centos.org. 1794 IN MX 20 mail2.centos.org.
centos.org. 1794 IN MX 10 mail.centos.org.


这样写:

$ dig apple.com MX +noall +answer centos.org NS +short
10 nwk-aaemail-lapp03.apple.com.
10 nwk-aaemail-lapp02.apple.com.
10 nwk-aaemail-lapp01.apple.com.
10 ma1-aaemail-dr-lapp03.apple.com.
10 ma1-aaemail-dr-lapp02.apple.com.
10 ma1-aaemail-dr-lapp01.apple.com.
ns4.centos.org.
ns3.centos.org.
ns1.centos.org.


#### 10.增加$HOME/.digrc文件存储默认查询选项

如果你的用户目录下面没有.digrc文件,新建一个,输入+noall +answer,相当于每次查询的时候都加上这两个参数,这样使用起来是不是清新爽朗的多了....

$ dig apple.com
apple.com. 3213 IN A 17.172.224.47
apple.com. 3213 IN A 17.178.96.59
apple.com. 3213 IN A 17.142.160.59


$ dig apple.com MX
apple.com. 3028 IN MX 10 nwk-aaemail-lapp03.apple.com.
apple.com. 3028 IN MX 10 nwk-aaemail-lapp02.apple.com.
apple.com. 3028 IN MX 10 nwk-aaemail-lapp01.apple.com.
apple.com. 3028 IN MX 10 ma1-aaemail-dr-lapp03.apple.com.
apple.com. 3028 IN MX 10 ma1-aaemail-dr-lapp02.apple.com.
apple.com. 3028 IN MX 10 ma1-aaemail-dr-lapp01.apple.com.



#### 好啦,DIG的使用就是这样啦,是不是很强大,很好用...

你可能感兴趣的:(Linux dig command usage)