Just Do IT !

解决Centos系统设置静态ip时报错 ping: www.baidu.com: Name or service not known

字数统计: 757阅读时长: 4 min
2020/07/05 Share

具体设置静态IP可以查看我这篇文章:
Centos7下NAT设置静态ip

问题

设置静态以后发现 ==ping: www.baidu.com: Name or service not known==

但是ping网关192.168.233.2,DNS服务器8.8.8.8与114.114.114.114都能ping通

并且设置完静态显示正常 Xshell也可以正常连接

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
[root@localhost ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:15:b8:04 brd ff:ff:ff:ff:ff:ff
inet 192.168.233.128/24 brd 192.168.233.255 scope global noprefixroute ens33
valid_lft forever preferred_lft forever
[root@localhost ~]# ping 8.8.8.8
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=3 ttl=128 time=48.4 ms
64 bytes from 8.8.8.8: icmp_seq=9 ttl=128 time=47.0 ms
64 bytes from 8.8.8.8: icmp_seq=10 ttl=128 time=46.7 ms
^C
--- 8.8.8.8 ping statistics ---
10 packets transmitted, 3 received, 70% packet loss, time 9006ms
rtt min/avg/max/mdev = 46.738/47.412/48.467/0.776 ms
[root@localhost ~]# ping 114.114.114.114
PING 114.114.114.114 (114.114.114.114) 56(84) bytes of data.
64 bytes from 114.114.114.114: icmp_seq=1 ttl=128 time=26.7 ms
64 bytes from 114.114.114.114: icmp_seq=2 ttl=128 time=26.4 ms
64 bytes from 114.114.114.114: icmp_seq=3 ttl=128 time=24.9 ms

修改/etc/resolv.conf文件也无果

1
2
3
4
5
6
7
8
9
10
[root@localhost ~]# cat /etc/resolv.conf 
# Generated by NetworkManager
nameserver 8.8.8.8
nameserver 114.114.114.114
nameserver 192.168.233.2
[root@localhost ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.233.2 0.0.0.0 UG 100 0 0 ens33
192.168.233.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33

解决

解决DNS解析错误问题无果后 尝试使用dhclient命令分配dhcp地址

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@localhost ~]# dhclient
[root@localhost ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:15:b8:04 brd ff:ff:ff:ff:ff:ff
inet 192.168.233.128/24 brd 192.168.233.255 scope global noprefixroute ens33
valid_lft forever preferred_lft forever
inet 192.168.233.129/24 brd 192.168.233.255 scope global secondary dynamic ens33
valid_lft 1770sec preferred_lft 1770sec

可以发现运行完dhclient命令后出现了
inet 192.168.233.129/24 brd 192.168.233.255 scope global secondary dynamic ens33 valid_lft 1770sec preferred_lft 1770sec
这一行代表网卡被分配了额外的dhcp地址 现在进行ping www.baidu.com

1
2
3
4
5
6
7
8
9
10
[root@localhost ~]# ping www.baidu.com
PING www.a.shifen.com (220.181.38.150) 56(84) bytes of data.
64 bytes from 220.181.38.150: icmp_seq=1 ttl=128 time=4.80 ms
64 bytes from 220.181.38.150: icmp_seq=2 ttl=128 time=5.43 ms
64 bytes from 220.181.38.150: icmp_seq=3 ttl=128 time=8.02 ms
64 bytes from 220.181.38.150: icmp_seq=4 ttl=128 time=5.12 ms
^C
--- www.a.shifen.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3005ms
rtt min/avg/max/mdev = 4.809/5.849/8.026/1.276 ms

推测dns目前由dhcp到的192.168.233.129地址解析

CATALOG
  1. 1. 问题
  2. 2. 解决