104

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 coffee shop) without worrying about securing or reconfiguring the VM

None of the VirtualBox network connection methods satisfies 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.

7 Answers7

117

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.

  • 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 192.168.56.0 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
  • 1
    Update: VirtualBox 4.3 introduced a NAT service that looks like it might obviate the need for two interfaces. 6.4. Network Address Translation Service – Christian Long Jan 26 '14 at 17:36
  • 4
    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
  • For me, I don't need network & broadcast. With it, it doesn't work I don't know why. – vee Nov 07 '15 at 14:09
  • 1
    Thanks. Not defining a gateway address for the host-only interface solved my issues. – Florian Jul 21 '16 at 10:20
  • 2
    Perfect. /etc/network/interfaces config was key THANKS!!!! – Byron Whitlock Nov 01 '16 at 20:35
  • I don't have a folder /etc/network – Joost Döbken Feb 07 '17 at 09:58
  • 1
    "Note that eth1 has no default gateway specified. eth2 will get a default gateway from dhcp." <-- THIS COMMENT IS THE KEY. – Pere Pages Apr 11 '17 at 11:18
  • Thanks it worked, but I was wondering why does virtualbox only work with this IP range "192.168.56.x" and If I try to set it to "192.168.9.9" it gets the static ip but apache stops responding to requests from the host. while the former ip works just fine. It got me pulling my hairs for so long. I just gave up. :( – Mohd Abdul Mujib Jan 18 '18 at 19:06
  • what IP would the host be given in this situation? (for updating guest firewall rules) – dangelsaurus Mar 18 '18 at 01:39
  • WARNING: The location in the VirtualBox utility to change the host-only network information has changed a bit in 5.2.12 (probably 5.2.x) – mdpc Jun 11 '18 at 20:00
19

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
Baa
  • 3,452
  • 1
    Thanks! I added an update section to my old answer and linked to this new info. – Christian Long Mar 09 '18 at 22:57
  • 1
    I have followed you solution, but miss nat support for enp0s8. I have manually added enable dhcp4 for enp0s8 in netplan and finally get both enp0s3 and enp0s8 up. Hope this will help somebody. – Dzmitry Prakapenka Nov 13 '18 at 13:50
17

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.

Alan Camillo
  • 171
  • 1
  • 2
16

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 192.168.56.0/24 -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
    
Danatela
  • 13,243
  • 11
  • 45
  • 72
Long Bui
  • 161
  • 1
    This works only on a Linux host. – Derek Mahar Oct 18 '16 at 20:57
  • We need the "dnsmasq" service running. In the link https://unix.stackexchange.com/a/384187/61742 we have complete information about what was suggested by @Danatela . Thanks! – Eduardo Lucio Aug 05 '17 at 20:06
  • 1
    @EduardoLucio please give credits to Long Bui. I just edited this post to make it more readable. – Danatela Aug 07 '17 at 10:38
  • @Long Bui Thank you for your contribution! Up! Up! Up! Up! Up! Up! =D – Eduardo Lucio Aug 07 '17 at 13:36
  • @DerekMahar similarly, on Windows it's supposed to be ok to use the built-in Internet Connection Sharing (ICS), and maybe, even ip forwarding: https://serverfault.com/questions/929081/how-can-i-enable-packet-forwarding-on-windows. – YumeYao Aug 23 '22 at 13:48
4

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.

  • I tried this solution with Android x86 but it will use only one adapter, I thought the first only but it is random. It is using either NAT or Host-only adapter, not both, any idea why? Compared to bridged adapter, NAT has the advantage that it will use my VPN connection configured on my host OS. – baptx Jun 18 '21 at 16:01
2

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 for anyone who wants to know how it works.

Zanna
  • 70,465
B0zmeister
  • 29
  • 1
  • I used this solution also. Unfortunately it will only work for apps using HTTP protocol. For apps like WhatsApp on Android x86, we would need a SOCKS proxy like Dante but I can't get this solution working anymore, using redsocks: https://android.stackexchange.com/questions/221389/how-to-send-all-internet-traffic-to-a-socks5-proxy-server-in-local-network/221448#221448 – baptx Jun 18 '21 at 15:05
0

In my case, I could not see the IP 192.168.250.1 on vboxnet0 (e.g. with "ip addr"), despite setting this in the host-only network settings in VirtualBox, until I created the file /etc/netplan/02-vbox.yaml containing

network:
    version: 2
    renderer: networkd
    ethernets:
        vboxnet0:
            addresses:
                - 192.168.250.1/24
            dhcp4: no

and ran "netplan generate && netplan apply"