解决 Linux curl 报错 curl 6 Could not resolve host DNS 域名解析失败修复
排查 Linux 执行 curl 抛出 curl: (6) Could not resolve host: example.com 错误,配置 /etc/resolv.conf 与排查 systemd-resolved 状态。
问题现象
在服务器上请求外部域名时,命令瞬间报错返回:
curl: (6) Could not resolve host: api.example.com
系统所有需要通过域名访问公网依赖的脚本与服务全面瘫痪。
触发根因
/etc/resolv.conf中配置的上游 DNS 服务器(Nameserver)地址失效或网络不通;- Ubuntu / Debian 系统内置的
systemd-resolved缓存服务挂死,无法响应本地127.0.0.53循环请求; - 域名真实不存在、拼写错误或其权威权威 DNS 发生故障。
解决步骤
1. 检查并为 /etc/resolv.conf 配置可靠的公共上游 DNS
在配置文件中写入全球公共 DNS 备用地址:
# /etc/resolv.conf
nameserver 1.1.1.1
nameserver 8.8.8.8
nameserver 114.114.114.114
2. 重启 systemd-resolved 并刷新系统 DNS 高速缓存
针对采用 systemd-resolved 管理的现代 Linux 发行版:
sudo systemctl restart systemd-resolved
sudo resolvectl flush-caches
3. 使用 dig 或 nslookup 单独探测目标域名
直接向特定公共 DNS 询问域名解析以排除域名本身解析配置问题:
dig @8.8.8.8 api.example.com +short
验证确认
重新运行 curl 命令:
curl -I https://api.example.com/
控制台成功打印解析后的目标 IP 与 HTTP 返回状态行,域名解析顺畅无误。


