Update 2012-05-16: These instructions have been superseded by a new version of this guide. Follow that document instead.
Update 2010-08-18: These instructions are still valid as of VirtualBox 3.2.8 and Ubuntu 10.04 “Lucid Lynx”. VirtualBox now creates a “vboxnet0” interface by default, but this is not a bridge.
I use VirtualBox every day. The satellite world is infested with bad Windows-based management tools that fail to run in Wine. So I often run those apps in a Windows virtual machine, safely sandboxed the way Windows belongs.
Note to hardware developers: if your network-based device does not have a standards-compliant HTTP interface, you lose. If it has a Windows-based management tool instead, you lose twice. I will buy your product only if I have no other choice.
I imagine running Windows apps is what 90% of VirtualBox users use it for, but it can do so much more than that. I also run several Linux-based VMs, and use them to test server configs, or even whole networks before rolling out the real thing. If you do this, you probably want to use more than the basic NAT networking that VirtualBox uses by default. For instance, wouldn’t it be nice to install an SSH server in the VM, minimise the VirtualBox GUI, and SSH in from a terminal just like you would a real server?
I assume you are using VirtualBox 2.1.4 from a Linux host running Ubuntu 8.04 “Hardy Heron”. Similar commands can be used on any recent Debian or Ubuntu release. You’ll have to adapt some things to use it on RPM- or source-based Linux distributions. Assume all commands are run as root (directly or with sudo).
Step 1: Create a bridge interface
First, we have to create a bridge interface for the VMs. Install the bridge utilities:
apt-get install bridge-utils
Now make the bridge start on boot. Add the following to /etc/network/interfaces:
# VirtualBox NAT bridge auto vnet0 iface vnet0 inet static address 172.16.0.1 netmask 255.255.255.0 bridge_ports none bridge_maxwait 0 bridge_fd 1 up iptables -t nat -I POSTROUTING -s 172.16.0.0/24 -j MASQUERADE down iptables -t nat -D POSTROUTING -s 172.16.0.0/24 -j MASQUERADE
Either reboot or start it manually:
ifup vnet0
We now have a bridge interface to which VirtualBox can attach virtual machines. That traffic will be NATed to your host’s IP address when the guest OS accesses the Internet. However, the traffic won’t yet route.
Note: if you are already using a firewall such as iptables, shorewall, or ufw, you should remove the two iptables lines above and add equivalent commands to your firewall configuration. Otherwise NAT will probably not function.
Step 2: Enable IP forwarding
Now you must tell the kernel to route traffic. Find the ‘net.ipv4.ip_forward’ line in /etc/sysctl.conf, and uncomment it:
# Uncomment the next line to enable packet forwarding for IPv4 net.ipv4.ip_forward=1
And load it:
sysctl -p
Step 3: Setup DHCP and DNS for clients
OK, now you can forward and NAT traffic from client VMs. But you still have to configure static IPs in each guest’s OS. Here is where DNSMasq shines. It provides an all-in-one DHCP/DNS server in a small footprint. Install it:
apt-get install dnsmasq
And edit /etc/dnsmasq.conf to include:
interface=vnet0 dhcp-range=172.16.0.2,172.16.0.254,1h
That’s all you really need, but you may want to explicitly define DNS servers and domains for the guests, or static assignments. Add:
dhcp-option=option:dns-server,172.16.0.1,208.67.222.222,208.67.220.220 dhcp-option=option:domain-name,example.com dhcp-host=08:00:27:00:00:02,vmxp,172.16.0.2 # Windows XP dhcp-host=08:00:27:00:00:03,vmubuntu,172.16.0.3 # Ubuntu
This defines the host OS and the OpenDNS servers as the DNS servers (instead of passing on whatever your host OS uses), tells all guests they are part of the domain example.com, and defines two static assignments by MAC address.
Step 4: Set up the virtual machine
- Start the VirtualBox interface, and edit your virtual machine’s settings.
- Choose “Network”.
- Enable a network adaptor.
- Under “Attached to:”, select “Host Interface”.
- If you assigned a static DHCP assignment above, be sure to set the same MAC address.
- Under “Host Interfaces”, select the bridge you created in step 1, vnet0.
Example:
Your virtual machines will now automatically receive an IP address in the 172.16.0.0/24 network, will resolve DNS, will NAT to your host’s external IP address, and can directly address each other.
Tags: networking, virtualbox
-
Hi,
Congratulations, very nice and useful documentation :-)
Zsolt
-
I’m using Gufw for my firewall. How do I add add equivalent commands to my firewall configuration? Thanks in advance for any tips.
-
I’ll give it a shot. By the way, your instructions worked perfectly when I disabled Gufw :)
-
I ahve a question, how would this setup work with two or more virtual machines on the same physical interface ?
-
Crafty (ab)use of bridge-utils. I haven’t played with it since bridging DWL-900APs back in 2002.
The networking options GUI is different in VBox 3.0, which was just released yesterday. I was quite surprised to go to the manuals download to find 2.2.4 documentation wasn’t available and shiny new 3.0 had replaced it. Time to upgrade again!
-
thanks very much, worked first time of course!
-
Does anyone know how to do Step 1 using Fedora 11?
-
Hi
I am beginner and need some help in making inter networking between host and virtual machine.On my machine these software are installed
1. ubuntu 9.04 (Host)
2. virtual Box 3.0.8
3. ubuntu 9.04 (virtual machine)
i have assigned the IP to host eth0 using file /etc/networking/interfaces. while on virtual machine eth0 ip assigned by VirtualBox. i am replacing this IP with same class which assigned host.
The problem is that, when i was send a ping command from host to virtual machine or virtual machine to host , then response is unreached host.any tutorial or web link that helpful for me.thanks for advanced.
-
Hi,
Thanks for this tutorial.
Can you explain us how to put “vnet0” in “Adapter1″->”Host Interfaces”?
I am not sure, but do we need to do, something like this?
http://spinczyk.net/blog/2008/03/05/setting-up-a-bridged-network-for-virtualbox-on-ubuntu-linux/
-
-
68 comments
Comments feed for this article
Trackback link: https://www.tolaris.com/2009/03/05/using-host-networking-and-nat-with-virtualbox/trackback/