Muffinresearch Labs by Stuart Colville

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:

VirtualBox Networking Dialogue

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!

Post Tools

Comments: Add yours

1. On April 8th, 2009 at 1:58 pm From OSX to Ubuntu by Stuart Colville said:

[...] 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. [...]

2. On June 18th, 2009 at 2:42 pm Stuart Colville said:

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

3. On July 5th, 2009 at 5:53 pm Mark said:

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

4. On July 6th, 2009 at 6:42 am Stuart Colville said:

@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?

5. On July 7th, 2009 at 11:39 am Mark said:

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

6. On July 7th, 2009 at 12:12 pm Stuart Colville said:

@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.

7. On July 7th, 2009 at 11:28 pm poldim said:

Any chances of a how to for those trying to run ubuntu guest on Vista host?

8. On July 8th, 2009 at 6:18 am Mark said:

Hi,

Yep, you’re right more haste less speed I think is the saying :)

Thanks again!

Mark

9. On July 8th, 2009 at 7:41 am Stuart Colville said:

@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!

10. On February 8th, 2010 at 1:11 pm Howto: SSH into VirtualBox 3 Linux Guests by Stuart Colville said:

[...] 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. [...]

11. On March 10th, 2010 at 11:00 am peter daalder said:

You cannot imagine how thankful I am. Damn lost 6 years of my life ;-) getting this to work. The best!!!!! Many many thanx

12. On March 30th, 2010 at 2:23 am Sasi Siddharth said:

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…

13. On February 12th, 2011 at 7:22 pm Damien Dell said:

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







XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>



GNU screen: open tab in current working directory|(1)

A nice trick for having screen open a new tab in the same directory as the one you’re currently in. To use it add it to your .screenrc

# Open new window in current dir.
bind c stuff "screen -X chdir \$PWD;screen^M"
bind ^c stuff "screen -X chdir \$PWD;screen^M"

Hat tip: mteckert on SuperUser.com

Ubuntu: add-apt-repository: command not found|(2)

When you’re using a minimal Ubuntu install if you find the ‘add-apt-repository’ command is missing (it’s useful for adding PPAs and other repositories), then simply run:

sudo apt-get install python-software-properties

Photos on Flickr

© Copyright 2004-12 Stuart Colville, all rights reserved. May contain traces of Muffin. Powered by WordPress. Hosting by Slicehost.com This page was baked in 0.480s.