VirtualBox

How to Use VirtualBox Host-Only Networking

VirtualBox is one of the popular virtualization platforms from Oracle Corporation. With VirtualBox, we can run multiple operating systems simultaneously. We do not need to mess up with our main Host OS. It has many powerful networking features to set up any desired configuration. There are six networking modes supported in VirtualBox, namely:

1. Not Attached (network card is present, but the cable is unplugged)

2. NAT (Network Address Translation)

3. NAT Service

4. Bridged Networking

5. Internal Networking

6. Host-only Networking

7. Generic Networking

What will we cover?

In this guide, we will learn how we can configure Host-only networking mode in a VirtualBox virtual machine. We will use two virtual machines: 1) Fedora 34 2) Ubuntu 20.04. After connecting them in a Host-only mode, we will check if both of them can ping each other and the Host machine. Let us first understand the concept and how to use the Host-only networking mode.

VirtualBox Host-only Networking Mode

Host-only networking performs the combined functions of bridged and internal networking modes. In bridge mode, a virtual machine can communicate with the host machine and other virtual machines because they share the same physical interface of the host machine. Similarly, in the case of internal networking mode, virtual machines can only talk to each other but cannot communicate with the host machine and any other machine outside of their set as they are not connected to any physical interface.

Note: It should be clear that all the functions that can be achieved with internal networking can also be performed through Bridge Networking. But in the latter case, there is a security risk of passing virtual machine traffic through the physical interface of the Host.

A Host-only networking mode creates a network between the Host machine and virtual machines using a software interface (NIC). This means that in this mode, a virtual machine can connect to the Host and other virtual machines. In the case of the Internal networking mode, the connectivity is limited to virtual machines on the same Host. Also, unlike the Internal networking mode, Host-only mode provides DHCP services to assign IP addresses to the connected virtual machines. For this mode, there is no need for the physical interface of the host machine. Instead, the software interface (named as vboxnet) will perform the same function.

In the case of virtual appliances which are already configured, the Host-only networking helps a great deal. These appliances contain several virtual machines running different applications like database servers, web servers, etc. We can connect the web server and database server using a Host-only adapter. In this way, both can communicate with each other, but cannot connect to the outside world. This is the desired setup for securing a database server from direct access from the outside world. But in the case of a web server, we need to access it from all over the world. So to get this done, we will activate the second adapter in the VirtualBox networking option and connect it to the Bridge adapter.

Enabling Host-only Networking Mode

To enable Host-only networking mode in VirtualBox, follow the steps below:

Step 1. We need to create a Host-only network adapter. For this, go to the “File” option in the VirtualBox menu bar and select the “Host Network Manager”.

Step 2. In the new pop-up window, select the green icon corresponding to “Create Host-Only Network”. A new adapter, “vboxnet0”, will be created. The IP range of this adapter can be set from manual mode to automatic mode using the “Properties” menu.

Note the IPv4 address and mask for the adapter: 192.168.56.1/24. We will need it later when configuring IP addresses on virtual machines.

Step 3. Once the virtual adapter is created, we can use it with the Host-only networking mode. Now select the virtual machine from the list on the left pane. Right-click the name of the virtual machine and select the option “Settings” or select the “Settings” icon from the right pane.

Step 4. In the new pop up window, select the label corresponding to “Network”.

Step 5. On the right pane, under Adapter 1 tab:

1. Check to mark the “Enable Network Adapter” option.

2. Under the “Attached to” label, select “Host-only Adapter” option from the drop down menu.

3. From the drop down menu with the label “Name”, select the name of the virtual adapter (vboxnet0 in our case). Save settings and exit.

Step 6. Now launch your virtual machines and check the IP of the interfaces for both. You can use the “ip a” command for this. Let us configure each machine:

a) Run “ip a” command:

As you can see, there is no IP address for the enp0s3 interface on both VMs.

b) Now, we will set the IP address on both virtual machines. The steps will be the same for both Ubuntu and Fedora. The IP addresses should be in the range of the “vboxnet0” network.

1) Ubuntu VM

Run the following command on each machine.

a) Check the name of the connection

nmcli con show

b) Add IP addresses

sudo nmcli con mod Wired\ connection\ 1 ipv4.addresses 192.168.56.10/24 ipv4.gateway 192.168.56.1 ipv4.method manual

c) Restart the connection

sudo nmcli con down Wired\ connection\ 1

sudo nmcli con up Wired\ connection\ 1

2) Fedora VM

a) Check the name of the connection

nmcli con show

b) Add IP addresses

sudo nmcli con mod Wired\ connection\ 1 ipv4.addresses 192.168.56.11/24 ipv4.gateway 192.168.56.1 ipv4.method manual

c) Restart the connection

sudo nmcli con down Wired\ connection\ 1

sudo nmcli con up Wired\ connection\ 1

Testing Network Connectivity

Now that we have configured the host-only networking, let us check if pinging is working between the VMs and host. We will ping each machine from the other:

1. Pinging from Ubuntu to Fedora and Host machine.

2. Pinging from Fedora to Ubuntu and Host machine.

3. Pinging from the Host machine to Fedora to Ubuntu.

Conclusion

We successfully configured Host-only networking between two virtual machines (VMs) in the VirtualBox. A good understanding of various features of the VirtualBox can help to create various configurations and scenarios for testing prior to deployment.

About the author

Ali Imran Nagori

Ali imran is a technical writer and Linux enthusiast who loves to write about Linux system administration and related technologies. You can connect with him on LinkedIn
.