解决 Linux 域名解析报错 Temporary failure in name resolution 排查指南
排查 Linux 系统使用 curl、apt 或 ping 时提示 Temporary failure in name resolution 域名解析超时,修复 resolv.conf 与 systemd-resolved。
问题现象
在 Linux 主机上执行网络请求、更新软件包或 ping 外部域名时,命令报错退出:
$ ping google.com
ping: google.com: Temporary failure in name resolution
或者使用 curl 访问网站:
$ curl https://api.github.com
curl: (6) Could not resolve host: api.github.com
但直接 ping 8.8.8.8(公网 IP)网络通信完全畅通无阻。
触发根因
Temporary failure in name resolution(EAI_AGAIN)表明系统的域名解析库(glibc getaddrinfo)尝试向配置的 DNS 服务器发送查询请求,但在等待超时前未收到有效回包。
核心成因包括:
/etc/resolv.conf软链接断裂或指向了失效的本地 stub resolver:现代 Ubuntu/Debian 系统默认使用systemd-resolved监听127.0.0.53:53,若该服务崩溃或被禁用,所有 DNS 查询均会超时。- 上游 DNS 服务器地址不可达或被拦截:配置的 DNS(如内网 DNS 服务器)离线,或者云安全组丢弃了出口 UDP/TCP 53 端口流量。
- NetworkManager 覆盖了 DNS 配置:每次网络重启后 DHCP 下发的无用 DNS 覆盖了手工配置。
解决步骤
1. 检查 /etc/resolv.conf 当前配置
查看当前文件内容:
cat /etc/resolv.conf
如果配置了:
nameserver 127.0.0.53
options edns0 trust-ad
测试该本地服务是否正在监听:
ss -tulpn | grep 127.0.0.53
如果无输出,检查 systemd-resolved 运行状态:
sudo systemctl status systemd-resolved
sudo systemctl restart systemd-resolved
2. 直接配置可靠的公共 DNS
如果不需要本地缓存解析服务,可以直接在 /etc/resolv.conf 中指定公共 DNS(如 1.1.1.1、8.8.8.8、223.5.5.5)。
如果 /etc/resolv.conf 是软链接,先将其解绑:
sudo rm -f /etc/resolv.conf
新建文件并写入标准 DNS 服务器配置:
# /etc/resolv.conf
nameserver 223.5.5.5
nameserver 8.8.8.8
options timeout:2 attempts:3
设置写保护防止被 DHCP 自动覆盖(可选):
sudo chattr +i /etc/resolv.conf
3. 配置 systemd-resolved 上游 DNS(针对标准系统)
如果保留 systemd-resolved,建议规范修改 /etc/systemd/resolved.conf:
[Resolve]
DNS=223.5.5.5 8.8.8.8
FallbackDNS=1.1.1.1 114.114.114.114
重新启动解析守护进程:
sudo systemctl restart systemd-resolved
4. 验证 DNS 出口连通性
测试宿主机是否能够正常向外部 53 端口收发 UDP 报文:
nc -zvu 8.8.8.8 53
若提示超时,检查云服务器出站安全组(Egress Rules)是否限制了 UDP 协议或 53 端口。
验证确认
使用 dig 或 nslookup 验证解析功能:
dig +short debug.wiki
输出顺利返回目标服务器 IP:
64.90.3.46
重新执行 ping google.com 或 curl 测试,域名瞬时解析成功,业务网络恢复通畅。


