VirtualBox Ubuntu16.04双网卡联网问题

VirtualBox Ubuntu16.04双网卡联网问题

为了写个实验耗费了10+小时,浏览器开爆了
在这里插入图片描述

本人网卡配置为:


  • 网卡1:仅主机(Host-Only) 对应:enp0s3
  • 网卡2:网络地址转换(NAT)对应:enp0s8

网卡1用于主机访问虚拟机,网卡2用于访问外网。但是网卡2始终没能接通外网

原因: /etc/network/interface 配置未完全,使得host-only成为了默认网卡

可以输入:$route -n
查看网卡设置

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.56.1    0.0.0.0         UG    0      0        0 enp0s3
0.0.0.0         10.0.3.2        0.0.0.0         UG    0      0        0 enp0s8
10.0.3.0        0.0.0.0         255.255.255.0   U     0      0        0 enp0s8
169.254.0.0     0.0.0.0         255.255.0.0     U     1000   0        0 enp0s3
192.168.56.0    0.0.0.0         255.255.255.0   U     0      0        0 enp0s3

网卡1(enp0s3)(host-only只能完成主机与虚拟机间通信不能上网) 成了默认网卡

原因找到

修改方式

输入: $sudo vim /etc/network/interface
(sudo表示root权限的指令,否则会因为没有权限无法修改)

#原代码
# interfaces(5) file used by ifup(8) and ifdown(8)
auto lo
iface lo inet loopback

auto enp0s3
iface enp0s3 inet static
address 192.168.56.101
netmask 255.255.255.0
gateway 192.168.56.1 
dns-nameservers 8.8.8.8

auto enp0s8
iface enp0s8 inet dhcp

加入
up route del default dev enp0s3
up route add default gw 10.0.3.3 dev enp0s8

#需要修改为
# interfaces(5) file used by ifup(8) and ifdown(8)
auto lo
iface lo inet loopback

auto enp0s3
iface enp0s3 inet static
address 192.168.56.101
netmask 255.255.255.0
gateway 192.168.56.1 
dns-nameservers 8.8.8.8
up route del default dev enp0s3

auto enp0s8
iface enp0s8 inet dhcp
up route add default gw 10.0.3.3 dev enp0s8

虚拟机NAT网关、IP命名规则

In NAT mode, the guest network interface is assigned to the IPv4 range 10.0.x.0/24 by default where x corresponds to the instance of the NAT interface +2. So x is 2 when there is only one NAT instance active. In that case the guest is assigned to the address 10.0.2.15, the gateway is set to 10.0.2.2 and the name server can be found at 10.0.2.3.

网卡顺序同数组从0开始,故我的NAT网卡为1号网卡(0号网卡为host-only网卡)
所以我的网卡信息为:

ipv4:   10.0.3.0/24
gateway:10.0.3.3
nameserver:还不确定是啥(挠头求助)

重启虚拟机,查看网卡default

$route -n

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         10.0.3.3        0.0.0.0         UG    0      0        0 enp0s8
0.0.0.0         10.0.3.2        0.0.0.0         UG    0      0        0 enp0s8
10.0.3.0        0.0.0.0         255.255.255.0   U     0      0        0 enp0s8
169.254.0.0     0.0.0.0         255.255.0.0     U     1000   0        0 enp0s3
192.168.56.0    0.0.0.0         255.255.255.0   U     0      0        0 enp0s3



测试联网

ping www.baidu.com

成功

方法灵感来源
https://blog.csdn.net/styshoo/article/details/56456102?utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7Edefault-4.no_search_link&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7Edefault-4.no_search_link
致谢

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值