This short tip explains, how to connect the host system to the guest VM and allows Internet access for the guest at the same time, without using the bridged network mode. This setup makes sense, if you don’t want to expose the VM to the rest of the host’s local network, but still want the guest to have Internet access.
To get this done, we are using two virtual network adapters on the guest VM and one on the host. One of these virtual adapters will be a host-only adapter for bidirectional communication between host system and guest VM. This way, the host can access services running on the guest, like a web or database server. The IP address on the VM can be set manually or by using the integrated DHCP-server.
The second adapter will be of type NAT. This will allow the guest VM to use the network access of the host system, without exposing itself directly to the host’s local network. This way, the host can share its internet connection with the guest VM.
If you don’t mind that the guest VM is exposed to the local network, it’s easier to use a single adapter in bridged network mode. This way, the guest VM will be part of the host’s local network.
In this example, the virtual network adapters on the guest VM (Ubuntu, Linux) will look like this:
IP address for enp0s3: 192.168.100.2 (set manually) IP address for enp0s8: 10.0.3.15 (set by VirtualBox)
The first adapter will be used by the host to access the VM. The second adapter will be used by the VM to access the hosts internet connection.
On the host (a MacBook Pro running macOS), it basically looks like this:
IP address for en1: 192.168.104.102 (LAN, set by local DHCP-server) IP address for vboxnet0: 192.168.100.1 (set manually in VirtualBox)
The first adapter is the hosts currently used network adapter for accessing the local network and Internet via NAT “router”. The second adapter is the virtual adapter created in step 1.