0%

一次解决centos7设置固定ip之后无法上外网问题的记录

之前使用的是电信的网络,在房间里接了一个路由器,宿主机连接这个路由器,在虚拟机中装了centos,设置了固定ip可以正常上外网,详见:http://blog.kungge.com/p/linux-centos-network/

问题描述

搬家了,新住的地方使用的长城宽带,和原来一样接了一个二级路由器,宿主机可以正常上网,可以连上虚拟机centos,但是虚拟机centos却连不上外网,有时候能ping通,但是大部分时间都没法ping通。

1
2
[root@localhost dockerTest]# ping baidu.com
ping: baidu.com: Name or service not known

原先的网络配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE="Ethernet"
BOOTPROTO="static"
DEFROUTE="yes"
PEERDNS="yes"
PEERROUTES="yes"
IPV4_FAILURE_FATAL="no"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
IPV6_DEFROUTE="yes"
IPV6_PEERDNS="yes"
IPV6_PEERROUTES="yes"
IPV6_FAILURE_FATAL="no"
IPV6_ADDR_GEN_MODE="stable-privacy"
NAME="ens33"
UUID="208128e4-ad02-4f81-aef1-601f533b485f"
DEVICE="ens33"
ONBOOT="yes"
IPADDR="192.168.2.57"
GATEWAY="192.168.2.1"
NETMASK="255.255.255.0"
DNS="8.8.8.8"
DNS1="114.114.114.114"

问题解决

将网络配置修改如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
TYPE="Ethernet"
BOOTPROTO="static"
DEFROUTE="yes"
PEERDNS="yes"
PEERROUTES="yes"
IPV4_FAILURE_FATAL="no"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
IPV6_DEFROUTE="yes"
IPV6_PEERDNS="yes"
IPV6_PEERROUTES="yes"
IPV6_FAILURE_FATAL="no"
IPV6_ADDR_GEN_MODE="stable-privacy"
NAME="ens33"
UUID="208128e4-ad02-4f81-aef1-601f533b485f"
DEVICE="ens33"
ONBOOT="yes"
IPADDR="192.168.2.57"
GATEWAY="192.168.2.1"
NETMASK="255.255.255.0"
BROADCAST="192.168.2.255"
DNS1="192.168.2.1"

保存后,重启网卡 systemctl restart network,连接外网正常:

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@localhost ~]# ping kungge.com
PING kungge.com (119.3.74.185) 56(84) bytes of data.
64 bytes from ecs-119-3-74-185.compute.hwclouds-dns.com (119.3.74.185): icmp_seq=1 ttl=112 time=7.69 ms
64 bytes from ecs-119-3-74-185.compute.hwclouds-dns.com (119.3.74.185): icmp_seq=2 ttl=112 time=6.03 ms
64 bytes from ecs-119-3-74-185.compute.hwclouds-dns.com (119.3.74.185): icmp_seq=3 ttl=112 time=7.42 ms
64 bytes from ecs-119-3-74-185.compute.hwclouds-dns.com (119.3.74.185): icmp_seq=4 ttl=112 time=5.71 ms
64 bytes from ecs-119-3-74-185.compute.hwclouds-dns.com (119.3.74.185): icmp_seq=5 ttl=112 time=6.70 ms
64 bytes from ecs-119-3-74-185.compute.hwclouds-dns.com (119.3.74.185): icmp_seq=6 ttl=112 time=6.33 ms
64 bytes from ecs-119-3-74-185.compute.hwclouds-dns.com (119.3.74.185): icmp_seq=7 ttl=112 time=6.10 ms
64 bytes from ecs-119-3-74-185.compute.hwclouds-dns.com (119.3.74.185): icmp_seq=8 ttl=112 time=7.32 ms
64 bytes from ecs-119-3-74-185.compute.hwclouds-dns.com (119.3.74.185): icmp_seq=9 ttl=112 time=6.80 ms
64 bytes from ecs-119-3-74-185.compute.hwclouds-dns.com (119.3.74.185): icmp_seq=10 ttl=112 time=6.59 ms
...
您的支持是我最大的动力!