首页 > 技术文章 > 【Ubuntu日常技巧】VirtualBox多网卡路由配置,保障虚拟机连接上外网

QuLory 2015-05-02 16:25 原文

背景】: 配置Ubuntu 虚拟机双网卡,一个是Host-Only网络,一个是桥接网络。当在虚拟机中同时连接到两个网络后,虚拟机能够ping通内部网络,不能ping通外部网络,如www.baidu.com。

1. 虚拟机网卡配置

    

2. 虚拟机内部网络配置

HostOnly网络配置:                                   

桥接网络配置:

 

3.检查虚拟机网络

 ping不通外网,查看路由表。

 

失败原因: 发现default路由是HostOnly 的网络,网段是56段的,不能与外网相通;将default路由修改为1段(192.168.1.X)的。

4.修改default路由

dev@dev-VirtualBox:~$ sudo route add default gw 192.168.1.1
dev@dev-VirtualBox:~$ route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         192.168.1.1     0.0.0.0         UG    0      0        0 eth1
default         192.168.56.1    0.0.0.0         UG    0      0        0 eth0
192.168.1.0     *               255.255.255.0   U     1      0        0 eth1
192.168.56.0    *               255.255.255.0   U     1      0        0 eth0

 

 修改后发现有两个default路由,然后出现了Dns,无法进行域名解析。

 解决方案:

 删除56网段的default路由

dev@dev-VirtualBox:~$ sudo route del default gw 192.168.56.1
dev@dev-VirtualBox:~$ route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         192.168.1.1     0.0.0.0         UG    0      0        0 eth1
192.168.1.0     *               255.255.255.0   U     1      0        0 eth1
192.168.56.0    *               255.255.255.0   U     1      0        0 eth0

 

 

 

还有一种比较简单的方案,在创建虚拟机网卡时,设置虚拟机网卡顺序为:网卡1 桥接网络; 网卡2HostOnly网络。

 

 

推荐阅读