Virtualbox on 8.04 Hardy

https://help.ubuntu.com/community/VirtualBox



To configure bridging, you basically go through three steps on the host machine. First you need to create a bridge. Next you create virtual interface(s). Then you add a real interface and virtual interface(s) to the bridge. Because of the scripts available on the distribution which take care of all the details, all of this sums up to two steps:







Rahmen1Edit One System File On The Host Machine

Edit the host machine's /etc/network/interfaces to let your system know what the tap device is. Note: This step will be saved permanently to your host machine's settings so that even if you reboot the machine, you won't need to repeat it.

Back Up The System File

Before you begin, back up the current interfaces file with a copy that has the current date in its name:

sudo cp /etc/network/interfaces /etc/network/interfaces.`date +~%b-%d-%Y~%T` 

Edit The System File

To edit the file in GNOME, type:

gksudo gedit /etc/network/interfaces

To edit the file in KDE, type:

kdesu kate /etc/network/interfaces 

Go to the bottom of the file and add this section, making sure to leave a blank line between the existing contents of the file and the new section you're adding:

 auto tap1
 iface tap1 inet manual
 up ifconfig $iface 0.0.0.0 up
 down ifconfig $iface down
 tunctl_user USERNAME

Replace USERNAME with your username and save the file.

Create A Bridge

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

sudo tunctl -t tap1 -u USERNAME
sudo chown root.vboxusers /dev/net/tun
sudo chmod g+rw /dev/net/tun

Replace USERNAME with the username you use on your host computer.

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. Note after running the following ifconfig command via ssh you may/will lose connectivity to the box. I'm thinking that using the auto script instead, which is later on in this document, will be required for creating the tunnels.

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 host machine's IP address and netmask, and add the default gateway route:

#Where 192.168.1.105 is your host machine's 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 http://ubuntuforums.org/showthread.php?t=561461#3)

sudo brctl addif br0 tap1
sudo ifconfig tap1 up

You should now be able to use host networking in VirtualBox. Just change "Attached to" from "Nat" to "Host interface" and type "tap1" (without the quotes) into the Interface Name box in your virtual machine's Networking settings. Read the manual as well. There are some other nifty ways to do this. Do not forget to use the root account when doing this. Also, reboot your computer afterwards.

Automate The Process

Since the steps in the "Create A Bridge" section are not permanent, if you reboot the machine, you'll have to repeat them (with the exception of the part where you can edit /etc/udev/rules.d/20-names.rules). Rather than re-entering all this information manually, you can automate it by saving the commands into text files and executing them either via a shortcut or from the command line.

Below are four scripts. You'll need either the two static IP scripts or the two dynamic IP scripts, depending on whether your host computer uses a static or dynamic IP. Paste the contents of each into a text editor and save the file. I recommend naming them BridgeUp and BridgeDown or something similar so that you'll know at a glance what each one does.

If you want to make them executable, open the properties of each file and change the executable setting. Then you can either click the files to launch them or make shortcuts to them and click the shortcuts.

If you'd rather run them from the command line, don't make the files executable. Open a terminal and change to the directory the scripts are in and type:

bash SCRIPTNAME

Be sure to substitute BridgeUp or BridgeDown (or whatever you named the files) for SCRIPTNAME in the command.

Script To Bring The Bridge Up If Your Host Computer Uses A Static IP

sudo tunctl -t tap1 -u USERNAME
sudo chown root.vboxusers /dev/net/tun
sudo chmod g+rw /dev/net/tun
sudo brctl addbr br0
sudo ifconfig eth0 0.0.0.0 promisc
sudo brctl addif br0 eth0
sudo ifconfig br0 HOST_IP netmask NETMASK
sudo route add default gw GATEWAY br0
sudo brctl addif br0 tap1
sudo ifconfig tap1 up

Replace USERNAME with the username you use on your host machine. Replace NETMASK with your router's netmask number. Replace GATEWAY with your router's gateway number.

Script To Bring The Bridge Up If Your Host Computer Uses A Dynamic IP (DHCP)

sudo tunctl -t tap1 -u USERNAME
sudo chown root.vboxusers /dev/net/tun
sudo chmod g+rw /dev/net/tun
sudo brctl addbr br0
sudo ifconfig eth0 0.0.0.0 promisc
sudo brctl addif br0 eth0
sudo dhclient br0
sudo brctl addif br0 tap1
sudo ifconfig tap1 up

Replace USERNAME with the username you use on your host machine.

Script To Bring The Bridge Down If Your Host Computer Uses A Static IP

sudo ifconfig tap1 down
sudo ifconfig br0 down
sudo brctl delbr br0
sudo ifconfig eth0 HOST_IP netmask NETMASK up
sudo route add default gw GATEWAY eth0 

Replace NETMASK with your router's netmask number. Replace GATEWAY with your router's gateway number.

Script To Bring The Bridge Down If Your Host Computer Uses A Dynamic IP (DHCP)

sudo ifconfig tap1 down
sudo ifconfig br0 down
sudo brctl delbr br0
sudo dhclient eth0

Now you can put the bridge up or take it down at the drop of a hat!

Additional Tunnels for more Clients

 auto tap2
 iface tap2 inet manual
 up ifconfig $iface 0.0.0.0 up
 down ifconfig $iface down
 tunctl_user USERNAME

Replace USERNAME with your username and save the file.

Increment tap# to as many as needed

Modify the BridgeUp.sh and BridgeDown.sh script adding for each new tunnel client, matching all the new tap#