This post describes how to set up host networking for VirtualBox on Ubuntu 7.04 Feisty. The procedure probably works for older versions of Ubuntu.

The outcome of the procedure is that you’ll have a second network card for your virtual machine, which you can configure from inside the virtual machine as if it was a real network card connected to the same ethernet as your real network card.

I assume that you either

  • get your IP address with DHCP
  • or have a static IP address

If you’re on DSL or cable modem with PPPoE you probably don’t want host networking, but NAT, which works out of the box.

We’re going to

  1. create a virtual network interface
  2. set up an ethernet bridge
  3. add your physical network interface and the virtual interfaces to the bridge

Preparations

At first let’s install the needed software:

$ sudo apt-get install bridge-utils uml-utilities

The virtual network interface is configured with the package uml-utilities. uml stands for User Mode Linux, so understandably the package is preconfigured for use with User Mode Linux. In order to work correctly with VirtualBox, in /etc/network/if-pre-up.d/uml-utilities replace

chown root:uml-net /dev/net/tun

with

chown root:vboxusers /dev/net/tun

As we’re going to change the network configuration, we’re going to shut down the network. So don’t close this browser window :)

$ sudo /etc/init.d/networking stop

Creating a Virtual Network Interface

Now on to configuring the virtual network interface. Ubuntu and Debian store network configuration in the file /etc/network/interfaces. Assuming your network card is eth0, you’ll find a stanza beginning with the following in the file:

auto eth0
iface eth0 inet .....

Actually at install Ubuntu writes several such stanzas into the file so that they automatically work. Create a backup copy of /etc/network/interfaces (Now.) Then comment out all interfaces you don’t use so that NetworkManager doesn’t change the network configuration when we don’t want to. Leave in the lo interface, it’s required for local IP communication.

auto lo
iface lo inet loopback

auto eth0
iface eth0 inet .....

#auto eth1
#iface eth1 inet dhcp

#auto ath0
#iface ath0 inet dhcp

#auto wlan0
#iface wlan0 inet dhcp

The next step is to create a virtual network interface. This is the network interface that will be used by the virtual machine. We don’t configure it – the virtual machine will do it. Add the following stanza under the lo interface:

auto tap0
iface tap0 inet manual
tunctl_user markus
uml_proxy_arp markus.mynetwork.loc
uml_proxy_ether eth0

Replace markus.mynetwork.loc with the host name or IP address you get from DHCP for your host machine (not the virtual machine). Also replace eth0 with your real network interface, if that is not eth0, and markus with your user name on the system. The user name set here will be the only user with permission to work with the virtual network interface.

Static IP address

If you have a static IP address, replace markus.mynetwork.loc instead with your static IP address.

Creating the bridge

Now we’ll set up the bridge. Add the following stanza after the tap0 stanza:

auto br0
iface br0 inet dhcp
bridge_ports eth0 tap0
bridge_maxwait 0

Static IP address

If you have a static IP address, you have to carry over the network settings from eth0 to br0. Assuming your eth0 stanza looks something like this:

auto eth0
iface eth0 inet static
address 192.168.1.2
netmask 255.255.255.0
gateway 192.168.1.254

change the br0 stanza to look like this:

auto br0
iface br0 inet static
address 192.168.1.2
netmask 255.255.255.0
gateway 192.168.1.254
bridge_ports eth0 tap0
bridge_maxwait 0

In any case, change the stanza for eth0 to the following:

auto eth0
iface eth0 inet manual

That’s it. Enable networking again:

$ sudo /etc/init.d/networking start

You can use ifconfig -a to see if all devices were correctly configured. You can now use host networking in VirtualBox. Enter tap0 in the interface name field. The fields setup application and terminate application can be left empty.

More than one virtual network interface

You can run more than one OS at the same time and have all them use host networking. Add a tap interface for all of them. Here’s an example for three virtual network interfaces:

auto tap0
iface tap0 inet manual
tunctl_user markus
uml_proxy_arp markus.mynetwork.loc
uml_proxy_ether eth0

auto tap1
iface tap1 inet manual
tunctl_user markus
uml_proxy_arp markus.mynetwork.loc
uml_proxy_ether eth0

auto tap2
iface tap2 inet manual
tunctl_user markus
uml_proxy_arp markus.mynetwork.loc
uml_proxy_ether eth0

auto br0
iface br0 inet dhcp
bridge_ports eth0 tap0 tap1 tap2
bridge_maxwait 0

Have fun, and let me know if that post was helpful to you :)