VirtualBox: Access Guests via a Virtual Interface

Note: I'm using VirtualBox 2.1.4 PUEL edition. With other versions YMMV. See comments for more info.

Note: If you're using VBox 3.x then Howto: SSH into VirtualBox 3 Linux Guests is more up to date.

On Ubuntu I've been using VirtualBox as the solution for cross-browser testing as well as a place for creating sandboxed Hardy VMs for testing Project Fondue infrastructure changes. For example I'm currently testing out how to migrate from Trac to Redmine so VM's are the perfect way to carry out a dry-run of the installation and migration.

One of the difficulties I'd had was getting the networking of the Hardy VM set-up in a way that works so that I can access the outside world and access all services of the Guest OS. With NAT it's possible to run various commands so you can forward ports from the host to the guest vm but this is a bit of annoyance if you have to set-up port-forwarding for every service you need to access on your VM.

I'd also tried Host networking but this didn't really work out, I like to be able to access VMs when I'm not attached to a network so having a Host networking configuration that relies on setting-up the VMs as if they're on the local network I'm attached to at the time doesn't really cut it as soon as I'm trying to interact with a VM on the train for example.

With VMware fusion on OSX it was really easy to access the Guest from the host via the vmnet8 interface. Having had it easy I was looking for a similar solution for VirtualBox. Fortunately I found this post on using TAP interfaces to make connections to the Guest very straightforward. The following is based on that post expanding on some of the steps with further code examples and explanations as necessary.

Howto: Access SSH etc with virtual interface

First step is to install the ulm-utilities which make it possible to create virtual interfaces:

sudo apt-get install uml-utilities

Next on the host set-up a TAP interface in /etc/network/interfaces as follows:

auto tap0
iface tap0 inet manual
    up ifconfig $IFACE 172.16.1.1 netmask 255.255.255.0 up
    down ifconfig $IFACE down
    tunctl_user <user>

The tap interface is a virtual interface as explained here.

Replacing <user> for your username. For the tap interface to work add yourself to the vboxusers groups: sudo usermod -a -G vboxusers <user>

You can bring-up the tap0 interface by running: sudo ifup tap0
Running ifconfig on the host should show the tap0 interface.

In the settings for the Guest set-up the second interface to use host networking and select the tap0 interface:

Next boot-up the guest and test outbound networking by pinging google.co.uk. If that's all good add a second interface to /etc/network/interfaces on the guest:

auto eth1
iface eth1 inet static
    address 172.16.1.2
    netmask 255.255.255.0

Restart the guest and you should be able to see the guest's apache (assuming apache is installed) or ssh (you have installed openssh-server right?) in via 172.16.1.2.

NAT should should also still be working for outbound net access.

Now you have the tap0 interface on your host you can add more VMS and set them up accordingly with unique ip addresses and you can still access your VMs from your host when offline. Winner!

Show Comments