主机通过host-only虚拟网卡连接VBOX虚拟机
[blockquote]
How to set up host-only virtual machines that host can access it...
[/blockquote]
问题
因为工作和学习,我经常需要移动装有Linux系统的笔记本电脑,有时在家里用,有时在公司里用,这两个地方的网络都是不同的,或者有时要开会或到外面,不能连接任何的网络,所以即使虚拟机配置了NTA或者桥接网络主机不能访问VBOX虚拟机的资源.即便在家里和办公司室里每次都要根据环境修改静态IP联网,为了解决这个问题,尝试了很多方法,觉得以下的方法还可以,便整理下来,以下的案例主机为:Fedora 25,虚拟机为:CentOS 6.7.
解决方案
首先打开vobx,进入File=>Preferences=>Network设置Host-only Networks,进入这个选项如果以前没有设置过新建一个(在Linux上这可能需要root权限,提示没有权限时请使用root权限打开vbox重试),新建一个host-only网卡,并设置IP和网关保存;
保存后主机上多了一个vboxnet0网卡;
vboxnet0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.56.1 netmask 255.255.255.0 broadcast 192.168.56.255 inet6 fe80::800:27ff:fe00:0 prefixlen 64 scopeid 0x20<link> ether 0a:00:27:00:00:00 txqueuelen 1000 (Ethernet) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 172 bytes 31905 (31.1 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
进入虚拟机settings=>NetWork,添加新的Host-only Adapter网卡,Name项选择上面创建的host-only网卡;
重启虚拟机,检查新网卡adapter;
# ls /sys/class/net eth0 eth1 eth2 lo
本例新添加的adapter为eth2;此时通过ifconfig可能看不到eth2网卡的信息,进入网卡配置目录,复制一个已经存在的网卡配置文件并命名为ifcfg-eth2;
cd /etc/sysconfig/network-scripts/ cp ifcfg-eth1 ifcfg-eth2
编辑该文件,设置静态IP和网关;
DEVICE="eth2" BOOTPROTO="static" IPV6INIT="yes" NM_CONTROLLED="yes" ONBOOT="yes" TYPE="Ethernet" IPADDR=192.168.56.10 NETMASK=255.255.255.0
重启网络服务;
service network restart
再次检查eth2网卡已经正常启动;
eth2 Link encap:Ethernet HWaddr 08:00:27:E5:7C:79 inet addr:192.168.56.10 Bcast:192.168.56.255 Mask:255.255.255.0 inet6 addr: fe80::a00:27ff:fee5:7c79/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:664 errors:0 dropped:0 overruns:0 frame:0 TX packets:434 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:75195 (73.4 KiB) TX bytes:69955 (68.3 KiB)
最后在主机上通过host-only连接虚拟机正常;
$ ssh [email protected] [email protected]'s password: Last login: Sat May 20 09:00:45 2017 from 192.168.56.1 [root@gpdb-sandbox ~]# ifconfig eth2 Link encap:Ethernet HWaddr 08:00:27:E5:7C:79 inet addr:192.168.56.10 Bcast:192.168.56.255 Mask:255.255.255.0 inet6 addr: fe80::a00:27ff:fee5:7c79/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:726 errors:0 dropped:0 overruns:0 frame:0 TX packets:475 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:83180 (81.2 KiB) TX bytes:78426 (76.5 KiB) lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:65536 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:0 (0.0 b) TX bytes:0 (0.0 b)
可以肯定的是只要主机的host-only网卡启用和正确配置的情况下,即使主机的其它网卡因为移动更改IP或没有连接到任何网络,这个连接依旧可用.