最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

virtualbox虛擬機(jī)NAT模式下不能連接外網(wǎng)的解決方法

 更新時(shí)間:2019年03月25日 14:39:19   作者:styshoo  
這篇文章主要為大家詳細(xì)介紹了virtualbox虛擬機(jī)NAT模式下不能連接外網(wǎng)的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

背景

給VirtualBox虛擬機(jī)(裝載了Ubuntu16.04系統(tǒng))配置了兩張網(wǎng)卡,網(wǎng)絡(luò)模式分別為“網(wǎng)絡(luò)地址轉(zhuǎn)換(NAT)”和“僅主機(jī)(Host-Only)適配器”,其中,enp0s3網(wǎng)卡(NAT)用于外網(wǎng)訪問(wèn),而enp0s8網(wǎng)卡(Host-Only)用于主機(jī)訪問(wèn)虛擬機(jī)。然而,虛擬機(jī)啟動(dòng)后,卻不能訪問(wèn)外網(wǎng)。

定位

網(wǎng)絡(luò)配置文件如下:

# vi /etc/network/interface

...
# The primary network interface
auto enp0s3
iface enp0s3 inet dhcp

auto enp0s8
iface enp0s8 inet static
address 192.168.137.16
netmask 255.255.255.0
gateway 192.168.137.1

eth0使用dhcp,eth1使用static。eth0的實(shí)際網(wǎng)絡(luò)如下:

# ifconfig 
enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
    inet 10.0.2.15 netmask 255.255.255.0 broadcast 10.0.2.255
    inet6 fe80::a00:27ff:fe55:2858 prefixlen 64 scopeid 0x20<link>
    ether 08:00:27:55:28:58 txqueuelen 1000 (Ethernet)
    RX packets 6 bytes 1476 (1.4 KB)
    RX errors 0 dropped 0 overruns 0 frame 0
    TX packets 33 bytes 3108 (3.1 KB)
    TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

打開其路由,才發(fā)現(xiàn)了問(wèn)題。

# route -n
Kernel IP routing table
Destination   Gateway     Genmask     Flags Metric Ref  Use Iface
0.0.0.0     192.168.137.1  0.0.0.0     UG  0   0    0 enp0s8
10.0.2.0    0.0.0.0     255.255.255.0  U   0   0    0 enp0s3
192.168.137.0  0.0.0.0     255.255.255.0  U   0   0    0 enp0s8

enp0s8網(wǎng)卡成為了默認(rèn)路由,這就導(dǎo)致其他路由不能匹配到的網(wǎng)段都會(huì)走enp0s8這個(gè)網(wǎng)卡,而我們實(shí)際上配置與外網(wǎng)連接的虛擬網(wǎng)卡是enp0s3,環(huán)境自然就連接不了外網(wǎng)了。我們可以嘗試手動(dòng)來(lái)刪除現(xiàn)在的默認(rèn)路由。

# route del default
# route add default gw 10.0.2.2 dev enp0s3
# route -n

Kernel IP routing table
Destination   Gateway     Genmask     Flags Metric Ref  Use Iface
default     gateway     0.0.0.0     UG  0   0    0 enp0s3
10.0.2.0    0.0.0.0     255.255.255.0  U   0   0    0 enp0s3
192.168.137.0  0.0.0.0     255.255.255.0  U   0   0    0 enp0s8

路由設(shè)置成功,OS也可以訪問(wèn)外網(wǎng)了。但這只是修改了本次的路由設(shè)置,OS重啟后就失效了,因此我們需要將配置持久化。

持久化路由配置

我們將路由持久化設(shè)置在網(wǎng)絡(luò)配置文件/etc/network/interfaces中。在網(wǎng)卡啟動(dòng)后添加對(duì)應(yīng)的路由增刪的代碼,與route命令類似,只是在句首加上up即可。

# vi /etc/network/interfaces
...
auto enp0s3
iface enp0s3 inet dhcp
up route add default gw 10.0.2.2 dev enp0s3

auto enp0s8
iface enp0s8 inet static
address 192.168.137.16
netmask 255.255.255.0
gateway 192.168.137.1
up route del default dev enp0s8

注意:up route add default gw [gateway-addr] dev [dev-name],該語(yǔ)句中,[dev-name]表示外網(wǎng)網(wǎng)卡的名稱,即上面的enp0s3,而[gateway-addr]表示外網(wǎng)網(wǎng)卡使用的網(wǎng)關(guān)ip地址。
那么,如何獲取這個(gè)外網(wǎng)網(wǎng)卡的網(wǎng)關(guān)地址呢?virtualbox如下規(guī)定:

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.

簡(jiǎn)單的說(shuō),就是如果第0個(gè)網(wǎng)卡是NAT網(wǎng)卡,那么其網(wǎng)段的第三個(gè)數(shù)字就0+2=2就是10.0.2.0,網(wǎng)關(guān)為10.0.2.2,name server則是10.0.2.3.以此類推。

參考:鏈接地址

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

额敏县| 谷城县| 潮安县| 兴安盟| 井冈山市| 弋阳县| 蓬莱市| 呼伦贝尔市| 辉南县| 治多县| 诏安县| 达尔| 象山县| 法库县| 洛川县| 霸州市| 元朗区| 灯塔市| 新干县| 栾川县| 久治县| 涿鹿县| 江安县| 鄢陵县| 翁源县| 罗田县| 邛崃市| 天台县| 黔东| 通江县| 松江区| 康乐县| 马鞍山市| 新沂市| 马尔康县| 嘉峪关市| 井冈山市| 罗源县| 沂源县| 通榆县| 杭锦后旗|