背景
由于笔记本电脑的网络环境总是会变化,导致虚拟机的网络ip不能固定,因此我给虚拟机设置了两块网卡,一块是Host-only,名为enp0s3
,网段为192.168.137.0/24
;另一块是网络地址转换(NET),用于访问互联网,名为eth0
。
默认的设置会导致访问互联网时有时会走eth1
,导致无法上网。也就是路由表配置错误。
一开始的路由表如下
1 2 3 4 5 6 7
| $ route Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface default 192.168.137.1 0.0.0.0 UG 0 0 0 eth0 default 10.0.3.2 0.0.0.0 UG 100 0 0 eth1 10.0.3.0 0.0.0.0 255.255.255.0 U 100 0 0 eth1 192.168.137.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
|
可以看到有两个default
,导致上网会使用eth0
。可以输入sudo route del eth0 gw 192.168.137.1
临时解决。
解决方案
这里使用netplan
配置网络,在/etc/netplan/
下新建任意名称的yaml
文件,内容如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| network: version: 2 renderer: networkd ethernets: eth0: dhcp4: no addresses: [192.168.137.3/24] optional: true eth1: dhcp4: yes addresses: [] optional: true nameservers: addresses: [101.6.6.6,8.8.8.8]
|
然后,用命令sudo netplan apply
就可以生效了。
参考文章
- Ubuntu18.04下双网卡内外网设置 - 大橘为重之大橘已定