In VirtualBox, how do I set up host-only virtual machines that can access the Internet?

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:
ip 192.168.56.1
netmask 255.255.255.0
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

Adapter2
NAT

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
        address         192.168.56.20
        netmask         255.255.255.0
        network         192.168.56.0
        broadcast       192.168.56.255

# NAT interface
auto eth2
iface eth2 inet dhcp

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


Update March 2018

See this answer from @Hugo14453 for an updated version that works with Ubuntu 17.10 and newer.


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


Network configuration has changed in Ubuntu 17.10.1. You now use the netplan config.

I followed this guide here

As a migration of Christian's answer, do the following:

Create a new config file inside of /etc/netplan to hold your host-only adapter config.

e.g sudo nano /etc/netplan/02-netcfg.yaml

Enter the following to configure a static IP of 192.168.56.12 where enp0s3 is the name of your host-only adapter.

network:
    version: 2
    renderer: networkd
    ethernets:
        enp0s3:
            addresses:
                - 192.168.56.12/24
            dhcp4: no

Then run the following two commands:

sudo netplan generate
sudo netplan apply

NAT should work without configuration, run ifconfig to see the result:

enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.56.12  netmask 255.255.255.0  broadcast 192.168.56.255
        inet6 fe80::a00:27ff:fe06:6cdd  prefixlen 64  scopeid 0x20<link>
        ether 08:00:27:06:6c:dd  txqueuelen 1000  (Ethernet)
        RX packets 252  bytes 23076 (23.0 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 208  bytes 30015 (30.0 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

enp0s8: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.0.3.15  netmask 255.255.255.0  broadcast 10.0.3.255
        inet6 fe80::a00:27ff:fe4d:a6b8  prefixlen 64  scopeid 0x20<link>
        ether 08:00:27:4d:a6:b8  txqueuelen 1000  (Ethernet)
        RX packets 95  bytes 94894 (94.8 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 85  bytes 7436 (7.4 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0