返回首页

DNS 故障排查实战指南 — 从本地解析到权威 DNS 全链路

📅 创建于 2026-05-29 🔄 更新于 2026-05-29 📝 558 字

来源:马哥Linux运维 | 发布日期:2026-05-25

DNS 故障排查实战指南

DNS 是逐级缓存、多层转发、分布式协作的系统。问题根因可能出在本地 resolver、递归 DNS、权威 DNS、域名注册商、CDN 调度等任何一环。排查核心:分阶段定位,从本地到远端,从客户端到服务器。

排障流程概览

第一阶段:是不是 DNS 问题?
  └─ ping 域名 vs ping IP → 域名不通 IP 通 → DNS 问题
第二阶段:本地 DNS 解析器排查
  └─ /etc/resolv.conf → 测试 nameserver 连通性
第三阶段:dig/nslookup 精细检测
  └─ 查询各记录类型 → 对比权威 vs 递归
第四阶段:缓存问题与传播验证
  └─ 本地缓存 → CDN 缓存 → TTL 剩余时间

第一阶段:确认是不是 DNS 问题

初步区分

DNS 问题和网络问题的核心区别:DNS 问题只影响域名解析,IP 直连应该正常。

# ① 先 ping 域名
ping -c 4 www.example.com

# ② 获取 IP
nslookup www.example.com

# ③ ping 这个 IP(IP 通说明不是网络问题)
ping -c 4 93.184.216.34
  • IP 通域名不通 → DNS 问题
  • IP 也不通 → 网络连通性问题或服务器不可达

第二阶段:本地 DNS 解析器排查

/etc/resolv.conf

cat /etc/resolv.conf
# nameserver 10.0.0.2     # 第一个优先
# nameserver 8.8.8.8      # 后备
# search example.com
# options timeout:2 attempts:3 rotate

关键配置项:

  • nameserver — DNS 服务器 IP,最多 3 个,按顺序尝试
  • search — 搜索域,补全短域名(如 ping webping web.example.com
  • timeout — 每次查询超时时间(秒)
  • attempts — 重试次数
  • rotate — 轮询多个 nameserver 而非固定顺序

第三阶段:dig/nslookup 精细检测

dig — 最推荐的 DNS 查询工具

# 基本查询(默认 A 记录)
dig www.example.com

# 指定 DNS 服务器查询(绕过本地 resolver)
dig @8.8.8.8 www.example.com

# 查询特定记录类型
dig www.example.com A
dig www.example.com AAAA
dig example.com MX
dig example.com NS
dig example.com CNAME

# 查看权威 DNS 服务器
dig example.com NS +short

# 追踪完整解析路径
dig +trace www.example.com

# 简略输出
dig www.example.com +short

nslookup — 快速验证

nslookup www.example.com
nslookup www.example.com 8.8.8.8   # 指定 DNS 服务器

失败类型速查

错误 含义 排查方向
SERVFAIL 权威 DNS 服务器内部错误 DNSSEC 验证失败、NS 宕机
NXDOMAIN 域名不存在 域名过期、配置错误
REFUSED 查询被拒绝 ACL 配置错误、防火墙拦截
TIMEOUT 无响应 DNS 服务器不可达、网络不通
NOERROR 空 Answer 记录类型不匹配 查 A 只有 AAAA 记录

常用工具链

工具 用途 安装包
dig 最推荐的详细 DNS 查询 bind-utils
nslookup 快速 DNS 查询 内置
host 简化 DNS 查询 bind-utils
resolvectl systemd-resolved 查询控制 systemd

第四阶段:缓存与传播验证

本地 DNS 缓存

# systemd-resolved 缓存
resolvectl statistics
sudo resolvectl flush-caches

# BIND/named 缓存
rndc status
rndc flush

# dnsmasq
systemctl restart dnsmasq

# 验证 TTL 剩余时间
dig www.example.com | grep "ANSWER SECTION" -A 5

传播验证(全球视角)

# 从不同地理位置 DNS 服务器查询
dig @8.8.8.8 www.example.com        # Google DNS
dig @1.1.1.1 www.example.com        # Cloudflare
dig @114.114.114.114 www.example.com # 国内 DNS
dig @223.5.5.5 www.example.com      # 阿里 DNS

CDN 域名问题

CDN 域名经常出现「时好时坏」——同域名不同次解析返回不同 IP,这是 CDN 智能调度,不是 DNS 故障。可通过以下方式确认:

# 多次查询对比
for i in 1 2 3 4 5; do
  dig @8.8.8.8 www.example.com +short
  sleep 1
done

# 直接解析到目标源站 IP,绕过 CDN(如果有源站 IP)
curl --resolve www.example.com:443:203.0.113.1 https://www.example.com/

常见生产场景

场景一:服务器能 ping 通 IP 但域名解析失败 → 检查 /etc/resolv.conf → 确认 nameserver 可达 → 检查本机 DNS 缓存

场景二:nslookup 返回 SERVFAIL → 用 dig @权威NS域名 A记录 直接查询权威 → 检查 DNSSEC 配置

场景三:刚刚迁移了服务器 IP,但用户仍解析到旧 IP → 降低 TTL 先行 → 等待传播 → 用全球 DNS 验证

场景四:邮件服务器提示 Host not founddig example.com MX 确认 MX 记录 → dig example.com A 确认 A 记录

场景五:微服务之间出现 Unknown host → 检查内网 DNS 服务状态 → 检查 CoreDNS Pod → 检查 Service 的 Endpoints

关联页面

页面关联点
k8s-coredns-custom-domain-resolutionK8s CoreDNS 自定义域名解析
network-troubleshooting-order网络排障七步法(含 DNS 步骤)
linux-essential-commands-referenceLinux 常用命令
nginx-502-504-connection-reset-guide502/504 排障(DNS 是排查步骤之一)