VirtualBox: give Host-Only access to internet

This post describes a simple trick for VirtualBox to give host-only virtual machines access the internet or access your host from the guest using NAT.

Problem

The original problem or requirements were as follows:

  • The host should be able to browser the webserver on the Guest (the virtual machine)
  • The guest should be able to access the outside world (intranet/internet)
  • Use fixed IP address

Yes, you can use port-forwarding using NAT network interface for the virtual machine. But configuring this is just way to complicate and error prone. A few googles more, and I couldn’t find anything good.

Solution

The solution I came up with: configure 2 network interfaces for your Virtual Box host. Simple, isn’t it?

First, we need to make a new network in VirtualBox. You can do this in the GUI preference, which is a bit platform specific, but it’s not easy to miss.

Create a new network in VirtualBox, named like vboxnet2 with following settings (change to suit your needs):

  • IPv4 Address: 10.88.12.0
  • IPv4 Network Mask: 255.0.0.0
  • Leave DHCP disabled if you don’t need it. Fixed IP addresses are just more predictable

Setup your virtual machine using 2 network adapters. The order doesn’t really matter, but you’ll need one NAT and one Host-Only:

  1. NAT (not much to configure).
  2. Host-Only using the network you created earlier, for example, vboxnet2.

Boot your virtual machine, and configure the network interfaces in your guest OS (same order as previous point):

  1. First interface using NAT, which you can use with the internal DHCP
  2. Second interface using Host-Only will use a fixed IP address and netmask which you configured earlier for the vboxnet2 VirtualBox network. For example, 10.88.12.4.

Guest has access to outside, and host can access services running on guest. This is handy when you have a demo or develop using a service running in the virtual machine, and the virtual machine needs to access the outside world.

(If the above has been posted somewhere else: I was either blind, or it was not visible or indexed enough.)

Comments

Emin
No one commented here, but this is really good article. I too was not able to find solution and I did not like port forwarding either, so this is exactly what I needed. Thanks very much!
Lili
This is great, I was trying to figure this out and no one was able to explain as simple as you did. Thanks!
Reynold
Alex
Thanks, is what i want. :) good luck in your projects
Andi
it works :-) Thanks a lot.
Jurgen

Thanks for great solution. Although I’ve got one trouble on a guest system. When configuring two network adapters the guest system gets two default routes. In my case I’ve got default route to Host-only adapter going first and for NAT adapter going after. route -n showed something like this: default 10.88.12.0 - which is HOST-only default 10.0.2.2 - which is NAT

Guest system couldn’t ping Internet. After deleting first default route - everything wen well.

Rubens Mariuzzo
Great post ever!
Adrian Lee

I got my solution from this link: http://www.youtube.com/watch?v=zR_TwPaWw8k Easy.

Cheers

Bjoern

it’s quite horrible that I did not even think of this…

Thank you dude!!

Don

Thank you so much! It couldn’t have been any easier than this and exactly what I wanted! I did it with a Debian Guest and set the NAT as the default during installation. After installation I changed my /etc/network/interfaces file:

The loopback network interface

auto lo iface lo inet loopback

The primary network interface

auto eth0 iface eth0 inet dhcp

The seconday network interface

auto eth1 iface eth1 inet static address 10.88.12.4 netmast 255.0.0.0

It does complain when restarting the network that it can’t bring up the network, but it does work when you tried to access the Guest’s web server. You might have to do an additional ‘ifconfig eth1 up’ to get it going, but it usually works without that.

Joe
@Don Perhaps it is because you spelled ‘netmask’ incorrectly (‘netmast’)?
haythem
Thank guys :)
haythem
**haythem :** Thank you guys :)
Szymon

Hey.

Thanks for that!!! The idea is simple and working perfectly. Hit this wall yesterday and couldn’t figure it out how without setting up local router to provide Internet within Host-Only network. it has to be set up on every single host within your Host-Only network but still, saves a lot of time.

Cheers.

andriy
It’s so easy! Thank you!
Alex
I’ve been struggling all day to get this working. And i finally did. Thank you very much
noone
Would like to pretend that I thought of this myself. I should have. But the fact is that I didn’t, and got it from here! Thanks.
Ralf
Wow, exactly what I wanted to do. Thanks so much!
kjv007
Awesome… this saves me a lot of time :)
Adam Plocher
I just wanted to say THANK YOU. This saved me some trouble. I was pretty familiar with NAT and Bridged networks but I have a demo machine with some VMs running different server software. I was having a hard time getting the host machine to access the guest IPs and it has to be able to run offline (with the option of going online, too). This was perfect.
B0zmeister!

Yeah I had this problem it was a total pain! But I solved it by simply installing Squid Cache Proxy server on my physical PC, and that way - my host-only internet virtualbox PCs could connect to the internet !

I did a quick 3 minute guide here - for anyone who want’s to how it works ! http://b0zmeister.wordpress.com/allowing-host-only-virtualbox-guest-to-connect-to-the-internet/

Geri
Thanks!
Asad Hasan
wow Boss Man, you really made my day.
Thiago

I did set up vboxnet0 : IP 192.168.56.1 // mask /24

#interface 1º (NAT) iface inet eth0 dhcp

#interface 2º (hostonly) iface inet eth1 static IP: 192.168.56.101 mask /24

and I did restart it but my vm not has internet i tested with sudo apt-get install’

Avi
Exactly what I was looking for. Thank you.