今天我们来探讨一下在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:~$
查看网络服务状态
lwk@ubuntu1604:~$ 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.
lwk@ubuntu1604:~$
查看当前网卡配置
lwk@ubuntu1604:~$ 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
查看网络能否上互联网
lwk@ubuntu1604:~$ 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
lwk@ubuntu1604:~$
虚拟主机ping宿主主机
lwk@ubuntu1604:~$ 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
lwk@ubuntu1604:~$ 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
lwk@ubuntu1604:~$
宿主主机ping虚拟主机
lwk@qwfys ~ $ 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
lwk@qwfys ~ $
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:~$
查看当前系统中的网络适配器情况
lwk@ubuntu1804:~$ 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
lwk@ubuntu1804:~$
至此问题得以解决。
参考文献