setting up a bridged network for virtualbox on ubuntu linux (Host Interface)

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

Be Sociable, Share!

19 Comments

  • kragas
    June 8, 2008 - 7:58 am | Permalink

    Thanks ! You Saved my day !

  • Faust
    July 3, 2008 - 6:19 pm | Permalink

    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.

  • July 11, 2008 - 3:03 pm | Permalink

    @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.

  • 2j
    July 16, 2008 - 12:12 am | Permalink

    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.

  • 2j
    July 16, 2008 - 2:42 am | Permalink

    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.

  • gewe
    July 22, 2008 - 12:03 pm | Permalink

    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.)

  • September 19, 2008 - 6:28 am | Permalink

    I love the script.You sir are a legend.

  • Roger
    September 23, 2008 - 9:18 pm | Permalink

    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”

  • Seth
    September 25, 2008 - 4:51 pm | Permalink

    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

  • Lewis Balentine
    October 26, 2008 - 11:23 pm | Permalink

    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.

  • Pingback: Linux is what you want it to be, nothing else | notes2myself

  • Pingback: Issues that I encountered while creating my DataPower to MQ Demo | danzrobok.com

  • December 25, 2008 - 11:39 pm | Permalink

    I appreciate the info & i’m early in solving the problem here but under Ubuntu 8.10 and Virtualbox 2.0.4_OSE, this FAILS. The error is as follows when the VM executes:

    Failed to initialize Host Interface Networking.
    VBox status code: -3100 (VERR_HOSTIF_INIT_FAILED).

    Result Code:
    NS_ERROR_FAILURE (0×80004005)
    Component:
    Console
    Interface:
    IConsole {e3c6d4a1-a935-47ca-b16d-f9e9c496e53e}

    Again 0 another “it worked on mine…” kind of answer. If I get it working, I’ll post it but I have to agree with the individual who said this prevents Ubuntu from succeeding by having to patch every little thing. That’s kind of the overall Linux problem succintly stated. The problem is it takes so $&@)*&# long to get everything working. God forbid, a kernel update comes out – you’re at it all over again. (sorry – just ranting at yet another linux FAILURE)

    If anyone has solved this problem, please share & save me the time that I truly need to devote elsewhere…

  • toby
    February 20, 2009 - 10:56 pm | Permalink

    I have the same problem as “rshane” has.
    Does anybody solved this? If yes, can you please post it? thank you.

  • February 21, 2009 - 1:57 pm | Permalink

    My guess would be, that there is something wrong with the permissions on the file /dev/net/tun . Did you change the permissions like explained above in the first step? And are you a member of the group vboxusers? Remember also, that if you just made your user a member of that group, you have to log out and then log in again for the changes to take effect!

  • abetpcmt
    March 13, 2009 - 1:21 pm | Permalink

    does anybody solved toby & rshane issue? i’m also encountering the same problem. tnx

  • Yazid
    April 27, 2009 - 3:48 pm | Permalink

    Excellent, this is whta I was looking for

    I got stuck on the following:

    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”

    I cannot see Host Interface, could you give more details?

    TIA
    Yaz

  • PMorton
    November 11, 2009 - 1:48 pm | Permalink

    On Virtualbox 2.2.4, there is no “Host Interface” under “Attached to”; only “Bridged Adaptor” and “Host Only Adaptor”. Neither is it possible to run scripts. An update would be very useful.

  • Hodge
    June 23, 2010 - 2:23 pm | Permalink

    It looks like if you select “Bridged Adapter” as attached to and select eth0 that it will work. It worked for me anyway allowing me to join my domain.

  • Comments are closed.