DNS排错

DNS 排错工具

  • 以下工具全部来自 bind9-utils 包

host

  • 简单的 dns 检测工具

语法

host [-t type] name [SERVER]

范例

# 测试否能实现 DNS 解析
# host www.baidu.com
www.baidu.com is an alias for www.a.shifen.com.
www.a.shifen.com has address 39.156.66.18
www.a.shifen.com has address 39.156.66.14


#测试140.205.41.21这个DNS服务器能否直接将xiangzheng.vip的IP解析出来
# host xiangzheng.vip 140.205.41.21
Using domain server:
Name: 140.205.41.21
Address: 140.205.41.21#53
Aliases: 

xiangzheng.vip has address 8.140.166.135



host -t NS magedu.org 172.16.0.1
host -t soa magedu.org
host -t mx magedu.org
host -t axfr magedu.org
host 1.2.3.4

dig

  • 专业的 dns 检测工具
  • dig只用于测试dns系统,不会查询hosts文件进行解析

语法

dig [-t type] name [@SERVER] [query options]
query options:
 +[no]trace:跟踪解析过程 : dig +trace magedu.org
 +[no]recurse:进行递归解析

范例

# 测试否能实现 DNS 解析
dig xiangzheng.vip


#测试140.205.41.21这个DNS服务器能否直接将xiangzheng.vip的IP解析出来,aa表示可以(权威DNS服务器)
dig xiangzheng.vip @140.205.41.21


#查看域名或IP对应的邮件服务器地址
dig -t mx 邮件服务器的域名或IP


#抓取目标DNS的所有记录(对安全加固的DNS服务器无效)
[root@client ~]# dig -t axfr xiangzheng.com @10.0.0.103

; <<>> DiG 9.11.36-RedHat-9.11.36-3.el8 <<>> -t axfr xiangzheng.com @10.0.0.103
;; global options: +cmd
xiangzheng.com.		600	IN	SOA	ns1.xiangzheng.com. mail.xiangzheng.com. 2015042201 3600 600 86400 43200
xiangzheng.com.		600	IN	NS	ns1.xiangzheng.com.
xiangzheng.com.		600	IN	NS	ns2.xiangzheng.com.
ns1.xiangzheng.com.	600	IN	A	10.0.0.103
ns2.xiangzheng.com.	600	IN	A	10.0.0.104
xiangzheng.com.		600	IN	SOA	ns1.xiangzheng.com. mail.xiangzheng.com. 2015042201 3600 600 86400 43200
;; Query time: 1 msec
;; SERVER: 10.0.0.103#53(10.0.0.103)
;; WHEN: Tue Jul 05 20:49:42 CST 2022
;; XFR size: 6 records (messages 1, bytes 216)



#测试反向解析
dig -x IP = dig –t ptr reverseip.in-addr.arpa

#模拟区域传送
dig -t axfr ZONE_NAME @SERVER
dig -t axfr magedu.org @10.10.10.11
dig –t axfr 100.1.10.in-addr.arpa @172.16.1.1
dig -t NS . @114.114.114.114
dig -t NS . @a.root-servers.net

nslookup

  • dns 检测工具
  • 此工具 Windows 也支持

语法

#命令格式
nslookup [-option] [name | -] [server]


#交互模式
 nslookup>
 server IP: 指明使用哪个DNS server进行查询
 set q=RR_TYPE: 指明查询的资源记录类型
 NAME: 要查询的名称

范例

# 测试否能实现 DNS 解析
# nslookup xiangzheng.vip
Server:		223.5.5.5 #DNS服务器的地址
Address:	223.5.5.5#53

Non-authoritative answer:
Name:	xiangzheng.vip
Address: 8.140.166.135


#测试140.205.41.21这个DNS服务器能否直接将xiangzheng.vip的IP解析出来
# nslookup xiangzheng.vip 140.205.41.21
Server:		140.205.41.21 #DNS服务器的地址
Address:	140.205.41.21#53

Name:	xiangzheng.vip
Address: 8.140.166.135

rndc

  • 利用rndc工具可以实现管理DNS功能
  • rndc 监听端口: 953/tcp

语法

# 语法
rndc COMMAND


# COMMAND:
 status #查看状态
 reload #重载主配置文件和区域解析库文件
 reload zonename #重载区域解析库文件
 retransfer zonename #手动启动区域传送,而不管序列号是否增加
 notify zonename #重新对区域传送发通知
 reconfig #重载主配置文件
 querylog #开启或关闭查询日志文件/var/log/message
 trace #递增debug一个级别
 trace LEVEL #指定使用的级别
 notrace #将调试级别设置为 0
 flush#清空DNS服务器的所有缓存记录

Ubuntu查看DNS

resolvectl status
#或
systemd-resolve --status

-

DNS排错

范例1

dig A example.com

; <<>> DiG 9.9.4-RedHat-9.9.4-14.el7 <<>> A example.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 30523

...

SERVFAIL:The nameserver encountered a problem while processing the query.

可使用dig +trace排错,可能是网络和防火墙导致

NXDOMAIN:The queried name does not exist in the zone.

可能是CNAME对应的A记录不存在导致

REFUSED:The nameserver refused the client’s DNS request due to policy restrictions.

可能是DNS策略导致

范例2

dig A example.com
; <<>> DiG 9.9.4-RedHat-9.9.4-14.el7 <<>> A example.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 30523
...
SERVFAIL:The nameserver encountered a problem while processing the query.

可使用dig +trace排错,可能是网络和防火墙导致

NXDOMAIN:The queried name does not exist in the zone.

可能是CNAME对应的A记录不存在导致

REFUSED:The nameserver refused the client’s DNS request due to policy restrictions.

可能是DNS策略导致

-

DNS故障排错

自建DNS无法解析

  • 10.0.0.8:DNS服务端
  • 10.0.0.18:客户端
#客户端指向本地DNS服务器
[root@client ~]# cat /etc/resolv.conf 
# Generated by NetworkManager
nameserver 10.0.0.8

#客户端访问百度显示名称或服务未知(其实这种问题绝大多数都是DNS服务器出现问题)
[root@client ~]# ping www.baidu.com
ping: www.baidu.com: Name or service not known

#DNS服务端抓包,显示只有10.0.0.18的请求包,而DNS没有相应包,初步判断是DNS服务器的问题
#正常port前需要加tcp或udp协议,但dns是使用了tcp和udp两个传输协议,所以不用加
[root@DNS ~]# tcpdump -i eth0 port 53 -vv
dropped privs to tcpdump
tcpdump: listening on eth0, link-type EN10MB (Ethernet), capture size 262144 bytes
12:01:08.187695 IP (tos 0x0, ttl 64, id 44798, offset 0, flags [DF], proto UDP (17), length 59)
    10.0.0.18.53249 > DNS.domain: [udp sum ok] 54556+ A? www.baidu.com. (31)
12:01:08.187821 IP (tos 0x0, ttl 64, id 44799, offset 0, flags [DF], proto UDP (17), length 59)
    10.0.0.18.53249 > DNS.domain: [udp sum ok] 57124+ AAAA? www.baidu.com. (31)
12:01:08.189138 IP (tos 0x0, ttl 64, id 44800, offset 0, flags [DF], proto UDP (17), length 59)
    10.0.0.18.53480 > DNS.domain: [udp sum ok] 54556+ A? www.baidu.com. (31)
12:01:08.189196 IP (tos 0x0, ttl 64, id 44801, offset 0, flags [DF], proto UDP (17), length 59)
    10.0.0.18.53480 > DNS.domain: [udp sum ok] 57124+ AAAA? www.baidu.com. (31)
    
#客户端在用telnet测试,显示连接被拒绝
[root@client ~]# telnet 10.0.0.8 53
Trying 10.0.0.8...
telnet: connect to address 10.0.0.8: Connection refused

#最后发现DNS只监听在了本机的127.0.0.1
[root@DNS ~]# ss -ntl
tcp   LISTEN    0     10            127.0.0.1:53       0.0.0.0:*
udp   LISTEN    0     10            127.0.0.1:53       0.0.0.0:*


#最后修改DNS配置文件的监听地址和允许使用DNS的主机, 重启dns服务问题解决
[root@DNS ~]# cat /etc/named.conf
	listen-on port 53 { localhost; };
	allow-query       { 10.0.0.0/24; }; #允许哪些IP使用此DNS
[root@DNS ~]# systemctl restart named
[root@client ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0 
...
DNS1=10.0.0.8
[root@client ~]# cat /etc/resolv.conf 
# Generated by NetworkManager
nameserver 10.0.0.8


#最后测试可以访问
[root@client ~]# ping www.baidu.com
PING www.a.shifen.com (39.156.66.18) 56(84) bytes of data.
64 bytes from 39.156.66.18 (39.156.66.18): icmp_seq=1 ttl=128 time=32.7 ms

-

DNS问题汇总

解析记录冲突规则

  • 参考文档:https://help.aliyun.com/document_detail/39787.html?spm=a2c4g.11186623.0.0.62df191bFs7VzY