virtualbox虛擬機(jī)NAT模式下不能連接外網(wǎng)的解決方法
背景
給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)訪(fǎng)問(wèn),而enp0s8網(wǎng)卡(Host-Only)用于主機(jī)訪(fǎng)問(wèn)虛擬機(jī)。然而,虛擬機(jī)啟動(dòng)后,卻不能訪(fǎ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
打開(kāi)其路由,才發(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也可以訪(fǎng)問(wèn)外網(wǎng)了。但這只是修改了本次的路由設(shè)置,OS重啟后就失效了,因此我們需要將配置持久化。
持久化路由配置
我們將路由持久化設(shè)置在網(wǎng)絡(luò)配置文件/etc/network/interfaces中。在網(wǎng)卡啟動(dòng)后添加對(duì)應(yīng)的路由增刪的代碼,與route命令類(lèi)似,只是在句首加上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)卡的名稱(chē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.以此類(lèi)推。
參考:鏈接地址
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- VirtualBox下CentOS7網(wǎng)絡(luò)配置教程(可連外網(wǎng))
- 詳解VirtualBox虛擬機(jī)網(wǎng)絡(luò)環(huán)境解析和搭建-NAT、橋接、Host-Only、Internal、端口映射
- VirtualBox 配置虛擬網(wǎng)卡(橋接),實(shí)現(xiàn)主機(jī)-虛擬機(jī)網(wǎng)絡(luò)互通(圖文教程)
- 手把手教你使用 virtualBox 讓虛擬機(jī)連接網(wǎng)絡(luò)的教程
- 虛擬機(jī)VirtualBox中centos6.5網(wǎng)絡(luò)設(shè)置圖文詳解
- 詳解如何配置 VirtualBox 中的客戶(hù)機(jī)與宿主機(jī)之間的網(wǎng)絡(luò)連接
- VirtualBox配置虛擬網(wǎng)卡(橋接)——實(shí)現(xiàn)主機(jī)-虛擬機(jī)網(wǎng)絡(luò)互通
- 詳解VirtualBox + CentOS 虛擬機(jī)網(wǎng)卡配置
- virtualbox虛擬機(jī)網(wǎng)絡(luò)設(shè)置原理解析
- VirtualBox 未指定要bridged的網(wǎng)絡(luò)界面的解決辦法
- VirtualBox 2.2.0使用主機(jī)網(wǎng)絡(luò)上網(wǎng)配置教程
相關(guān)文章
windows中VirtualBox如何調(diào)整擴(kuò)大VMDK格式的磁盤(pán)空間
這篇文章主要介紹了windows中VirtualBox調(diào)整擴(kuò)大VMDK格式的磁盤(pán)空間,需要的朋友可以參考下2017-12-12詳解VirtualBox + CentOS 虛擬機(jī)網(wǎng)卡配置
要學(xué)好Linux,還是得自己搭建虛擬機(jī). VirtualBox比較小巧簡(jiǎn)單,在配合CentOS 6.4使用時(shí),首要的問(wèn)題就是網(wǎng)卡配置,有興趣的可以了解一下。2016-12-12VirtualBox 虛擬機(jī)中安裝 Ubuntu 12.04(圖文教程)
這篇文章主要介紹了VirtualBox 虛擬機(jī)中安裝 Ubuntu 12.04(圖文教程),有需要的可以了解一下。2016-11-11VirtualBox 配置虛擬網(wǎng)卡(橋接),實(shí)現(xiàn)主機(jī)-虛擬機(jī)網(wǎng)絡(luò)互通(圖文教程)
這篇文章主要介紹了VirtualBox 配置虛擬網(wǎng)卡(橋接),實(shí)現(xiàn)主機(jī)-虛擬機(jī)網(wǎng)絡(luò)互通(圖文教程),有需要的朋友可以了解一下。2016-11-11在虛擬機(jī)virtualbox中安裝ubuntu的圖文教程
Virtualbox虛擬機(jī)相對(duì)比VMWare workstation輕量級(jí)一些,運(yùn)行一般的游戲的話(huà),性能不輸于vmware。但是配置起來(lái)相對(duì)麻煩,接下來(lái)通過(guò)本文給大家介紹在虛擬機(jī)virtualbox中安裝ubuntu的圖文教程,一起看看吧2016-10-10在Virtualbox下為Ubuntu16.04開(kāi)機(jī)自動(dòng)掛載共享目錄的最佳方法
這篇文章主要介紹了在Virtualbox下為Ubuntu16.04開(kāi)機(jī)自動(dòng)掛載共享目錄的最佳方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-09-09virtualbox下osx系統(tǒng)盤(pán)擴(kuò)容方法
這篇文章主要為大家詳細(xì)介紹了virtualbox下osx系統(tǒng)盤(pán)擴(kuò)容方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04