美文网首页
Centos7 网络无法访问外网解决

Centos7 网络无法访问外网解决

作者: IvanGuan | 来源:发表于2021-06-09 11:44 被阅读0次

背景

此前部署了一套Centos7的虚拟机,但是无法上外网,下面简单记录排查,解决的过程。

检查网卡

看下在virtual box里面网卡部分有没有选择桥接模式,如果没有的话,改为桥接模式

修改域名解析地址

进入 /etc/resolv.conf, 添加一个谷歌解析器
nameserver 192.168.60.204
nameserver 192.168.60.202
nameserver 114.114.114.114

如果修改之后还不能访问外网,那么需要检查路由

路由检查

[root@An3Xtao ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
172.17.0.0      0.0.0.0         255.255.0.0     U     0      0        0 docker0
192.168.60.0    0.0.0.0         255.255.254.0   U     100    0        0 enp0s9
192.168.77.0    0.0.0.0         255.255.255.0   U     100    0        0 enp0s3
192.168.200.0   0.0.0.0         255.255.255.0   U     100    0        0 enp0s8

发现没有默认路由,所以需要添加缺省路由,一共有三种方式

1, route command

添加路由
[root@An3Xtao ~]# route add default gw 192.168.61.254  (dev enp0s9)  //dev 后面的可以省略
[root@An3Xtao ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         192.168.61.254  0.0.0.0         UG    0      0        0 enp0s9
172.17.0.0      0.0.0.0         255.255.0.0     U     0      0        0 docker0
192.168.60.0    0.0.0.0         255.255.254.0   U     100    0        0 enp0s9
192.168.77.0    0.0.0.0         255.255.255.0   U     100    0        0 enp0s3
192.168.200.0   0.0.0.0         255.255.255.0   U     100    0        0 enp0s8

[root@An3Xtao ~]# ping baidu.com
PING baidu.com (39.156.69.79) 56(84) bytes of data.
64 bytes from 39.156.69.79: icmp_seq=1 ttl=49 time=7.30 ms
64 bytes from 39.156.69.79: icmp_seq=2 ttl=49 time=7.47 ms

删除路由
[root@An3Xtao ~]# route del default gw 192.168.61.254
[root@An3Xtao ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
172.17.0.0      0.0.0.0         255.255.0.0     U     0      0        0 docker0
192.168.60.0    0.0.0.0         255.255.254.0   U     100    0        0 enp0s9
192.168.77.0    0.0.0.0         255.255.255.0   U     100    0        0 enp0s3
192.168.200.0   0.0.0.0         255.255.255.0   U     100    0        0 enp0s8

2, ip route command

添加路由
ip route add default via 192.168.61.254 dev enp0s9

删除路由
ip route del default

3, 通过配置文件修改

在配置文件中添加gateway
vim /etc/sysconfig/network-scripts/ifcfg-enp0s9
添加下面内容:
IPADDR=192.168.60.202
PREFIX=23
GATEWAY=192.168.61.254

然后执行下面命令使其生效,则default  会自动产生
nmcli con reload
nmcli con up enp0s9

相关文章

网友评论

      本文标题:Centos7 网络无法访问外网解决

      本文链接:https://www.haomeiwen.com/subject/cyceeltx.html