【Linux】CentOS 7.3 能ping通IP地址,但ping不通域名的问题
xixuefeng
Linux
2019-05-24 20:51:00
1,023 次浏览
【Linux】CentOS 7.3 能ping通IP地址,但ping不通域名的问题已关闭评论
1:环境
1 2 3 |
[root@msp ~]# cat /etc/redhat-release CentOS Linux release 7.3.1611 (Core) [root@msp ~]# |
2:现象:ping IP地址可以ping通,但是ping域名则显示如下现象
1 2 3 4 5 6 7 8 9 10 11 12 13 |
[root@msp ~]# ping www.baidu.com ping: www.baidu.com: Name or service not known [root@msp ~]# [root@msp ~]# ping 61.135.169.121 PING 61.135.169.121 (61.135.169.121) 56(84) bytes of data. 64 bytes from 61.135.169.121: icmp_seq=1 ttl=52 time=1.55 ms 64 bytes from 61.135.169.121: icmp_seq=2 ttl=52 time=2.03 ms 64 bytes from 61.135.169.121: icmp_seq=3 ttl=52 time=1.53 ms ^C --- 61.135.169.121 ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 2002ms rtt min/avg/max/mdev = 1.537/1.708/2.037/0.232 ms [root@msp ~]# |
3:缓解因无法解析域名,导致yum无法连接到服务器的问题
这个问题一直困扰着我,比如:用yum的时候,因为解析的都是域名,所以,根本就用不了,不过,通过修改/etc/hosts,添加IP和域名,则能“勉强”使用yum了,如下:
1 2 3 4 5 6 7 8 9 10 |
[root@mss ~]# cat /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 10.75.99.50 msp 192.168.31.200 mss 121.22.232.217 mirrors.aliyun.com 62.210.92.35 nginx.org 59.111.0.251 mirrors.163.com |
4:解决
网上搜的话,大部分说的是没有配置DNS,或者VMWare的网络设置等。这些问题在我的环境中都不存在。
最终解决问题的是/etc/nsswitch.conf这个配置文件
1 2 3 4 |
[root@msp ~]# cat /etc/nsswitch.conf|grep hosts #hosts: db files nisplus nis dns hosts: files myhostname [root@msp ~]# |
改为如下,也就是加上dns
1 2 3 4 |
[root@msp ~]# cat /etc/nsswitch.conf|grep hosts #hosts: db files nisplus nis dns hosts: files dns myhostname [root@msp ~]# |
不用重启,立即生效。
1 2 3 4 5 6 7 8 9 10 |
[root@msp ~]# ping www.baidu.com PING www.a.shifen.com (61.135.169.125) 56(84) bytes of data. 64 bytes from 61.135.169.125 (61.135.169.125): icmp_seq=1 ttl=52 time=1.73 ms 64 bytes from 61.135.169.125 (61.135.169.125): icmp_seq=2 ttl=52 time=1.71 ms 64 bytes from 61.135.169.125 (61.135.169.125): icmp_seq=3 ttl=52 time=1.75 ms ^C --- www.a.shifen.com ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 2003ms rtt min/avg/max/mdev = 1.715/1.735/1.754/0.015 ms [root@msp ~]# |