星期二, 6月 17, 2008

設定 VirtualBox 網卡為 Host

上網找了一下,在 https://help.ubuntu.com/community/VirtualBox 裡發現相關的作法,記錄下來,以免下次忘記。


VirtualBox - Community Ubuntu Documentation
Networking

To start, NAT is by far the easiest way to get your guests connected to the interweb, but you may want to use the guests as servers, for this you need Host Networking. You will need to install bridge-utils and uml-utilities so that you can make a tap device and add it to a bridge.

sudo apt-get install bridge-utils uml-utilities

Now make a bridge, and put your current interface into it:

sudo tunctl -t tap1 -u fred #請將 fred 改為可以執行 VirtualBox 的帳號
sudo chown root.vboxusers /dev/net/tun
sudo chmod g+rw /dev/net/tun


Set the permission to persist across reboots, by editing as root /etc/udev/rules.d/20-names.rules and changing:

KERNEL=="tun", NAME="net/%k"

to

KERNEL=="tun", NAME="net/%k", GROUP="vboxusers", MODE="0660"

Make a new bridge called br0:

sudo brctl addbr br0

Put your current interface (in this case eth0) into promiscuous mode, then add it to the bridge and give the bridge a dhcp address.

sudo ifconfig eth0 0.0.0.0 promisc
sudo brctl addif br0 eth0


If you are using DHCP to automatically get an IP address, set the bridge to use DHCP.

sudo dhclient br0

If you are using a static IP, specify the IP address, netmask and add the default gateway route

#Where 192.168.1.105 is your static IP and 255.255.0.0 is your netmask
sudo ifconfig br0 192.168.1.105 netmask 255.255.0.0

#Where 192.168.1.1 is your default gateway
sudo route add default gw 192.168.1.1 br0

Add the new tap1 device to the bridge and activate tap1 (the second line appears to be necessary according to [WWW] http://ubuntuforums.org/showthread.php?t=561461#3)

sudo brctl addif br0 tap1
sudo ifconfig tap1 up