在 CentOS 上“域名报错”是一个比较笼统的说法,常见原因和对应排查方向如下,你可以对照看是哪一种情况。
ping: unknown host xxx.com原因:
/etc/resolv.conf 为空或被覆盖排查:
cat /etc/resolv.conf
应有类似:
nameserver 8.8.8.8
nameserver 114.114.114.114
如果没有,手动添加:
echo "nameserver 8.8.8.8" > /etc/resolv.conf
⚠️ 注意:
CentOS 7+ 若使用 NetworkManager,resolv.conf 可能被自动覆盖。
curl: (6) Could not resolve host原因:
排查:
nslookup www.baidu.com
dig www.baidu.com
原因:
CentOS 7 示例:
vi /etc/sysconfig/network-scripts/ifcfg-eth0
添加:
DNS1=8.8.8.8
DNS2=114.114.114.114
重启网络:
systemctl restart network
/etc/hosts 配置错误原因:
示例:
127.0.0.1 localhost
192.168.1.10 test.com
检查:
getent hosts test.com
原因:
解决:
nmcli con mod eth0 ipv4.dns "8.8.8.8 114.114.114.114"
nmcli con up eth0
systemctl stop firewalld
setenforce 0
(仅用于排查)
ip addr
ping 8.8.8.8
cat /etc/resolv.conf
nslookup 域名
systemctl status NetworkManager
例如:
cat /etc/centos-release)我可以给你精准解决方案。