host-only networking for VirtualBox

November 1st, 2007
VMware came with a very useful networking feature, called host-only networking, that created a private network connection between the host and guest operating systems. This is feasible with VirtualBox, too, altough not quite as obvious. Here is described how you can do it with Ubuntu host and Windows XP guest system.

First you have to install the uml-utilities package:

sudo apt-get install uml-utilities

Then you will have to create the virtual network interface that the two operating systems will share:

sudo tunctl -t tap0 -u giannis

Of course you will have to replace "giannis" with your own username.

Now, assign the IP 10.0.1.1 to the Host system (Linux):

sudo ifconfig tap0 10.0.1.1

We will now have to make sure that VirtualBox has full access to /dev/net/tun:

sudo chgrp vboxusers /dev/net/tun
sudo chmod 660 /dev/net/tun


You have to add all users of VirtualBox in the vboxusers group. You can to that either by going to System → Administration → User and Groups, or by directly editing your /etc/group file:

sudo gedit /etc/group

Find the vboxusers group:

...
mysql:x:121:
vboxusers:x:1001:
postfix:x:122:
...


And add all the users that will use VirtualBox into it:

...
mysql:x:121:
vboxusers:x:1001:giannis
postfix:x:122:
...


Now start VirtualBox and before you boot your Windows XP, highlight your virtual machine and click on Settings. Go to Network. Find a free network adapter. Enable it and select Attached to: Host Interface. At the Host Interface Settings section go to Interface Name and type in the interface's name, that is, tap0. Click Ok and boot your Windows XP.

When Windows is started, go to Start → Settings → Network Connections. Find the connection you previously added and open its Properties window. Highlight Internet Protocol (TCP/IP) and click Properties. Check the Use the following IP address option. In IP address enter 10.0.1.2. In Subnet mask enter 255.255.255.0. Leave Default gateway empty. It should look like this:

snap of windows xp internet protocol properties



Click OK and then click OK once more. That's it. Now you should be able to access each from the other. To test if your private networking really works, try to ping each host from the other.

From Host (Ubuntu) use the IP 10.0.1.2 to ping the Guest (Windows XP):

giannis@giannis-laptop:~$ ping 10.0.1.2
PING 10.0.1.2 (10.0.1.2) 56(84) bytes of data.
64 bytes from 10.0.1.2: icmp_seq=1 ttl=128 time=0.455 ms
64 bytes from 10.0.1.2: icmp_seq=2 ttl=128 time=0.951 ms
64 bytes from 10.0.1.2: icmp_seq=3 ttl=128 time=0.368 ms

--- 10.0.1.2 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2001ms
rtt min/avg/max/mdev = 0.368/0.591/0.951/0.257 ms


Note that you will have to disable firewall for that network interface in Windows XP in order for ping to work.

From Guest (Windows XP) use the IP 10.0.1.1 to ping the Host (Ubuntu):

C:Documents and SettingsGiannis>ping 10.0.1.1

Pinging 10.0.1.1 with 32 bytes of data:

Reply from 10.0.1.1: bytes=32 time=14ms TTL=64
Reply from 10.0.1.1: bytes=32 time=1ms TTL=64
Reply from 10.0.1.1: bytes=32 time<1ms TTL=64

Ping statistics for 10.0.1.1:
Packets: Sent = 3, Received = 3, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 0ms, Maximum = 14ms, Average = 5ms
Control-C
^C


Although all settings applied to the network interface in Windows will be available every time you boot Windows, the virtual network interface will go away next time you shut down your Linux system. One easy solution to this, is to add the commands that set up your network interface in your /etc/rc.local script:

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

echo "Setting up tap0 interface..."
tunctl -t tap0 -u giannis
ifconfig tap0 10.0.1.1


exit 0


Now the tap0 interface will be available every time you boot your Ubuntu system. Note that you do not have to use sudo inside /etc/rc.local as this script is executed by root anyway.



Update on: December 13th, 2008

Here is also a tip from Ale Feltes Quenhan: you can also have NAT working by running the following two commands:

# iptables -t nat -A POSTROUTING -o eth1 -j MASQUERADE
# echo "1" > /proc/sys/net/ipv4/ip_forward


making sure to replace eth1 with the interface that gives your host machine an Internet connection.

Thanks to Ale.

21 Responses to “host-only networking for VirtualBox”

  1. Eko Wahyudiharto Says:
    Nice shot! Great articles. Keep up the works. Greetings from Indonesia
  2. [VirtualBox] Host-only 的設定方法 « My Knowledge Base Says:
    [...] Host-only 的設定方法 參考 host-only networking for VirtualBox [...]
  3. genome Says:
    Very good article!!!
  4. zephix Says:
    OMG thanks! You saved me a ton of time!
  5. krishna Says:
    that was crystal clear.thanks for sharing...
  6. Sean Says:
    When I enter the printer address it loads a window "configure internet port" and asks for a username. None of the options work from there. What's wrong?
  7. Manuel G. Fraga Castro Says:
    Very good article; thank you very much, I've saved a lot of time
  8. links for 2008-11-28 « My place Says:
    [...] » host-only networking for VirtualBox VMware came with a very useful networking feature, called host-only networking, that created a private network connection between the host and guest operating systems. This is feasible with VirtualBox, too, altough not quite as obvious. Here is described how you can do it with Ubuntu host and Windows XP guest system. (tags: ubuntu networking virtualbox host-only host) [...]
  9. afraxus Says:
    Great article. I'm about to install Vmware, but now i got the solution with VBox. Thanks a lot
  10. Aramis. Says:
    Thank you, a lot.

    It was very helpful, and I also loved your site.

    Cheers,

    Aramis.
  11. Elmar Says:
    Thanks for the very good article.

    Just one comment: in Win XP SP3 you need to change firewall settings for the connectivity test (ping) to work. So go to firewall settings by clicking around in the network connections dialog, then choose ICMP settings and activate the first check box. After doing the ping test you can disable this setting again, if you're concerned about ping compromising your security.
  12. Vinicius Says:
    Excellent post. Thank you so much!
  13. sowmithry Says:
    Hi Ale
    many thanks for posting this article
    I am facing problem with I am able to ping i.e., from my guest(xp) cmd> ping 10.0.1.1
    but not able to ping from host(ubuntu) sowmithry>ping 10.0.1.2
    could you please tell me how to solve this issue

    Thanks,
    Sowmithry
  14. julio1 Says:
    Hi, how's it done the other way around ie between ubuntu on xp host
  15. sowmithry Says:
    My Host OS:Ubuntu and Guest OS:XP
    after doing all the above steps, I am able to ping from Guest Xp i.e.,
    cmd>ping 10.0.1.1
    (this Ip is tap0 on Host ubuntu) its p working.

    But in Host Iam unable to ping the GuestXP IP address i.e.,
    $ping 10.0.1.2
    this is not working/pinging

    Please help me out
  16. riekje Says:
    Thanks for the manual.
    Pinging worked, but when I try to print from guest XP, it tells me the paper needs refilling. (that's not true) Otherwise the printer doesn't react, however the dialog works.

    If you have any additional help...thanks!
  17. kyianb Says:
    When trying to write to the rc.local file, I get access denied when trying to save changes. I've checked my groups and I have root access on my account.
  18. S. Christian Collins Says:
    Hi, I'm using Ubuntu Karmic, and I've noticed that "Host Interface" is not an option in VirtualBox 3.0. The closest matches are "Host-only Adapter", which doesn't allow selection of a name other than "vboxnet0", or "Internal Network", which allows a custom name, but doesn't seem to work. Neither option works when pinging in either direction with the firewall disabled.

    Has anybody been able to successfully set up shared folders using SMB with VirtualBox 3.0 and Ubuntu Karmic?
  19. east Says:
    Hello!

    @S. Christian Collins:
    You might want to use:
    Attached to: Bridged Adapter
    Name: tap0
    Be aware of iptables/firewall at host :) !

    Nice HowTo btw!
  20. Phil Says:
    Thanks, got this working perfectly. I could not quite follow your instructions for maintaining the network after reboot so I didnt do it. Sure enough' Virtualbox gave me an error which means I cannot now open it,sorry I'm just starting with linux and need a bit of handholding Have you any suggestions PLEASE?
    HostInterface Networking -tap0
    VERR_INTNET_FLT_IF_NOT_FOUND
  21. Brian Says:
    The bridged adapter setting worked. Thanks!

Leave a Reply