Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

In setting up virtual machines with VirtualBox, I often want the following characteristics

  • vm has a static ip
  • host can access vm without port forwarding
  • vm can access the internet
  • I can move my laptop from network to network (e.g. from home to office to coffeeshop) without worrying about securing or reconfiguring the vm

None of the VirtualBox network connection methods staisfies these requirements on their own.

  • NAT
    Requires port forwarding if you want to connect to the vm from the host.

  • Host-only
    The vm can not access the internet, unless the host is a router.

  • Bridged
    Exposes the vm to the network; not portable.

share|improve this question
up vote 49 down vote accepted

I can get the setup I want by setting up two adapters on the vm.

VirtualBox 4.2.12
Ubuntu 12.04 guest

In VirtualBox > Preferences > Network, set up a host-only network.

Mine is called vboxnet0, it is manually configured:
no dhcp

VirtualBox network configuration VirtualBox network configuration

Then, in the network settings for the virtual machine, set up two adapters:

Adapter 1
host only, vboxnet0


Boot the virtual machine and log in through the console VirtualBox provides.

Run this to see your adapters:

ls /sys/class/net

In my case the adapters were named eth1 and eth2 (and lo, the loopback interface).

Then, edit your network configuration.

sudoedit /etc/network/interfaces

# The loopback network interface
auto lo
iface lo inet loopback

# Host-only interface
auto eth1
iface eth1 inet static

# NAT interface
auto eth2
iface eth2 inet dhcp

Note that eth1 has no default gateway specified. eth2 will get a default gateway from dhcp.

share|improve this answer
Now to reach the guest from your host, did you have to add a route and a name in /etc/hosts on the host machine? Your host doesn't have an interface on the net, does it? – Mojo Jan 10 '14 at 18:29
No! VirtualBox creates an interface on the host for that network, for you. (Never mind.) – Mojo Jan 10 '14 at 19:20
This helped me a lot, I use Virtualbox for development, and when I'm in my house, everything works correctly since I have the network setup to my likings, the nightmare starts when I move to another network (family, companies...), I have to change websites URLs, delete some systems files, reboot I don't know how many times and the list goes on. This solution doesn't care about your current network which is why I like it...Just plug your PC to any network and focus on your work. – Nabil Kadimi Jun 14 '14 at 11:22
Thanks, huge help! – JSON Dec 11 '14 at 6:55
Thanks. Not defining a gateway address for the host-only interface solved my issues. – Florian Jul 21 at 10:20

I could solve my problem with a mix of Christian Long solution. I added 2 adapters:

Adapter 1 - NAT

Adapter 2 - host only, vboxnet0

The only diference was in VM's interfaces file:

sudoedit /etc/network/interfaces

# The loopback network interface
auto lo
iface lo inet loopback
auto eth0
iface eth0 inet dhcp
# Host only
auto eth1
iface eth1 inet dhcp

In VirtualBox Network config I left DHCP checked.

After a VM reboot everything worked fine.

share|improve this answer
This works for me, but I want the VMs to have static IP addresses. The minute I do that, Internet stops working. If I set up both with DHCP, it works fine. How do I make the host-only interface get a static IP? – Umar Farooq Khawaja Apr 7 '14 at 23:27

There is another simple way that we don't need to create a new NAT adapter

  1. On the host machine, please add the following iptables rules. This will forward packets through the host and on to the Internet:

    sudo iptables -A FORWARD -o eth0 -i vboxnet0 -s -m conntrack --ctstate NEW -j ACCEPT 
    sudo iptables -A FORWARD -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
    sudo iptables -A POSTROUTING -t nat -j MASQUERADE
  2. You will also need to enable IP forwarding on the host by issuing the following command:

    sudo sysctl -w net.ipv4.ip_forward=1
share|improve this answer

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 !

share|improve this answer
That worked great for me, thanks for sharing. It's by far the simplest way to get it all working. – this.lau_ Dec 28 '13 at 11:22
Hi Laurent - no problems - glad it helped! Gotta love running the virtualbox PCs off a different network - but - with internet connection working :) – B0zmeister Jan 4 '14 at 18:32

I Just added 2 adapters:

Adapter 1 host only, vboxnet0

Adapter2 NAT

And it works perfect, I can access the virtual machine from the host, and I have internet on the vm.

share|improve this answer

protected by Community Mar 15 at 11:23

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.