dig命令

介绍

简介:dig命令是一个域名查询工具,可以根据域名查询对应的IP,或者根据IP查询对应的域名等其它功能。

适用范围:RedHat、RHEL、Ubuntu、CentOS、Fedora

语法

  1. 1 dig [选项] (参数)

选项

参数选项

  1. 1 @<服务器地址>:指定进行域名解析的域名服务器;
  2. 2 -b<ip地址>:当主机具有多个IP地址,指定使用本机的哪个IP地址向域名服务器发送域名查询请求;
  3. 3 -f<文件名称>:指定dig以批处理的方式运行,指定的文件中保存着需要批处理查询的DNS任务信息;
  4. 4 -P:指定域名服务器所使用端口号;
  5. 5 -t<类型>:指定要查询的DNS数据类型;
  6. 6 -x<IP地址>:执行逆向域名查询;
  7. 7 -4:使用IPv4
  8. 8 -6:使用IPv6
  9. 9 -h:显示指令帮助信息。

查询选项

  • 前面加no表示不输出对应内容 | 选项 | 说明 | | —- | —- | | +[no]cmd | 标识dig版本和已修改的查询选项 | | +[no]short | 提供一个简洁的答案。默认情况下,以详细的形式打印答案。 | | +[no]all | 设置或清除所有显示标志。 | | +[no]answer | 显示[不显示]应答的应答部分。默认设置是显示它。 |

使用

1.正向解析

  1. [root@master ~]# dig www.baidu.com
  2. ; <<>> DiG 9.9.4-RedHat-9.9.4-14.el7 <<>> www.baidu.com
  3. ;; global options: +cmd
  4. ;; Got answer:
  5. ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 26110
  6. ;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 1
  7. ;; OPT PSEUDOSECTION:
  8. ; EDNS: version: 0, flags:; udp: 1280
  9. ;; QUESTION SECTION:
  10. ;www.baidu.com. IN A
  11. ;; ANSWER SECTION:
  12. www.baidu.com. 228 IN CNAME www.a.shifen.com.
  13. www.a.shifen.com. 228 IN A 14.215.177.38
  14. www.a.shifen.com. 228 IN A 14.215.177.39
  15. ;; Query time: 3 msec
  16. ;; SERVER: 192.168.50.1#53(192.168.50.1)
  17. ;; WHEN: 1 02 12:04:28 CST 2022
  18. ;; MSG SIZE rcvd: 104

2.使用指定的DNS服务器查询

  1. [root@master ~]# dig @202.103.24.68 www.baidu.com
  2. ; <<>> DiG 9.9.4-RedHat-9.9.4-14.el7 <<>> @202.103.24.68 www.baidu.com
  3. ; (1 server found)
  4. ;; global options: +cmd
  5. ;; Got answer:
  6. ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 41548
  7. ;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 5, ADDITIONAL: 5
  8. ;; QUESTION SECTION:
  9. ;www.baidu.com. IN A
  10. ;; ANSWER SECTION:
  11. www.baidu.com. 600 IN CNAME www.a.shifen.com.
  12. www.a.shifen.com. 600 IN A 14.215.177.38
  13. www.a.shifen.com. 600 IN A 14.215.177.39
  14. ;; AUTHORITY SECTION:
  15. a.shifen.com. 1081 IN NS ns3.a.shifen.com.
  16. a.shifen.com. 1081 IN NS ns2.a.shifen.com.
  17. a.shifen.com. 1081 IN NS ns4.a.shifen.com.
  18. a.shifen.com. 1081 IN NS ns1.a.shifen.com.
  19. a.shifen.com. 1081 IN NS ns5.a.shifen.com.
  20. ;; ADDITIONAL SECTION:
  21. ns1.a.shifen.com. 271 IN A 110.242.68.42
  22. ns2.a.shifen.com. 1081 IN A 220.181.33.32
  23. ns3.a.shifen.com. 293 IN A 112.80.255.253
  24. ns4.a.shifen.com. 1081 IN A 14.215.177.229
  25. ns5.a.shifen.com. 140 IN A 180.76.76.95
  26. ;; Query time: 5 msec
  27. ;; SERVER: 202.103.24.68#53(202.103.24.68)
  28. ;; WHEN: 1 02 12:01:19 CST 2022
  29. ;; MSG SIZE rcvd: 260

3.仅显示权威应答

  1. [root@master ~]# dig +noall @202.103.24.68 www.baidu.com +authority
  2. a.shifen.com. 1198 IN NS ns2.a.shifen.com.
  3. a.shifen.com. 1198 IN NS ns1.a.shifen.com.
  4. a.shifen.com. 1198 IN NS ns3.a.shifen.com.
  5. a.shifen.com. 1198 IN NS ns5.a.shifen.com.
  6. a.shifen.com. 1198 IN NS ns4.a.shifen.com.

4.反向解析

  1. [root@master ~]# dig -x 114.114.114.114 +short
  2. public1.114dns.com.

5.查询CNAME记录

  1. [root@master ~]# dig +nocmd +noall cloud.tencent.com CNAME +answer
  2. cloud.tencent.com. 2977 IN CNAME cloud.tencent-cloud.com.
  3. cloud.tencent-cloud.com. 560 IN CNAME cloud.tencent.com.dsa.dnsv1.com.
  4. cloud.tencent.com.dsa.dnsv1.com. 560 IN CNAME 2794037.sched.d0-dk.tdnsv5.com.

6.查询邮件交换记录

  1. [root@master ~]# dig +nocmd qq.com -t mx +noall +answer
  2. qq.com. 5389 IN MX 10 mx3.QQ.COM.
  3. qq.com. 5389 IN MX 30 mx1.QQ.COM.
  4. qq.com. 5389 IN MX 20 mx2.QQ.COM.

7.查询TXT记录

  1. [root@master ~]# dig +nocmd qq.com -t txt +noall +answer +short
  2. qq.com. 4891 IN TXT "v=spf1 include:spf.mail.qq.com -all"
  3. [root@master ~]# dig +nocmd qq.com -t txt +noall +answer +short
  4. "v=spf1 include:spf.mail.qq.com -all"

参考