Update 2013: This article is out of date. VirtualBox now includes a host-only network type. On my laptop I create 2 networks, one NAT to provide the VM with internet, and one host-only to provide the laptop access to the VM, even if the laptop is not on the internet.
Update: I just repeated this process with Ubuntu 11.04 host, 10.04 guest. It worked as described here. I also automated the setup on the host, and added a note at the bottom of the post explaining how I did that.
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 research, 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
sudo 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
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.
Edit: Finally, I added a script to automate the setup on the host machine. I created a script called /etc/init.d/virtualbox-bridgenetwork with the following contents:
# Create the br0 interface
brctl addbr br0
ip link set up dev br0
ip addr add 10.9.0.1/24 dev br0
# Create tap0 for the vm to connect to
tunctl -t tap0 -u USER
ip link set up dev tap0
brctl addif br0 tap0
You need to change USER to your own username and modify the IP to whatever you were using. Then to make this script run automatically at boot time, run:
sudo update-rc.d virtualbox-bridgenetwork defaults
Now the br0 and tap0 interfaces should be automatically created at boot time.