VirtualBox: Access Guests via a Virtual Interface | Comments (13)
Posted in Linux/Unix on 8th April 2009, 9:50 am by Stuart
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!

[...] Virtualbox OSE – is carrying out Virtualization duties – though I wish that it was easier to communicate with headless VMs I’m used to being able to SSH into VMs straight away having used VMware Fusion on OSX. Hopefully this is something that will be addressed in future versions or something I can resolve as I get more familiar with it’s networking configurations. The version of VirtualBox OSE in the upcoming Ubuntu release (Jaunty) is newer so I looking forward to trying that out. Update: I’ve fixed the issues with networking using a virtual interface to access VirtualBox guests. [...]
As of VirtualBox 2.2+ the second interface on your VM is connected to the TAP interface via the “bridged adapter”. Simply choose that and then select the TAP interface from the second drop-down.
See this screenshot for more info
Dude, finding this post has stopped me going mad.
I have been battling with this for a couple of days!
I have followed your set-up as above but I still can seem to connect to my VM. I am running Ubuntu and the VM is Ubuntu.
I have replicated it even down to the 172.16.1.2 name
As an aside you can connect to the host from within the VM by it’s own IP or 10.0.2.2
Hope you can help or point me in the right direction!
Mark
@Mark:
1. Can you ping your VM from the host machine on the ip address?
2. Presumably there’s no iptables rules preventing communication?
3. What error messages are you seeing when trying to connect?
4. Can you see eth1 listed in the output of ifconfig on the guest?
5. Can you see the tap interface listed in the output of ifconfig on the host machine?
Hi,
Heres the strrange thing:
1, I cant ping the VM from the Host but I can connect to the Host from the VM…
2, Thanks for the pointer on the iptables, I will check it out.
3, The error when connecting to the VM from the Host is a network timeout.
4, Ah… No, I cant perhaps I need to set this up in /etc/network/interfaces?
5, I can see the tap0 in the host box.
Looking through the answers above, do you think the issue maybe I am missing the network point on the VM?
Thanks a million for your help so far
Mark
@Mark:
>Looking through the answers above, do you think the issue maybe I am missing the network point on the VM?
Indeed you need to configure eth1 on your VM in /etc/network/interfaces as per the instructions in the post.
Any chances of a how to for those trying to run ubuntu guest on Vista host?
Hi,
Yep, you’re right more haste less speed I think is the saying
Thanks again!
Mark
@poldim: I’m pretty sure you should be able to do something similar on a vista host – unfortunately I don’t have a vista installation to be able to work something out. If you do figure something out feel free to come back here and drop a link to a solution in the comments.
@Mark: Glad you got it working!
[...] In a previous post I wrote about using a TAP interface to be able to SSH into linux guests from the …. Since then changes in the newer versions of VirtualBox have seen the addition of a virtual interface on the Host by default. This post details how to utilise this to allow communication from your host to your guests by simply configuring a second network interface on the guest. [...]
You cannot imagine how thankful I am. Damn lost 6 years of my life
getting this to work. The best!!!!! Many many thanx
Hi,
I’ve followed all the given steps. But I still am not able to connect between the host and the VM. I’m just trying to ping between them to check for connectivity, but in vain.
Can anyone please suggest any possibilities?
Thanks in advance…
thanks exactly what I was looking for I agree this is the best way to go for doing this.
Only addition I had to make is choose bridged adapter vs host networking, as someone mentioned above.
also on my ubuntu virtualbox runs as my own user