Xaprb

Stay curious!

How to set up host interface networking for VirtualBox on Ubuntu

with 12 comments

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.

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.

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:

#!/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:

sudo apt-get install uml-utilities bridge-utils

Specify ‘tap0′ as the network device in the VirtualBox machine’s settings.

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.

Written by Baron Schwartz

November 5th, 2008 at 11:26 pm

12 Responses to 'How to set up host interface networking for VirtualBox on Ubuntu'

Subscribe to comments with RSS

  1. Is the 192.168.1.51 ip address reliant on your dhcp networking being setup with that range?

    Ours is 172.26.1.x so would I have to pick a range suitable?

    Oh also do you think this would work with KVM? I’ve setup a br0 using monkeying with my network conf, but in 8.10 the network config seems to be a bit “magic” with no mention of eth0..

    John Wards

    6 Nov 08 at 5:02 am

  2. it’s an arbitrary IP address I picked. I set my router up to hand out addresses above .100 as DHCP, and anything below is free for the taking. Any unused address is fine.

    Xaprb

    6 Nov 08 at 9:09 am

  3. Hi Baron, thanks for proving this. I use virtual box quite a bit too, instead of setting up all the networking like so, I have used a port forward setup (eg to SSH).

    What you do of course depends on what you are trying to achieve.

    VBoxManage setextradata CentOS5 VBoxInternal/Devices/pcnet/0/LUN#0/Config/guestssh/Protocol TCP
    VBoxManage setextradata CentOS5 VBoxInternal/Devices/pcnet/0/LUN#0/Config/guestssh/GuestPort 22
    VBoxManage setextradata CentOS5 VBoxInternal/Devices/pcnet/0/LUN#0/Config/guestssh/HostPort 8822

    guestssh is the rule name
    CentOS5 is the instance name.
    Might need to “q uote” the servername if spaces etc.

    To remove it, just run the same commands but with out the last option, that is TCP 22 and 8822.

    Then I just ssh -p 8822 user@localhost

    Adam Dixon

    6 Nov 08 at 5:16 pm

  4. Do you use a script to remove the tap0?

    indigo196

    18 Nov 08 at 1:13 pm

  5. Actually I noticed that suspend/resume on my box doesn’t work right after doing this, so I shut down. Most of the time I am doing this work on my side projects in the evenings, so I just shut down my computer when I am ready for bed. I don’t know how to remove the interfaces but I assume it wouldn’t be so hard. Once or twice while debugging I did remove eth0 from the bridge, for example.

    Xaprb

    18 Nov 08 at 1:24 pm

  6. Thanks a lot for this script. I tried to set up the Host Interface Networking several times according different Howtos but it never worked.

    This is a very simple and fast way and I have my developer windows machine in my home lan :).

    leffe

    19 Nov 08 at 5:43 am

  7. Is there any way that this script could be modified to have the tap0 interface acquire its IP via DHCP?

    If not, is there any way that it could be modified so that the tap0 IP was set to a number that was based on what the current eth0 interface address is?

    For example, it checks to see that eth0 is currently 172.10.10.77, so it sets it to 172.10.10.76, etc.

    That way you would not have to modify the script every time your eth0 is using a different subnet.

    Chris Olson

    2 Dec 08 at 8:26 pm

  8. Hi,

    i tried ur way of creating a shell script, but i was wondering, does this bridged networking thing work over wireless as well? i would guess it does, but i’ve tried several ways of setting up this HIN without success…

    i keep getting this

    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}

    any idea?

    Lajfi

    20 Mar 09 at 4:36 pm

  9. You shouldn’t need to do any of this now, as VirtualBox is now supporting this natively with the latest builds.

    Chris Olson

    20 Mar 09 at 5:32 pm

  10. Lajfi, AFAIK most wireless hardware does NOT support this. Mine surely doesn’t (or the drivers don’t, anyway).

    Chris, that’s great; something to look forward to when I upgrade.

    Xaprb

    20 Mar 09 at 6:19 pm

  11. #!/bin/sh
    IP=`ifconfig | grep 10 | head -n 1 | awk ‘{print $2}’ | cut -d: -f2`
    if [ $# -gt 0 ]; then
    sudo ifconfig br0 down
    sudo ifconfig tap0 down
    sudo /usr/sbin/brctl delif br0 eth0
    sudo /usr/sbin/brctl delbr br0
    sudo route add -host $IP dev eth0
    sudo ifconfig eth0 down && sudo ifconfig eth0 up
    else
    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 10.1.1.50 up
    sudo bash -c ‘echo 1 > /proc/sys/net/ipv4/conf/tap0/proxy_arp’
    sudo route add -host $IP dev tap0
    sudo arp -Ds $IP eth0 pub
    fi

    airtonix

    4 Sep 09 at 5:35 am

  12. [...] I was looking for. After installing these two utilities and running the script, provided at this blog, I was able to make the WIndows XP Guest OS join the 192.168.1.0 network! Perfect. Now, all I had [...]

Leave a Reply