Update: the non-opensource edition of VirtualBox 2.2, still not included in Jaunty, will enable you to skip all of this hassle; it’ll set up a bridged connection for you automatically. However, for 2.1, this still applies.
Many of you may know the OpenSource alternative to the proprietary VMWare solution. Sun’s VirtualBox is very fast and feature-complete enough to substitute most of VMWare’s functionalities. I use it at work since more than a year with wonderful results, both to virtualize Windows under GNU/Linux, or GNU/Linux under Windows.
However, network configuration isn’t as obvious as it should, since NetworkManager doesn’t support bridge-ing interfaces by default. Herein, you’ll find a short tutorial on how to enable your virtual machine to access the Internet when hosted inside a GNU/Linux OS using NetworkManager.
Installing VirtualBox and other needed packages
Of course, we’ll need VirtualBox. You can install it directly from the official repositories. Other than that, we’ll use also a small package which will help us to set the DNS addresses in /etc/resolv.conf automatically directly via /etc/network/interfaces, saving us time and headaches.
To install the four needed packages, you can use Synaptics (via the System → Administration menu), or fire up a terminal and type:
sudo apt-get install virtualbox-ose resolvconf bridge-utils uml-utils
Once finished, you’ve all you need to proceed.
Preparing the network interfaces
We’ll be forced to “disable” NetworkManager, and to manage interfaces directly via /etc/network/interfaces. First of all, we’ll need to be in the right group to use the tunnel interface (/dev/net/tun). In a terminal, type:
sudo gpasswd -a 'yourusername' uml-net
Needless to say, substitute “yourusername” with your login name. Then, in a terminal, type:
gksudo gedit /etc/network/interfaces
(or, if you’re more experienced, use vim instead of gedit). Here’s an example of my interfaces file using a static IP configuration:
auto lo iface lo inet loopback auto br0 iface br0 inet static bridge_ports eth0 tap0 address 192.168.1.15 netmask 255.255.255.0 gateway 192.168.1.1 dns-nameservers 192.168.1.100 126.96.36.199 dns-search default.domain auto tap0 iface tap0 inet manual tunctl_user matteo auto eth0 iface eth0 inet manual up ip link set $IFACE promisc on down ip link set $IFACE promisc off
As you can see:
- The loopback device (lo) is always present. Don’t touch it.
- We’ve set eth0 to be manually managed, and to be in promiscuous mode. If your primary interface is eth1 (for example, a wireless network card), you can add it/substitute it to eth0. Notice how we don’t assign an IP address to the physical interface: only the bridge will have one.
- We’ve set up a new tap0 interface, which will be our tunnel to the virtual machine. Remember to change the username with yours.
- We created a bridge br0 with eth0 and tap0 as ports. We then proceeded to set up the interface with an IP address, a gateway, a netmask, and (thanks to the resolvconf package) the DNS servers. You can delete the line about the domain search if you don’t need it. It’s mostly useful when you’re under a Windows domain, to resolve other machine names in a short form (e.g. “pluto“) instead that with their FQDN (e.g. “pluto.somedomain.com“).
Mind you, if you’re using dhcp, the part about br0 becomes just:
auto br0 iface br0 inet dhcp bridge_ports eth0 tap0
Now let’s restart the needed services (or, you can restart your machine):
sudo /etc/init.d/networking restart sudo killall nm-system-settings sudo /etc/init.d/NetworkManager restart
From now on, NetworkManager will show you the “disconnected” icon. That’s okay. You can remove the NetworkManager service from startup if you don’t like it, for example using rcconf; however, my advice is just to ignore that icon.
Open a browser and visit some website. Can you reach it? If you can, you’ve done the most!
Setting up VirtualBox
Before commencing, remember to add your user to those which can create and manage virtual machines. From the System menu, select Administration → Users and Groups, and then add the right permission to you.
Mostly, setting up VirtualBox depends on what you want to achieve. Fire up VirtualBox from the Applications → Accessories menu, and create a new virtual machine image.
When you’re done through the wizard, select the newly created image from the list and select “Settings” from the toolbar. Customize what you want, and then jump to the “Network” section.
Make sure the first network “card” is enabled, and that it is attached to a host interface. Under “Interface name“, type “tap0“.
You can now save the configuration by clicking on “Ok“, and start the virtual machine. The only thing you need to do now is to set up the network options under your virtualized OS.
That’s all, you should be able to surf the web as long as the host machine can!