VirtualBox可以建立Host-Only网络,也就是一个天然和外部独立的内部虚拟局域网,分配给虚机实例,使得处于该局域网的虚机可以彼此内部互通;同时host宿主机还能和这些虚机直接通信;建立一个host-only网络也即是在宿主机建立一张虚拟网卡vboxnetx,x从0开始递增。但是整个虚拟局域网vboxnetx是没有外网连接的。这篇博文目的就在于配置不同宿主机系统中(CentOS7,Mac OS X 10,Windows)多网卡(虚拟网卡)的网络共享。
建立一个Host-only网络,宿主机会自动建立一张虚拟网卡vboxnet0(vboxnetX,依次增加编号),也就是网关为10.0.0.1/24的子网10.0.0.0/24,该网卡默认不能连接外网。建立如下图:
一、CentOS 7宿主机
在CentOS7之前的版本中,默认启用iptables,而CentOS7中则默认不安装iptables而替代性的默认启用了另一个防火墙实现firewalld,通过命令行客户端firewall-cmd来配置。
# ip a // 查看宿主机网络配置 ,其中p2p1为物理网卡
-
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
-
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: p2p1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
-
link/ether 64:00:6a:0c:2a:24 brd ff:ff:ff:ff:ff:ff
-
inet 192.168.1.88/24 brd 192.168.1.255 scope global p2p1
-
valid_lft forever preferred_lft forever
-
inet6 fe80::6600:6aff:fe0c:2a24/64 scope link
-
valid_lft forever preferred_lft forever
-
3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN
-
link/ether 52:54:00:81:46:32 brd ff:ff:ff:ff:ff:ff
-
inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
-
valid_lft forever preferred_lft forever
-
4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master virbr0 state DOWN qlen 500
-
link/ether 52:54:00:81:46:32 brd ff:ff:ff:ff:ff:ff
-
5: vboxnet0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
-
link/ether 0a:00:27:00:00:00 brd ff:ff:ff:ff:ff:ff
-
inet 10.0.0.1/24 brd 10.0.0.255 scope global vboxnet0
-
valid_lft forever preferred_lft forever
-
inet6 fe80::800:27ff:fe00:0/64 scope link
-
valid_lft forever preferred_lft forever
# echo "net.ipv4.ip_forward=1" >> /etc/sysctl.conf // 更改宿主机系统的内核参数,启用IP转发
# cat /etc/sysctl.conf //查看更改情况
-
# System default settings live in /usr/lib/sysctl.d/00-system.conf.
-
# To override those settings, enter new settings here, or in an /etc/sysctl.d/<name>.conf file
-
#
-
# For more information, see sysctl.conf(5) and sysctl.d(5).
-
-
# For vboxnet0 network sharing
-
net.ipv4.ip_forward = 1
# sysctl -p // 让更改生效
# firewall-cmd --get-zone-of-interface=
-
lo p2p1 vboxnet0 virbr0 virbr0-nic
# firewall-cmd --get-zone-of-interface=p2p1 // 查看系统网卡p2p1和虚拟网卡vboxnet0所属的zone都是public
# firewall-cmd --get-zone-of-interface=vboxnet0 // 注意:网卡p2p1是连接外网的,而vboxnet0当前是不能的
# firewall-cmd --list-all // 查看当前firewalld默认配置,并未启用masquerade功能
-
public (default, active)
-
interfaces: p2p1 vboxnet0
-
sources:
-
services: dhcpv6-client ssh
-
ports:
-
masquerade: no
-
forward-ports:
-
icmp-blocks:
-
rich rules:
# firewall-cmd --zone=internal --change-interface=vboxnet0 // 把网卡vboxnet0的zone更改为internal
# firewall-cmd --permanent --zone=internal --change-interface=vboxnet0 // 永久更改
# firewall-cmd --get-zone-of-interface=vboxnet0 // 检查更改效果
# firewall-cmd --zone=public --add-masquerade // 启用连接了外网的zone public的masquerade功能
# firewall-cmd --permanent --zone=public --add-masquerade // 永久启用
# firewall-cmd --zone=public --list-all // 检查效果
-
public (default, active)
-
interfaces: p2p1
-
sources:
-
services: dhcpv6-client ssh
-
ports:
-
masquerade: yes //已启用
-
forward-ports:
-
icmp-blocks:
-
rich rules:
2. 去虚机验证网络共享效果(Mac和Windows的同样)
# ip a // 查看虚机网络配置
-
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
-
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 qlen 1000
-
link/ether 08:00:27:05:0d:96 brd ff:ff:ff:ff:ff:ff
-
inet 10.0.0.11/24 brd 10.0.0.255 scope global enp0s3
-
valid_lft forever preferred_lft forever
-
inet6 fe80::a00:27ff:fe05:d96/64 scope link
-
valid_lft forever preferred_lft forever
# ping baidu.com // ping通百度搞定
-
PING baidu.com (111.13.101.208) 56(84) bytes of data.
-
64 bytes from 111.13.101.208: icmp_seq=1 ttl=49 time=5.31 ms
-
64 bytes from 111.13.101.208: icmp_seq=2 ttl=49 time=5.04 ms
-
64 bytes from 111.13.101.208: icmp_seq=3 ttl=49 time=4.88 ms
-
64 bytes from 111.13.101.208: icmp_seq=4 ttl=49 time=5.03 ms
-
^C
-
--- baidu.com ping statistics ---
-
4 packets transmitted, 4 received, 0% packet loss, time 3393ms
-
rtt min/avg/max/mdev = 4.889/5.069/5.311/0.160 ms
二、Mac OS X 10 (Capitan)
# vim /etc/sysctl.conf // Mac OS没有文件/etc/sysctl.conf,所以这里新建并编辑启用IP转发
# ls -l /etc/sysctl.conf // 这个文件重启系统后会恢复原样抹掉更改
-
-rw-r--r-- 1 root wheel 25 Sep 25 11:40 /etc/sysctl.conf
# chmod 444 /etc/sysctl.conf // 所以禁止写权限
sysctl -a | grep forwarding // 重启系统后验证下
-
net.inet.ip.forwarding: 1
# ipconfig // 查看Mac网络配置,我这里是通过Thunderbolt接口en4来连接外网,WIFI无线网卡为en0,请根据你的环境做相应更改
-
...
-
en4: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
-
options=10b<RXCSUM,TXCSUM,VLAN_HWTAGGING,AV>
-
ether 98:5a:eb:d1:7e:ae
-
inet6 fe80::9a5a:ebff:fed1:7eae%en4 prefixlen 64 scopeid 0x9
-
inet 192.168.1.89 netmask 0xffffff00 broadcast 192.168.13.255
-
nd6 options=1<PERFORMNUD>
-
media: autoselect (100baseTX <full-duplex,flow-control>)
-
status: active
-
...
-
vboxnet0: flags=8943<UP,BROADCAST,RUNNING,PROMISC,SIMPLEX,MULTICAST> mtu 1500
-
ether 0a:00:27:00:00:00
-
inet 10.0.0.1 netmask 0xffffff00 broadcast 10.0.0.255
# vim /etc/pf.conf // 开启package filter功能,也即是启用nat功能
-
# 格式为:
-
# 1. nat on interface from vboxnetx:network -> (interface)
-
# 2. nat on interface from CIDR -> (interface)
-
# 如果有多个网卡,比如Mac的无线,通过更多的Thunderbolt接口联网,则对应的多加几条
-
nat on en4 from vboxnet0:network -> (en4)
-
# 或者是 nat on en4 from 10.0.0.0/24 -> (en4)
# defaults write /System/Library/LaunchDaemons/com.apple.pfctl ProgramArguments '(pfctl, -f, /etc/pf.conf, -e)' // 加载pf.conf文件配置
到这里,虚机就能通过虚拟网卡vboxnet0作为网关来连接外网了。同时注意,因为Mac OS X时不时要系统更新,更新之后这个pf.conf文件很可能会被还原,记得查看和再次更改。另外,如果pf.conf文件没有开机加载生效,则执行下面的命令来手工加载或者写个脚本:
-
# pfctl -e // 确认启用pf
-
# pfctl -f /etc/pf.conf // 手工加载pf.conf中的配置
我这里直接把这个命令优化下,用Mac自带的Automator写个程序,放在任务栏dock上,以后点击执行即可:
-
sudo pfctl -f /etc/pf.conf 1>/dev/null 2>/dev/null &
-
sudo pfctl -e 1>/dev/null 2>/dev/null &
三、Windows宿主机
Windows有UI界面,非常简单,下面以Win7截图示意。联网的网卡属性的共享tab中,勾选启用允许别的网络用户通过该网卡联网。而另一张网卡如vboxnet0随便设置个私网IP比如192.168.1.1,然后连接该网卡网络的虚拟主机IP设置在同一个网段如192.168.1.0/24并设置网关为192.168.1.1即可。
(责任编辑:IT) |