问题:
# 百度IP可以ping通
(venv) root@p006:/etc# ping 220.181.38.150
PING 220.181.38.150 (220.181.38.150) 56(84) bytes of data.
64 bytes from 220.181.38.150: icmp_seq=1 ttl=47 time=15.5 ms
64 bytes from 220.181.38.150: icmp_seq=2 ttl=47 time=14.6 ms
# 域名却ping不通
(venv) root@p006:/etc# ping www.baidu.com
ping: www.baidu.com: Name or service not known
解决:
查看 /etc/resolv.conf 显示 No nameservers found,添加 DNS 地址,重启服务
(venv) root@p006:/etc# vim /etc/resolv.conf
……
nameserver 114.114.114.114
(venv) root@p006:/etc# systemctl restart systemd-resolved
再尝试一下,ping正常 (其实我这里是不通的,只是这个问题常见,所以记录下)
那么域名还是 ping 不通,接着往下
(venv) root@p006:/etc# vi /etc/nsswitch.conf
hosts: files # 修改前
↓↓↓↓↓↓↓
hosts: files dns wins # 修改后(注意:wins 可不写)
这时就可以 ping 通域名了
(venv) root@p006:/etc# ping baidu.com
PING baidu.com (220.181.38.251) 56(84) bytes of data.
64 bytes from 220.181.38.251 (220.181.38.251): icmp_seq=1 ttl=47 time=15.0 ms
为防止重启失效,需要在此基础上关闭 networkmanager 服务
(venv) root@p006:/etc# service networkmanager stop # 临时关闭
(venv) root@p006:/etc# chkconfig NetworkManager off # 永久关闭
重启 (此步骤可忽略)
(venv) root@p006:/etc# reboot
(venv) root@p006:/etc# ping baidu.com
PING baidu.com (220.181.38.251) 56(84) bytes of data.
64 bytes from 220.181.38.251 (220.181.38.251): icmp_seq=1 ttl=47 time=15.0 ms
个人记录
tail -2 /etc/systemd/resolved.conf
echo "DNS=114.114.114.114" >> /etc/systemd/resolved.conf
systemctl restart systemd-resolved
systemctl enable systemd-resolved
mv /etc/resolv.conf /etc/resolv.conf.bak
ln -s /run/systemd/resolve/resolv.conf /etc/
tail -2 /etc/resolv.conf
ping -c 2 baidu.com