VirtualBox host to guest networking

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
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.

47 thoughts on “VirtualBox host to guest networking

  1. 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

  2. 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…

    … easily enough, but it would be nice to persist it.

    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 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

        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

    • 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?

  3. 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.

  4. 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.

  5. Pingback: Week in Review – 2010-15

    • 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.

  6. 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.

  7. 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.

  8. Thanks for a good article

    Now i am able to configure my host to guest networking..& able to access guest os from host .

  9. 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

  10. 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

  11. Just what I was looking for, work perfectly. I now have a fast connection between my virtual windows and my debian host

    Thanks
    Adam

    • I think the newer versions of VirtualBox have host to guest support in-built. I’m using VirtualBox’s own network adapters since upgrading to Ubuntu Precise / VirtualBox 4.1.8_Ubuntu.

  12. Hello Callum,
    I have installed Windows 7 ultimate in my PC and in that I installed Virtual Box . In the virtual box i installed Oracle Enterprise Linux 5.5. I have done the bridge networking. Now I cant ping the host or guest from either side. Could you please help me how to configure the network so that I can ping both the OS from either end.

    Looking forward for your reply at the earliest.

    Best Regards,
    Raxun

    • I know nothing about a Windows host. My setup is all linux. I no longer use the method described here because VirtualBox now includes this functionality as standard (at least on a Linux host).

  13. hello…
    great tutorial, but i had some problem…
    i can’t connect to my host from guest os..

    any idea ?

  14. Hi !

    thanks for the info.

    To sum up, i installed an ubuntu 12.04, and virtual box.
    I need a VM, with 2 eth.

    The frist eth (eth0 on vm) is my wifi (wlan0 on host ), the second is usually the rj45 (eth1 on vm, eth0 on the host).
    The trick is that, sometimes, no wifi is allowed at customer, this means no more eth0 on vm o((((

    So, i created a tap0, linked to virbr0, for my interface.

    thanks, thanks, thanks o)))
    /Rava

  15. Could you please suggest on the following Issue that I am facing with the VirtualBox connectivity with the Host Server.. Thanks in Advance,

    I ‘ve installed Virtualbox 4.2.10 on RHEL 6 and installed guest OS RHEL 6 on it. I ‘ve configured NAT & Host-only adapter in order to established the connection with the Virtualbox from the Host OS.

    The issue is that the Host machine is able to connect to Virtualbox i.e on which the virtualbox is installed But not vice versa i.e Failed to ping the server from Virtual machine.
    even though Virtual machine can connect to Internet but not able to ping the server. there is no ISsue in sharing a folder from the Host OS to virtaulbox . able to mount share directory from the Guest OS i.e from VirtualBox.
    i ‘ve disabled the SELINUX in both the machine as well.

    when i ping the Host machine from the virtualBox . ping ebs1.oracle.com or ping IP it says Destination host unreachable. But can ping the Virtual machine from the Host.

    Please see the following configuration of /etc/hosts & /etc/resolv.conf of both the machine.
    Host OS which is ebs.oracle.com & Guest OS i.e VirtualBox which is ebs1.oracle.com. And the IPs of both are : 192.168.1.1 And 192.168.1.8 & the DNS which i ‘ve configure is 192.168.1.1.
    [root@ebs Desktop]# vi /etc/hosts
    [root@ebs Desktop]# cat /etc/hosts The same configurtion in VirtualBox /etc/hosts file as well
    127.0.0.1 localhost.localdomain localhost
    192.168.1.1 ebs.oracle.com ebs
    192.168.1.8 ebs1.oracle.com ebs1

    [root@ebs Desktop]# cat /etc/resolv.conf The same configurtion in VirtualBox /etc/resolv.conf file as well
    # Generated by NetworkManager
    search nameserver
    nameserver 192.168.1.1

    Could you please suggest why the Guest OS i.e Virtual machine is not able to ping the HOST OS despite getting the shared folder or no issue in mounting the shared directory from the Virtualbox.

    what i am trying to achieve is to esatablish a connection between the Linux OS Host And VirtualBox
    for that i ‘ve assigned an IP of 192.168.1.1 to linux OS Host and assgined a DNS 192.168.1.1 which is same as IP of the Host OS through netconfig utility.
    Please see the following.
    [root@ebs Desktop]# ifconfig
    eth0 Link encap:Ethernet HWaddr 00:1E:67:22:2D:6E
    inet addr:192.168.1.1 Bcast:192.168.1.255 Mask:255.255.255.0
    UP BROADCAST MULTICAST MTU:1500 Metric:1
    RX packets:0 errors:0 dropped:0 overruns:0 frame:0
    TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
    collisions:0 txqueuelen:1000
    RX bytes:0 (0.0 b) TX bytes:0 (0.0 b)
    Memory:d0100000-d0120000

    eth1 Link encap:Ethernet HWaddr 00:1E:67:22:2D:6F
    inet addr:192.168.1.2 Bcast:192.168.1.255 Mask:255.255.255.0
    inet6 addr: fe80::21e:67ff:fe22:2d6f/64 Scope:Link
    UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
    RX packets:79359 errors:0 dropped:0 overruns:0 frame:0
    TX packets:64585 errors:0 dropped:0 overruns:0 carrier:0
    collisions:0 txqueuelen:100
    RX bytes:65810295 (62.7 MiB) TX bytes:12737639 (12.1 MiB)
    Memory:d0000000-d0020000

    lo Link encap:Local Loopback
    inet addr:127.0.0.1 Mask:255.0.0.0
    inet6 addr: ::1/128 Scope:Host
    UP LOOPBACK RUNNING MTU:16436 Metric:1
    RX packets:9325 errors:0 dropped:0 overruns:0 frame:0
    TX packets:9325 errors:0 dropped:0 overruns:0 carrier:0
    collisions:0 txqueuelen:0
    RX bytes:18942730 (18.0 MiB) TX bytes:18942730 (18.0 MiB)

    vboxnet0 Link encap:Ethernet HWaddr 0A:00:27:00:00:00
    inet addr:192.168.1.8 Bcast:192.168.1.255 Mask:255.255.255.0
    inet6 addr: fe80::800:27ff:fe00:0/64 Scope:Link
    UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
    RX packets:0 errors:0 dropped:0 overruns:0 frame:0
    TX packets:1014 errors:0 dropped:0 overruns:0 carrier:0
    collisions:0 txqueuelen:1000
    RX bytes:0 (0.0 b) TX bytes:87654 (85.5 KiB)

    virbr0 Link encap:Ethernet HWaddr E2:A1:32:D0:FB:8E
    inet addr:192.168.122.1 Bcast:192.168.122.255 Mask:255.255.255.0
    UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
    RX packets:0 errors:0 dropped:0 overruns:0 frame:0
    TX packets:1750 errors:0 dropped:0 overruns:0 carrier:0
    collisions:0 txqueuelen:0
    RX bytes:0 (0.0 b) TX bytes:85491 (83.4 KiB)

    As the machine having 2 Ethernet cards so it Automatically assigned the eth1 IP to 192.168.1.2 . And virbr0 inet addr is ISP assigned address as the server is connected to internet
    through a modem. And vboxnet0 is VirtulaBox & whose IP is :192.168.1.8.

    Could you Please suggest How could both the Host & guest OS which virtualbox can communicate i.e act like two Server which is connected as I wanna Install IDM database on
    the Virtualbox machine which is going to be Integrated with EBS R12 database which is installed on the Host OS.

    Is it the Issue with the DNS which same as IP add of the Host OS ?

    Could you please suggest what i need to do in order to make the connection between Host OS & VirtualBox,

    Thanks & Regards
    Priya

  16. I ‘ve been really stuck up trying to established a connection between
    the Host OS & VirtualBox, Really Appreciate the help!! I ‘ve tried the solution that you suggested here but to no avail.

    I ‘ve installed Virtualbox 4.2.10 on RHEL 6 and installed guest OS
    RHEL 6 on it. I ‘ve configured NAT & Host-only adapter in order to
    established the connection with the Virtualbox from the Host OS. and The Host server is connected to Internet through a modem not to any Office network as this is a Personal server.

    The Host OS IP is 192.168.56.8 & its DNS is 192.168.56.1 which i ‘ve assigned through the system-config-network utility on RHEL 6. and followed the instructions for the Guest VM IP given here. i.e guest configured
    NAT & Host-only adapter for the Virtualbox given here. and also checked with the ifconfig command that
    the IPs assigned correctly.
    The Guest Virtualbox IP: 192.168.56.100 & DNS 192.168.56.1 which i ‘ve assigned through the system-config-network utility on RHEL 6.

    But still I can not ping the Host OS from the Guest OS i,e from the VirtualBox
    when i ping the host OS it says that the Network unreachable.

    The entries in the /etc/hosts & /etc/resolv.conf for the Host IPs and the DNS which is same as that of HOST OS is there. But it failed to establised connectivity from the Guest OS to Host OS. although i can ping the
    guest OS from the Host OS.

    Could you please suggest what i need to do inorder to establish connectivity from Guest OS to Host OS.

    Thanks
    Priya

    • I’m not very familiar with all this stuff. I posted what worked for me at the time, but since then I’ve moved to using 1 NAT adapter and 1 host-only adpater. That “just works” for me. If my laptop is offline, the NAT adapter doesn’t work, but the host-only does. I did all of this without TAP / TUN or otherwise. Best of luck finding a solution.

  17. Hello Callum
    Very good article but there is a problem for me.
    I am using virtual box 4.2.8 with ubuntu 12.04 host and ubuntu 11.10 guest. I am unable to setup bridge networking beetween host and guest. here i am pasting configuration of interfaces.
    Configuration on Ubunutu12.04 host
    auto lo
    iface lo inet loopback

    # Configure the bridge
    auto br0
    iface br0 inet static
    address 192.168.56.22
    netmask 255.255.255.0
    broadcast 192.168.56.255
    gateway 192.168.56.1
    # Ports you want to add to your bridge
    bridge_ports eth0
    # Time to wait before loading the bridge
    bridge_maxwait 0

    auto eth0
    iface eth0 inet manual

    And for ubuntu 11.10 guest, i am selected bridged adapter and under name i selected eth0 interface.
    And configuration of guest ethernet interface is

    auto eth2
    iface eth0 inet static
    address 192.168.56.23
    netmask 255.255.255.0
    broadcast 192.168.56.255
    gateway 192.168.56.1

    But it is not working when i ping it just says destination host unreachable. Please help

      • actually what i want to do is, i want to install two vm on a ubunut host. specially i am intrested in bridging because i want to communication between both vm in such a way so that packet going from one VM to another VM can be sniffed using host machine. Is it possible using host only adapter if possiblre please guide me to right way?

Leave a Reply

Your email address will not be published. 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>