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
auto eth1
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.
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:
#!/bin/bash
# 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.
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
Wonderful news sensei. If you need any Ubuntu or linux related help, my lines are always open for you my friend.
Good Job, thanks
exactly what I’m looking for… I’ll try it later… thx very much
Amazingly easy instructions, thx a bunch!!!!
Thanks Callum!
Your guide was concise and worked perfectly. You described how to make the changes persistent by defining the interface in the guest’s interfaces file in /etc/network. I did that and when I rebooted my guest OS, eth1 was not present when I did an ifconfig. I then did ifconfig eth1 up, but it did not have an ip address defined. I am running ubuntu server 9.10 as the guest and ubuntu desktop 9.10 as the host. I verified that my /etc/network/interfaces addition was the same as what you posted, but the changes still do not persist between GUEST restarts. (I attached eth1 to tap0 like you suggested)
I can create a script to do…
Also, is there a need to change any config on the host to make the creation of the bridge and the tap persistent?
Thanks again for the great guide.
Kevin
It’s been a while since I set this up myself. I honestly can’t remember if or how I automated the brctl and tunctl stuff. I probably stuck it in a script somewhere, I’m not sure. You could look into running scripts automatically on startup and then put it in a script, I think that’ll work, but I’m not sure. Best of luck with it. Feel free to post back here if you find a way to get it working.
I finally got virtualbox up and running again, but I’m still running 8.04 as the guest. The changes are persistent and last across sessions. Here’s what I have in /etc/network/interfaces:
# Host only networkauto eth1
iface eth1 inet static
address 10.9.0.2
netmask 255.255.255.0
network 10.9.0.0
broadcast 10.9.0.255
Do have the auto eth1 part? I realise I missed that from my howto. Or did you find a solution already?
I have not gotten it working already. I am still using the exact steps from your guide, but I put them into scripts that I execute prior to launching the VirtualBox software. It works perfectly, but I’ll try what you posted here, when I get home, and let you know how I fare with it.
Thanks for the follow-up
I recently updated to Ubutnu 10.04 becausae of it’s LTS, but since that time VBox does not start when I setup the bridge network. I have to disable eth1 to boot virtual system…
I took a screenshot of the error message
http://files.tomasjancik.net/Screenshot.png
I tried to modprobe the modules, but still it didnt work…
can you help?
Apologies for the delayed reply, your message slipped into oblivion in my inbox. I recently set up Virtualbox on 10.04 and it works ok for me. As I read your error message, it seems like your tap0 interface doesn’t exist. Check the virtual machine settings, maybe you have network adapters mapped to the machine which no longer exist?
Callum
Thanks for your contribution. I have a slight problem which is also replicated on the virtualbox documentation; on which machine do I type the above commands- ie the guest or the host? Please help.
I’m not sure which commands you’re asking about. The first part is on the host, the second the on the guest. The last two code blocks are on the guest, everything else is on the host.
You can also simply use the “Bridged Adapter” instead of or in addition to the default NAT adapter.
The guest should then be able to fetch an IP from the hosts network and you can talk to the guest using that IP.
Worked easey peasey for me (Winblows host, Ubuntu10 guest)
That approach only works if you have a live network connection on the host. I have a development server on my laptop so I like to be able to work whether or not I have a wifi or ethernet connection. Also, as I switch connections, I’d have to change the bridged settings depending on which network card I’m using. It is a simpler option when the host has a continuous network connection.
Pingback: Week in Review – 2010-15
How do you do this with Ubuntu host and WinXP guest?
No idea, you might find a new network adapter pops up in Windows XP and you can simply set the network address for it. Don’t know.
Glad Google led me to this when I’d been beating my head against the wall after several days of trying to set up a hostonly network between a Ubuntu 10.04 server host and a Linux-based routerOS base.
A major part of the problem for me was that the VirtualBox manual clearly says:
“when host-only networking is used, VirtualBox creates a new software interface on
the host which then appears next to your existing network interfaces.”
Equally clearly VirtualBox doesn’t create the interface on the host – the user has to do it, as per the above. Having mistakenly assumed that VB had created the virbr0 interface that appears in Ubuntu’s ifconfig I spent hours trying to connect to it!
Another little wrinkle that tripped me up – when creating the hostonlyadapter it has to carry the same number as the nic it is being associated with, ie nic3 hostonlyadapter3 even if it is the first or only hostonlyadapter.
Glad the article was helpful and you got your problem sorted.
Hmmm. Only part sorted, I’m afraid. The next time I booted the host all the interfaces created on the host in accordance with your instructions had vanished and I had to set them up manually again, at which the guest booted up and the hostonly network established itself.
How do I make the br0 and tap0 interfaces persistent on a Ubuntu Server 10.04 host?
And, as an aside, what is the virbr0 interface Ubuntu creates by default for, if not for this?
I’ve never figured out how to automate the br0 and tap0 interface setup on the host. I think a simple bash script run as root on bootup would do it, but I haven’t looked into it. I’m not sure what virbr0 is, don’t think I’ve ever seen it on my setup.
Thanks for a good article
Now i am able to configure my host to guest networking..& able to access guest os from host .
I have set up two machines using Ubuntu 10.10 like this, except I left out the tap interface entirely. Instead, I told the host to bring up a bridged network called ‘vnet0′ (instead of br0), and I told virtualbox to bridge directly to vnet0. This works nicely whether my host’s “real” network connections (eth0 or wlan0) are up or not. I usually NAT the vnet0 traffic out through eth0 or wlan0 (using 1 line in a shorewall ‘masq’ file).
I am curious why you needed a tap interface on your bridge. What purpose does it serve?
The plot thickens. I am now trying to duplicate this setup on a Linux Mint Debian Edition box, and something is blocking some of my packets. For example, my winxp guest sends a DHCPDISCOVER to the vnet0 interface to ask for an IP address. My DHCP (running on the host) server sends a DHCPOFFER back onto vnet0, but the winxp guest never sees it. I am still debugging this. If you have ideas, I’d love to hear them.
Thanks for sharing your experience… I will experiment with the tun setup that you describe, and see if it helps.
Alan Porter
I still don’t understand why I did not need the tap interface on the Ubuntu boxes, but I do need it on the Mint one. But I am very happy that your solution worked for me.
I was able to automate the creation of the interfaces. This is my /etc/network/interfaces file:
# interfaces(5) file used by ifup(8) and ifdown(8)
auto lo
iface lo inet loopback
# eth0 is controlled by NetworkManager
#auto eth0
#iface eth0 inet dhcp
# VirtualBox TAP device bridge
auto tap0
iface tap0 inet manual
up ifconfig $IFACE 0.0.0.0 up
down ifconfig $IFACE down
tunctl_user alan
# VirtualBox NAT bridge
auto vnet0
iface vnet0 inet static
address 172.20.20.1
netmask 255.255.255.0
#bridge_ports none
bridge_ports tap0
bridge_maxwait 0
bridge_fd 1
bridge_stp off 1
up ip link set vnet0 qlen 1000
up iptables -t nat -I POSTROUTING -s 172.20.20.0/24 -j MASQUERADE
down iptables -t nat -D POSTROUTING -s 172.20.20.0/24 -j MASQUERADE
I hope this helps others.
Alan
Thanks for sharing. So you’re able to use the VirtualBox NAT connection to talk from the host to the guest, is that correct? It’s a long time since I set all this up, but as I recall, I set it up because when my external networks were down (eth0 wlan0) I was unable to reach my guest machine. I was using the guest machine as a dev web server, so working without internet became impractical. Maybe VirtualBox has changed slightly and now I can use a NAT connection instead, or maybe I missed something originally.
Actually, no. I am using a single bridged connection from virtualbox to my ‘vnet0′. I do not use virtualbox’s NAT at all. Then I use dnsmasq to do DHCP and DNS and I use shorewall to do NAT to my hosts eth0 or wlan0 (whichever one happens to be in use at the time).
My primary motivations were:
(1) I could start my VM’s whether my host’s eth0 or wlan0 were up or down. And I could move my laptop, unplug eth0, start using wlan0, and the VM’s would be unaffected. I would never need to change their bridge connection just because the host had changed the way it connected to the world.
(2) I could use dnsmasq to keep track of what winxp was doing, and I could also insert my own fake DNS entries, like “doubleclick.net = 127.0.0.1″, etc.
(3) Networking is cool.
A note about my example interfaces file above… the qlen line is not needed… that was an experiment. And you may have to get rid of the commented-out “bridge_ports none” line.
Alan
Ok, now I understand. Thanks.
I suppose that you need to manually change the NAT configuration of vnet0 according to which interface is up, is that correct? Or does the NAT routing automatically switch to whichever is the “live” outgoing network? If it does, I suppose that’s a simpler solution. My setup does allow me to disconnect my guest machine from the internet while still keeping it available from my host. But that’s not something I ever remember doing…
Shorewall allows me to NAT through whichever connection is active. My /etc/shorewall/masq file looks like this:
eth0,wlan0 172.20.20.0/24
If I were doing the same thing using an iptables rule, I would simply do this:
iptables -t nat -I POSTROUTING -s 172.20.20.0/24 -j MASQUERADE
You do not have to specify which interface you’re masquerading (NAT-ing) to. It’ll figure that out based on the destination and the routing tables.
Alan