VirtualBox with NAT and Host-Only network configuration

da lin
3 min readOct 9, 2020

--

TL;TR; It has been a long period that I keep using the Bridged network mode to for linux vm to be accessed from the host machine. Because I’ve never figured out how to make the NAT and Host-Only work properly. After started the preparation of RHCSA, a deeper understanding of how networking works in Centos8 is imperative.

Bridged vs NAT and Host-Only

From this link, search with the key word: Table 6.1. I found all networking modes and their access are well explained.

Bridged: This mode allows access between VM to VM, VM to Host and Host to VM, VM to Net and Net to VM

Host-only: This mode allows access between VM to VM, VM to Host and Host to VM

NAT: This mode allows access between VM to Host and Host to VM (by port forward), VM to Net

What I need

Even though bridged mode covers all needs but it has connections that I want to block: Net to VM. Beside that I would like to keep VM to Net to make the linux able to use internet. And Host to VM is needed for access to vm from ssh.

From the modes discussed previously, Bridged mode cannot be used because it allows access Net to VM.

NAT mode can concur the need but need Port forward setting. Which should work well but I prefer to testing the combination with Host-only mode.

Combination the usage of NAT and Host-only

Open VirtualBox -> File -> Host Network Manager …

Here, I removed all existing adapter and created a new one, I just leaved the ip address given by virualbox, and leave DHCP server disabled

Then go to the configuration of vm, then set Adapter 1 to NAT, Adapter 2 to Host-Only with the adapter just created

Configuration of network in Centos8

The nmcli (NetworkManager Command Line Interface) command-line utility is used for controlling NetworkManager and reporting network status.

Check the ip address, with the previous adapters, there should be no connection activated by default.

Here enp0s3 is the device name for Adapter 1 and enp0s8 is for Adapter 2.

Use command nmcli connection show to display the default connections, if there is any, they can be deleted by using the command nmcli connection del <con-name>.

Now, we start add new connections for devices enp0s3 & enp0s8

Enable connection from VM to Net

nmcli connection add con-name nat type ethernet ifname enp0s3 ipv4.method auto

Enable connection from Host to VM with static ip defined

nmcli connection add con-name hostonly type ethernet ifname enp0s8 ip4 192.168.56.3/24 gw4 192.168.56.1 ipv4.method manual

With nmcli connection show command, two new connections net & hostonly can be displayed in green color (means the connection has been established)

ping www.google.com should work by connection nat

ssh <username>@192.168.56.3 should work by connection hostonly

Instead of using the static ip address, a new dns entry defined in /etc/hosts like could help with memory, like: 192.168.56.3 centos-dev

Then the use of ssh <username>@centos-dev works as well.

--

--