VirtualBox host to guest networking

I’m creating a new development server on VirtualBox. I was using VMWare until recently, but since upgrading to Ubuntu 9.04 64bit, I’ve decided to try VirtualBox instead. I also recommended VirtualBox to my brother, so by using it myself I’ll be better able to support him if he has any issues.

Installing a new virtual machine was a breeze. After I activated hardware virtualisation in my bios, I installed a 64bit version of Ubuntu server 8.04 LTS. The install failed a couple of times, not sure why, but third time lucky.

My first major stumblingĀ  block was connecting to the virtual machine from the host machine. By default VirtualBox gives the guest (virtual machine) a NAT ethernet connection. So the guest can connect to the network, including the internet, but the host can’t connect to the guest. I’m creating a development server, so that’s precisely what I want to do, connect from the host to the guest. With a little reserach, it turns out there’s an easy solution (on Linux hosts).

The VirtualBox article on Advanced Networking in Linux was my guide. I’ll document all the steps I took here.

Install bridge-utils, vtun and uml-utilities:

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

Create the bridge:

sudo brctl addbr br0
sudo ip link set up dev br0
sudo ip addr add 10.9.0.1/24 dev br0

Create a tap device for the guest to use, put your username in place of USER:

sudo tunctl -t tap0 -u USER
sudo ip link set up dev tap0
brctl addif br0 tap0

If you need multiple guests connected, repeat this step replacing tap0 with tap1, tap2 and so on. Always use br0.

Now modify the virtual machine settings and map one of the network adapters (probably the second one) to the device tap0. Choose Attached To Host Interface and select the device tap0. I left the first network adapter as a NAT adapter so the virtual machine has internet access. In this configuration, I can disconnect the guest from the internet and / or the host separately.

When the virtual machine has started, setup the network. Assuming the guest is an Ubuntu machine, run these commands on the guest. If you linked the first network adapter to tap0 then use eth0 on the guest, if you chose the second network adapter use eth1, 3 to eth2, 4 to eth3 andĀ  so on.

sudo ip link set up dev eth1
sudo ip addr add 10.9.0.2/24 dev eth1

Now test it all works. On the host machine try ping -c4 10.9.0.2 and on the guest try ping -c4 10.9.0.1. Assuming both machines are set to respond to pings (default in Ubuntu), you should see 4 successful pings.

If this works, you can set the address permanently by editing /etc/network/interfaces and adding this text.

# Host only network
iface eth1 inet static
address 10.9.0.2
netmask 255.255.255.0
network 10.9.0.0
broadcast 10.9.0.255

I’ve used the 10.9.*.* addresses as an example. You can use any private network address (10.*.*.*, 192.168.*.* or 172.16.*.*-172.31.*.*). The most commonly used addresses are 192.168.*.* and 10.0.*.* or 10.1.*.* so I recommend staying away from them. You want to choose addresses that won’t clash with anything else on your network.

This entry was posted in Open Source, Techy and tagged , , , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

3 Comments

  1. Pepe
    Posted 28 Oct 2009 at 17:34 | Permalink

    Hello Sensei… I’m not sure when, but I will try Ubuntu soon. I found OSCAR which is an opensource hospital information solution and runs on Ubuntu.
    See you soon in my Linux and opensource adventures

    • Posted 28 Oct 2009 at 19:39 | Permalink

      Wonderful news sensei. If you need any Ubuntu or linux related help, my lines are always open for you my friend. :-)

  2. Posted 08 Feb 2010 at 07:46 | Permalink

    Good Job, thanks

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

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

  • Categories

  • History