1. 程式人生 > >VirtualBox 5.2.x 實戰 / 網路配置 / NAT host-only

VirtualBox 5.2.x 實戰 / 網路配置 / NAT host-only

  今天我們來探討一下在Linux mint 18.3作業系統中如何藉助VirtualBox5.2.10安裝Ubuntu Server 16.04 LTS環境,要求宿主主機與虛擬主機相互訪問,虛擬主機可以正常訪問網際網路。我們知道要完成這一要求,對於Virtualbox來講,可以有如下解決方案:

  • NAT+ host-only方式
  • NAT+ Bridged networking

   考慮到第一種解決方案來講不需要網路運維人員為每臺虛擬主機分配獨立的IP地址,所以這裡我們著重探討一下第一種解決方案。

約定

系統 主機名 IP地址
Ubuntu 16.04 LTS ubuntu1604 192.168.56.101
Linux Mint 18.0 LTS qwfys 192.168.1.4

VirtualBox配置

  在VirtualBox的“Host Network Manager”中新增一個虛擬見網絡卡,系統自動取名為vboxnet0,具體如下面諸圖所示。

這裡寫圖片描述

這裡寫圖片描述

這裡寫圖片描述

  在具體的Machine例項如ubuntu1604對應的網路設定對話方塊中分別啟用Adapter1和Adapter2兩個網路裝置代理,具體如下面諸圖所示。

這裡寫圖片描述

這裡寫圖片描述

ubuntu server 16.04 LTS 配置

  編輯檔案/etc/network/interfaces,

lwk@ubuntu1604:~$ sudo vim /etc/network/interfaces

編輯後的檔案內容如下:

# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

source /etc/network/interfaces.d/*

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
auto enp0s3 iface enp0s3 inet dhcp auto enp0s8 iface enp0s8 inet dhcp ~ "/etc/network/interfaces" 15L, 344C 1,1 All

重啟網路服務

lwk@ubuntu1604:~$ sudo service networking restart 
[sudo] password for lwk: 
lwk@ubuntu1604:~$

檢視網路服務狀態

[email protected]:~$ sudo service networking status
● networking.service - Raise network interfaces
   Loaded: loaded (/lib/systemd/system/networking.service; enabled; vendor preset: enabled)
  Drop-In: /run/systemd/generator/networking.service.d
           └─50-insserv.conf-$network.conf
   Active: active (exited) since Sun 2018-04-29 19:45:31 CST; 8s ago
     Docs: man:interfaces(5)
  Process: 1639 ExecStop=/sbin/ifdown -a --read-environment --exclude=lo (code=exited, status=0/SUCCESS)
  Process: 1730 ExecStart=/sbin/ifup -a --read-environment (code=exited, status=0/SUCCESS)
  Process: 1723 ExecStartPre=/bin/sh -c [ "$CONFIGURE_INTERFACES" != "no" ] && [ -n "$(ifquery --read-environment --list --exclude=lo)" ] && udevadm settle (code=exited, status=0/SUCCESS)
 Main PID: 1730 (code=exited, status=0/SUCCESS)
    Tasks: 2
   Memory: 1.8M
      CPU: 86ms
   CGroup: /system.slice/networking.service
           ├─1777 /sbin/dhclient -1 -v -pf /run/dhclient.enp0s3.pid -lf /var/lib/dhcp/dhclient.enp0s3.leases -I -df /var/lib/dhcp/dhclient6.enp0s3.leases enp0s3
           └─1837 /sbin/dhclient -1 -v -pf /run/dhclient.enp0s8.pid -lf /var/lib/dhcp/dhclient.enp0s8.leases -I -df /var/lib/dhcp/dhclient6.enp0s8.leases enp0s8

Apr 29 19:45:31 ubuntu1604 ifup[1730]: DHCPDISCOVER on enp0s8 to 255.255.255.255 port 67 interval 4 (xid=0x6ac283b)
Apr 29 19:45:31 ubuntu1604 dhclient[1822]: DHCPREQUEST of 192.168.56.101 on enp0s8 to 255.255.255.255 port 67 (xid=0x3b28ac06)
Apr 29 19:45:31 ubuntu1604 ifup[1730]: DHCPREQUEST of 192.168.56.101 on enp0s8 to 255.255.255.255 port 67 (xid=0x3b28ac06)
Apr 29 19:45:31 ubuntu1604 dhclient[1822]: DHCPOFFER of 192.168.56.101 from 192.168.56.100
Apr 29 19:45:31 ubuntu1604 ifup[1730]: DHCPOFFER of 192.168.56.101 from 192.168.56.100
Apr 29 19:45:31 ubuntu1604 dhclient[1822]: DHCPACK of 192.168.56.101 from 192.168.56.100
Apr 29 19:45:31 ubuntu1604 ifup[1730]: DHCPACK of 192.168.56.101 from 192.168.56.100
Apr 29 19:45:31 ubuntu1604 dhclient[1822]: bound to 192.168.56.101 -- renewal in 548 seconds.
Apr 29 19:45:31 ubuntu1604 ifup[1730]: bound to 192.168.56.101 -- renewal in 548 seconds.
Apr 29 19:45:31 ubuntu1604 systemd[1]: Started Raise network interfaces.
[email protected]:~$ 

檢視當前網絡卡配置

[email protected]:~$ ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 08:00:27:53:d9:dd brd ff:ff:ff:ff:ff:ff
    inet 10.0.2.15/24 brd 10.0.2.255 scope global enp0s3
       valid_lft forever preferred_lft forever
    inet6 fe80::a00:27ff:fe53:d9dd/64 scope link 
       valid_lft forever preferred_lft forever
3: enp0s8: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 08:00:27:78:2f:b4 brd ff:ff:ff:ff:ff:ff
    inet 192.168.56.101/24 brd 192.168.56.255 scope global enp0s8
       valid_lft forever preferred_lft forever
    inet6 fe80::a00:27ff:fe78:2fb4/64 scope link 
       valid_lft forever preferred_lft forever

檢視網路能否上網際網路

[email protected]:~$ ping www.baidu.com
PING www.a.shifen.com (183.232.231.173) 56(84) bytes of data.
64 bytes from 183.232.231.173: icmp_seq=1 ttl=63 time=30.2 ms
64 bytes from 183.232.231.173: icmp_seq=2 ttl=63 time=32.7 ms
64 bytes from 183.232.231.173: icmp_seq=3 ttl=63 time=32.6 ms
64 bytes from 183.232.231.173: icmp_seq=4 ttl=63 time=30.3 ms
64 bytes from 183.232.231.173: icmp_seq=5 ttl=63 time=32.8 ms
64 bytes from 183.232.231.173: icmp_seq=6 ttl=63 time=32.8 ms
^C
--- www.a.shifen.com ping statistics ---
6 packets transmitted, 6 received, 0% packet loss, time 5009ms
rtt min/avg/max/mdev = 30.273/31.962/32.890/1.187 ms
[email protected]:~$

虛擬主機ping宿主主機

[email protected]:~$ ping 192.168.56.1
PING 192.168.56.1 (192.168.56.1) 56(84) bytes of data.
64 bytes from 192.168.56.1: icmp_seq=1 ttl=64 time=0.241 ms
64 bytes from 192.168.56.1: icmp_seq=2 ttl=64 time=0.177 ms
64 bytes from 192.168.56.1: icmp_seq=3 ttl=64 time=0.439 ms
64 bytes from 192.168.56.1: icmp_seq=4 ttl=64 time=0.236 ms
64 bytes from 192.168.56.1: icmp_seq=5 ttl=64 time=0.223 ms
64 bytes from 192.168.56.1: icmp_seq=6 ttl=64 time=0.304 ms
64 bytes from 192.168.56.1: icmp_seq=7 ttl=64 time=0.310 ms
64 bytes from 192.168.56.1: icmp_seq=8 ttl=64 time=0.176 ms
64 bytes from 192.168.56.1: icmp_seq=9 ttl=64 time=0.460 ms
^C
--- 192.168.56.1 ping statistics ---
9 packets transmitted, 9 received, 0% packet loss, time 8016ms
rtt min/avg/max/mdev = 0.176/0.285/0.460/0.098 ms
[email protected]:~$ ping 192.168.1.4
PING 192.168.1.4 (192.168.1.4) 56(84) bytes of data.
64 bytes from 192.168.1.4: icmp_seq=1 ttl=63 time=0.531 ms
64 bytes from 192.168.1.4: icmp_seq=2 ttl=63 time=0.464 ms
64 bytes from 192.168.1.4: icmp_seq=3 ttl=63 time=0.521 ms
64 bytes from 192.168.1.4: icmp_seq=4 ttl=63 time=0.658 ms
64 bytes from 192.168.1.4: icmp_seq=5 ttl=63 time=0.491 ms
64 bytes from 192.168.1.4: icmp_seq=6 ttl=63 time=0.592 ms
64 bytes from 192.168.1.4: icmp_seq=7 ttl=63 time=0.609 ms
^C
--- 192.168.1.4 ping statistics ---
7 packets transmitted, 7 received, 0% packet loss, time 6012ms
rtt min/avg/max/mdev = 0.464/0.552/0.658/0.066 ms
[email protected]:~$ 

宿主主機ping虛擬主機

[email protected] ~ $ ping 192.168.56.101
PING 192.168.56.101 (192.168.56.101) 56(84) bytes of data.
64 bytes from 192.168.56.101: icmp_seq=1 ttl=64 time=0.361 ms
64 bytes from 192.168.56.101: icmp_seq=2 ttl=64 time=0.372 ms
64 bytes from 192.168.56.101: icmp_seq=3 ttl=64 time=0.437 ms
64 bytes from 192.168.56.101: icmp_seq=4 ttl=64 time=0.387 ms
64 bytes from 192.168.56.101: icmp_seq=5 ttl=64 time=0.265 ms
64 bytes from 192.168.56.101: icmp_seq=6 ttl=64 time=0.392 ms
^C
--- 192.168.56.101 ping statistics ---
6 packets transmitted, 6 received, 0% packet loss, time 5105ms
rtt min/avg/max/mdev = 0.265/0.369/0.437/0.052 ms
[email protected] ~ $ 

Ubuntu Server 18.04 LTS 配置

  Ubuntu 18.04 LTS版本中,網路配置採用了全新的網路netplan渲染器。Netplan是一個用於在Linux系統上輕鬆配置網路的實用工具。您只需建立所需網路介面的YAML描述以及每個應配置的內容。從這個描述中,Netplan將為您選擇的渲染器工具生成所有必要的配置。

  編輯檔案/etc/netplan/50-cloud-init.yaml,修改完成後的內容如下:

lwk@ubuntu1804:~$ cat /etc/netplan/50-cloud-init.yaml 
# This file is generated from information provided by
# the datasource.  Changes to it will not persist across an instance.
# To disable cloud-init's network configuration capabilities, write a file
# /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following:
# network: {config: disabled}
network:
    version: 2
    renderer: networkd
    ethernets:
        enp0s3:
            dhcp4: true
            optional: true
        enp0s8:
            dhcp4: true
            optional: true
lwk@ubuntu1804:~$ 

  啟用上述配置

lwk@ubuntu1804:~$ sudo netplan apply
lwk@ubuntu1804:~$

  檢視當前系統中的網路介面卡情況

[email protected]:~$ ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 08:00:27:c3:65:2c brd ff:ff:ff:ff:ff:ff
    inet 10.0.2.15/24 brd 10.0.2.255 scope global dynamic enp0s3
       valid_lft 83360sec preferred_lft 83360sec
    inet6 fe80::a00:27ff:fec3:652c/64 scope link 
       valid_lft forever preferred_lft forever
4: enp0s8: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 08:00:27:c6:b2:07 brd ff:ff:ff:ff:ff:ff
    inet 192.168.56.102/24 brd 192.168.56.255 scope global dynamic enp0s8
       valid_lft 261sec preferred_lft 261sec
    inet6 fe80::a00:27ff:fec6:b207/64 scope link 
       valid_lft forever preferred_lft forever
[email protected]:~$ 

  至此問題得以解決。

參考文獻

相關推薦

VirtualBox 5.2.x 實戰 / 網路配置 / NAT host-only

  今天我們來探討一下在Linux mint 18.3作業系統中如何藉助VirtualBox5.2.10安裝Ubuntu Server 16.04 LTS環境,要求宿主主機與虛擬主機相互訪問,虛擬主機可以正常訪問網際網路。我們知道要完成這一要求,對於Virtua

VirtualBox上安裝CentOS7的網路配置Host Only+NAT方式

安裝過程在這裡就不贅述了。安裝完成後發現虛擬機器能連線外網,但是無法與主機通訊,網絡卡ip地址是 10.0.2.15 的內網地址,在主機上ping這個地址無法ping通。 檢視設定可以發現虛擬機器是通過NAT來連線的,NAT模式下的虛擬系統的TCP/IP配置資訊是由(N

VirtualBox+ubuntu-16.04.2NAT+Host-only網絡配置

配置文件 mas 沒有 net 情況 win 網絡配置 add host-only VirtualBox 5.1.20 ubuntu-16.04.2服務器版。 主要用於工作編譯,虛擬機放在後臺跑。windows下ssl登錄。占用資源少。配置虛擬機網絡為NAT+Host-on

VirtualBox NAT+host-only高階網路配置 ,實現互通

概述:VirtualBox NAT host-only高階網路配置,虛擬機器既可以相互通訊也可以上網。同時,新增NAT方式讓虛擬機器上網,新增host-only方式讓虛擬機器與虛擬機器通訊。深入分析Host-only自定義網段,Windows和Linux環境下分別使用ssh,網絡卡命名規則。預設已經安裝v

Redhat/CentOS6.2 x86系統KVM虛擬機器網路配置 NAT方式

摘要:KVM虛擬機器網路配置的兩種方式:NAT方式和Bridge方式。NAT方式的配置原理和步驟。NAT方式適用於桌面主機的虛擬化。 問題 客戶機安裝完成後,需要為其設定網路介面,以便和主機網路,客戶機之間的網路通訊。事實上,如果要在安裝時使用網路通訊,需要提前設定客戶機的

Cobbler 2.x安裝與配置

centos7 ejs config sync uic mil 自動安裝腳本 file loading 軟件環境 Centos7.3 Cobbler 2.8.1 1、下載、編譯和安裝 創建自動安裝腳本cobbler-install.sh #!/bin/bash # Fi

Install Virtualbox 5.2 in Ubuntu 16.04 LTS

box debian vbox 6.0 ora sta tps update oracle add key wget -q https://www.virtualbox.org/download/oracle_vbox_2016.asc -O- | sudo apt-key

[公益課程]Spring Boot 2.x 實戰入門

thymeleaf 數據 沒有 講師 對象關系映射 編程 連接池 作用域 培訓機構 【公益內容】Spring Boot 2.x 實戰入門課程內容 http://edu.51cto.com/course/13147.html 第1章Spring Boot 2.X快速入門45

【轉載】Vue 2.x 實戰之後臺管理系統開發(二)

null element asc 其他 就會 ans 目錄 asi all 2. 常見需求 01. 父子組件通信 a. 父 -> 子(父組件傳遞數據給子組件) 使用 props,具體查看文檔 - 使用 Prop 傳遞數據(cn.vuejs.org/v2/guide

打開 VirtualBox-5.2 出錯:獲取 VirtualBox COM 對象失敗

img aid -c .config () mage ror linux下 dao 打開 VirtualBox-5.2 報以下錯誤的處理方法獲取 VirtualBox COM 對象失敗.應用程序將被中斷.Document is empty.Location: ‘/home/

VirtualBox 5.2.20 + Ubuntu 18.04.1 LTS + openstack Rocky devstack實驗安裝

順利的安裝了openstack rocky實驗環境,基本安裝過程記錄如下: 一、安裝VirtualBox 5.2.20 下載網址:https://www.virtualbox.org/wiki/Downloads  我選的是windows版本,在膝上型電腦win10下安裝

ubuntu網路配置(NAT方式)

NAT方式上網:   1, 選單欄中虛擬機器-》設定-》網路介面卡-》NAT模式 2,開啟虛擬網路編輯器   3,檢視主機的網路配置  ipcongig 4,開啟網路共享中心->更改介面卡設定 5,修改ip地址

VirturlBox 建立Centos7並配置網路NAT+Host-Only

近兩年的專案開發用的php語言,用的laravel框架,使用的環境一直是官方推薦的Homestead,最近接觸docker技術,覺得很不錯,在自己的伺服器上也成功使用laradock整合環境完成了laravel專案部署,這裡想在自己的虛擬機器上裝centos7,然後在用docker搭建環境,安裝cento

VirtualBox 5.2.20 設定靜態 IP

使用 網路地址轉換(NAT) 配合 僅主機(Host-Only)網路 設定靜態 ip,可以當主機網路變化的時候靜態 ip 依舊可用 總體思想:需要設定兩個網絡卡,一個使用 網路地址轉換(NAT) ,一個

AgileEAS.NET SOA 中介軟體平臺5.2版本下載、配置學習(一):下載平臺並基於直連環境執行

一、前言      AgileEAS.NET SOA 中介軟體平臺是一款基於基於敏捷並行開發思想和Microsoft .Net構件(元件)開發技術而構建的一個快速開發應用平臺。用於幫助中小型軟體企業建立一條適合市場快速變化的開發團隊,以達到節省開發成本、縮短開發時間,快速適應市場變化的目的。      A

AgileEAS.NET SOA 中介軟體平臺5.2版本下載、配置學習(四):開源的Silverlight執行容器的編譯、配置

一、前言      AgileEAS.NET SOA 中介軟體平臺是一款基於基於敏捷並行開發思想和Microsoft .Net構件(元件)開發技術而構建的一個快速開發應用平臺。用於幫助中小型軟體企業建立一條適合市場快速變化的開發團隊,以達到節省開發成本、縮短開發時間,快速適應市場變化的目的。      A

AFN網路框架2.x版—網路監控

- (void)viewDidLoad { [super viewDidLoad]; //1.獲得網路監控管理著 AFNetworkReachabilityManager *mgr = [AFNetworkReachabilityManag

APMServ 5.2.6虛擬主機配置bug

相信大家都用過APMServ 5.2.6吧?? 今天好蛋疼的是,通過檢視化去配置虛擬主機進行訪問本地專案,竟然無法訪問,搞了大半天,還是無法解決這個BUG! 後來沒辦法,只能通過手工進行配置了,具體方法如下: 找到安裝目錄:“D:/APM/APMServ5.2.6/Apac

Hadoop 2.x高可用配置

前言 這兩天整理了一下原來寫的各種文件,準備陸陸續續全部寫成部落格,以防文件丟失,第一篇,使用zookeeper來實現Hadoop的高可用 主機規劃 此處有三臺主機,規劃如下: 主機名 P地址 執行程序 hadoop