dev-guides

Setting Up a Host-Only Network on VirtualBox

So you’ve set up your virtual machine, but you would like to communicate with it over the network as if it were another machine, with its own IP address. You can do so by setting up a host-only network!

You may want to do so for a number of reasons:

Creating a Host-Only Network

If this is your first time setting up a host-only network, you’ll need to complete this step. Afterwards, you can just add more VMs to the same virtual network without needing to redo this.

First, open up your VirtualBox Manager GUI. Click the “Tools” button, then from the dropdown menu select “Network”.

In the Host Network Manager menu, you should see that a virtual, host-only network has been created for you, named “vboxnet0”. If not, click the “Create” button on the top left corner.

Make sure the “DHCP Server” box has been checked as “Enabled”.

Select “Properties” to check your Adapter and DHCP settings. By default, your adapter settings should have “Configure Adapter Manually” enabled, the IPv4 Address field should be 192.168.56.1, and the IPv4 Network Mask should be 255.255.255.0. (If you have a different IP address it is likely because the 192.168.56.1/24 range is already taken by another network interface on your host machine, this is okay).

Note the IPv4 address on the Adapter menu. This is the IP of your host machine on your virtual network. In particular, note the first 3 fields of the IP address: this is the subnet mask, and will help you determine your VM’s IP address later on.

Adding Your VM to the Host-Only Network

If you’re not using the reference VM, you’ll need to configure it to be placed on our host-only network. You’ll need to do this for every new VM you want added to your host-only network.

Open up your VirtualBox Manager again. Make sure your VM is off. Select your VM, go to “Settings”, and go to the “Network” tab.

We’re going to create a new network adapter. Go to the next available adapter (if you haven’t set up any other adapters, this should be “Adapter 2”), and select “Enable Network Adapter”. In the “Attached To:” dropdown menu, select “Host-only Adapter”, and make sure that “vboxnet0” is selected.

Hit “OK” to save your settings. If your VM is currently running, you should reboot it to get a new host-only IP from the DHCP server.

Finding Your VM’s IP Address

Now that your VM is on the virtual network, we need to find out its IP address. This may vary, depending on how many VMs you have on your network, and your DHCP settings.

On your VM, run the following command:

[guest]$ ip addr | grep 'inet[^6]'

That will list a series of IP addresses, one of which should start with the same 3 digits as your host-only network’s IP range, by default 192.

This is your VMs IP address. For example:

inet 192.168.56.3/24 brd 192.168.56.255 scope global enp0s8

This indicates that the VM has an IP address of 192.168.56.3. The last value, in this example enp0s8 is the network interface for this IP address.

Now you’ll be able to use that IP address from your host machine, or any other VM on the same virtual network, for things like SSH access and server load-testing.

Troubleshooting Common Issues

My VM does not have an IP address for the host-only network interface

Depending on the operating system of the VM, the new network interface may not be automatically enabled. To fix this, we should first find the name of the host-only network interface in your VM using

[guest]$ ip addr

This lists all the interfaces and corresponding IP addresses. Find the interface that does not have an IP address and note its name (by default this should be enp0s8).

Now edit the network configuration file /etc/network/interfaces and add the host-only interface to enable it. Open the file as sudo, and append the following lines to the end (change enp0s8 if your interface name is different):

allow-hotplug enp0s8
iface enp0s8 inet dhcp

Reboot your VM and try finding your IP address again.

My VM has an IP address for the host-only network, but I cannot connect to it

If your IP address starts with 169, this indicates your VM was unable to get an IP from the DHCP server and is trying to assign one to itself. This can usually be resolved by rebooting your VM, and if that fails try also rebooting your host machine.

SSHing into your VM via the Host-Only Network

If you have sshd setup on your VM, you should be able to SSH into your VM at that IP address. For example, with the above IP address, if your username is debbie, you would SSH into my VM with the following command:

[host]$ ssh debbie@192.168.56.3

If you’ve followed the SSH guide, you’ll notice that we no longer need to pass a -p flag; this is because instead of forwarding the port at the localhost loopback interface (with IP 127.0.0.1), we’re just treating the machine like it’s any other machine on a network, and using the default port 22 to SSH in.

The corresponding ~/.ssh/config file for this would be:

Host exeggutor
    Hostname 192.168.56.3
    User debbie
    ForwardAgent yes # optional

Of course, replace your Hostname with your VM’s IP and User with your username. For more details about this setup procedure, including SSH key generation and management, see the VirtualBox SSH guide.