[转]如何在Ubuntu上为VirtualBox建立 Host interface neworking
How to set up host interface networking for VirtualBox on Ubuntu
VirtualBox is really nice, but if you’re like me, maybe you found the networking confusing. There are three ways to do it, as explained by the manual, and the best way is with host interfaces, which don’t have limitations like the inability to ping and so on. I found what I think is a pretty good way to set up host interface networking.
VirtualBox 真的不错,但是如果你像我一样,或许你会为联网疑惑。根据手册,有三种联网方式,最好的方式是 Host interfaces, 没有任何限制,比如 ping 等等。我发现了,我认为是一个很好的建立 Host interface networking的方法。
The manual explains a bunch of ways to set up host interface networking, generally involving complex modifications to your system’s network configuration to add bridging and so on. This is necessary (contrary to what you might think, creating a virtual network interface won’t work). But the way they explain to set it up is a lot more complex than it needs to be, and actually left my machine’s networking nonfunctional.
手册解释了一堆建立host interface networking 的方法,通常需要对你的系统的网络配置添加桥接等等一些复杂的修改。这是必需的(和你想的相反,建立一个虚拟的network interface 不会work)。 但是他们解释的如何建立的方法要比真真需要做的复杂的多,最后,让我的电脑的联网功能也不好用了。
I created a little shell script and put it into my $PATH. All I have to do is run this before I start my virtual machine, and it sets up bridging and so forth:
我创建了一个小的脚本,并且放在了$PATH中,我需要做的就是在运行虚拟机前运行这段脚本,这个脚本会建立桥接等等。
#!/bin/sh
set -e
set -u
set -x
sudo tunctl -t tap0 -u `whoami`
sudo chmod 666 /dev/net/tun
sudo /usr/sbin/brctl addbr br0
sudo /sbin/ifconfig eth0 0.0.0.0 promisc
sudo /usr/sbin/brctl addif br0 eth0
sudo /sbin/dhclient br0
sudo /usr/sbin/brctl addif br0 tap0
sudo ifconfig tap0 192.168.1.51 up
sudo bash -c 'echo 1 > /proc/sys/net/ipv4/conf/tap0/proxy_arp'
IP=`ifconfig | grep 192 | head -n 1 | awk '{print $2}' | cut -d: -f2`
sudo route add -host $IP dev tap0
sudo arp -Ds $IP eth0 pub
The script assumes that your machine’s primary network device is named eth0. For this to work, you need a couple of packages installed:
这个脚本假定你的电脑的主要的网络设备是eth0, 要让这个脚本运行,你需要安装一系列的pkg.
sudo apt-get install uml-utilities bridge-utils
Specify ‘tap0′ as the network device in the VirtualBox machine’s settings.
在VirtualBox machine 的设置中设置 tap0为网络设备。
One of the biggest reasons I like this more than the methods in the manual is that it doesn’t mess with my networking config in a permanent way. There are no surprises after a reboot, for example.
我喜欢这样做而不是使用手册上的方法的最大原因是它不会永久地让我的网络配置混乱,比如重新启动之后没有惊喜。