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 <filename> [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的使用就是这样啦,是不是很强大,很好用...
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 214,444评论 6 496
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,421评论 3 389
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 160,036评论 0 349
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,363评论 1 288
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,460评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,502评论 1 292
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,511评论 3 412
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,280评论 0 270
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,736评论 1 307
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,014评论 2 328
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,190评论 1 342
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,848评论 5 338
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,531评论 3 322
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,159评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,411评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,067评论 2 365
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,078评论 2 352

推荐阅读更多精彩内容