How to set up host interface networking for VirtualBox on Ubuntu
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.

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
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
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
Do you use a script to remove the tap0?
indigo196
18 Nov 08 at 1:13 pm
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
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
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