notes2myself

a seo and web-developer’s daily stuff

In order for this to run, you will most likely need a wired network connection, since most wireless-adapters won’t support bridged networking! This description is intended to be used with VirtualBox >= 1.4.0, since earlier versions handle the virtual networking differently due to kernel changes in 2.6.18 and later.

First of all, you’ll have to check the permissions on the device /dev/net/tun . The user running VirtualBox with bridged networking needs to have access to this device. The easiest way to do this is by chown’ing the group vboxusers to it:

sudo chown :vboxusers /dev/net/tun
sudo chmod 0660 /dev/net/tun

You will also have to install the package bridge-utils and uml-utilities:

sudo apt-get install bridge-utils uml-utilities

Now we will create 2 scripts which are executed when the virtual machine starts/stops. I will create those scripts in my home dir. Here is the start script, I called it starttun.sh:

#!/bin/bash
brctl addbr br0
ifconfig eth0 0.0.0.0
brctl addif br0 eth0

#if you have a dhcp-server uncomment this line:
#dhclient3 br0

#If you have a static IP uncomment the following lines and
#change the IP accordingly to your subnet:
#ifconfig br0 192.168.178.5 up
#route add default gw 192.168.178.1

#Now we will create the tap device for the vm,!
# change your username accordingly
tunctl -t tap0 -u simon

#Now add the tap-device to the bridge:
ifconfig tap0 up
brctl addif br0 tap0

Now you’ll have to create the stop script, i called it stoptun.sh ;)

#!/bin/bash
#bring the interfaces down
ifconfig tap0 down
ifconfig br0 down
brctl delif br0 tap0
brctl delbr br0

#now setup your network-interface again
#for dhcp uncomment the following line
#dhclient3 eth0

#For a static IP uncomment the following lines and change them accordingly:
#ifconfig eth0 192.168.178.5
#route add default gw 192.168.178.1 dev eth0

Finally you’ll have to make the scripts executable:

sudo chmod ug+x starttun.sh
sudo chmod ug+x stoptun.sh

It’s time to set up VirtualBox to use the interface. For this go to the SetUp of your Virtual Machine under Network and tell VirtualBox to start/stop thescripts, when the VM is started/stopped. To do this, select “Host Interface” under Attached To. As Interface Name you use “tap0″ and for the startscript you use:

“gksudo /home/YOURHOMEDIR/starttun.sh”

For the stopscript accordingly:

“gksudo /home/YOURHOMEDIR/stoptun.sh”

Note: If you use KDE, you’ll have to use kdesu instead of gksudo

Share and Enjoy:
  • Digg
  • del.icio.us
  • Google
  • Yigg

12 Comments

  1. kragas
    7:58 am on June 8th, 2008

    Thanks ! You Saved my day !

  2. Faust
    6:19 pm on July 3rd, 2008

    Hey I was wondering if you could possibly make the script one script and have it call gksudo/kdesu so that you can have Virtualbox run the command from the ‘Network’ settings of the VM.

  3. @Faust:

    The scripts are intended to be used by Virtualbox this way. You can set the above commands for start/stop in the according VM-Settings under Network.

  4. 2j
    12:12 am on July 16th, 2008

    would you have any tips on how to grant a normal user the permissions to modify/create the br0 and tun0 interfaces? Right now, these scripts fail for me in vbox due to permissions. However, If I run them manually outside of vbox as root (or sudo them), they work very well.

  5. 2j
    2:42 am on July 16th, 2008

    Nevermind. I believe you covered that very well with the device perms, and the usage of gksudo in the start/stop scripts settings for the interface. I must have another issue that is preventing the scripts from running correctly.

  6. gewe
    12:03 pm on July 22nd, 2008

    How would I configure a bridge to have a VirtualBox guest in a network with the host, and VMware’s vmnet1 interface? vmnet1 is VMware’s host only network, where the guest machines are in a private network, and the host is part of that network. (So guests can see eachother, can see the host, but can not see anything outside the host, like the internet.)

  7. I love the script.You sir are a legend.

  8. Roger
    9:18 pm on September 23rd, 2008

    I tried working your way but I must have messed up somehow.

    Here is the script that I used. Can you please help me use eth0 with multiple taps!

    #!/bin/bash
    clear
    echo “Restarting VirtualBox Host Interfaces”
    echo “All virtual connections will be lost”
    ifconfig tap1 down
    ifconfig br0 down
    brctl delbr br0
    ifconfig eth0 172.16.31.150 netmask 255.255.255.0 up
    route add default gw 172.16.31.1 eth0
    echo “Bringing down VirtualBox Host Interfaces”
    tunctl -t tap1 -u dofta
    chown root.vboxusers /dev/net/tun
    chmod g+rw /dev/net/tun
    brctl addbr br0
    ifconfig eth0 0.0.0.0 promisc
    brctl addif br0 eth0
    ifconfig br0 172.16.31.150 netmask 255.255.255.0
    route add default gw 172.16.31.1 br0
    brctl addif br0 tap1
    ifconfig tap1 up
    echo “VirtualBox Host Interfaces Bridged Successfully”

  9. Seth
    4:51 pm on September 25th, 2008

    I’m looking for this answer on my own, but I’d like some help resolving it.

    I need this to work for an end user, the fewer things they have to do the better. How do I run these scripts without gksudo? I’m not so concerned about security (I know call me crazy) but I can’t have my users entering passwords all the time.

    Any help I appreciate. If I find the answer I’ll share it here.

    Seth

  10. Lewis Balentine
    11:23 pm on October 26th, 2008

    Appreciate the effort and answers but ….
    this is exactly the type of thing that will keep linux/ubuntu from ever achieving a significant portion of the mqarket. As long as users have to spend a day searching and patching every application that was not built into the OS distribution then the OS will never make it to the mainstream and it will remain in the realm of hakers, nerds and others that have more time than money.

  11. [...] isnt, and especially on what it tries to be. But some weeks ago I got a comment on my post “setting up a bridged network for virtualbox on ubuntu linux” which I can’t leave uncommented: Appreciate the effort and answers but …. this is [...]

  12. [...] a bridged network using VirtualBox is not as straightforward as it is with VMWare. I had to track down a script that would automatically configure the bridge before I started the [...]