How to SSH into virtual box guest VM from host

I have set up a virtual box guest VM running ubuntu 18.0.4. I tried to SSH into the ubuntu guest VM from host. It did not work. Here are the simple steps to enable SSH to access ubuntu guest VM from host.

By default, when we create a virtual machine in virtual box, it will come up with a pre-configured NAT adapter.

virtualbox NAT

This will provide the internet connection to the guest VM. This is not enough for enabling SSH. Power off the virutal machine, open network settings in virtual box and add a host only adapter.

Host only adapter

Go to File –> Host Network Manager  and make sure that the selected network adapter for host only network is enabled with DHCP configuration.

DHCP1

DHCP2

The virtual machine that is configured with this Host only network adapter can receive static ip addresses in the range of lower bound and upper bound mentioned in this configuration. In this  case, it is between 192.168.57.3 and 192.168.57.254 (both bounds inclusive).

Switch on the virtual machine and log in.

We need to make changes to the network interfaces file of ubuntu, to reflect the host only adapter and the static ip for the VM.

Navigate to /etc/network/interfaces and add the following lines.

auto enp0s8
iface enp0s8 inet static
address 192.168.57.3
netmask 255.255.255.0

In mycase the host only network adapter is assigned as enp0s8 in the guest VM. In your case, it may be same or different. Based on that set it appropriately.

The next step is to install the openssh server. Run the following commands in the terminal.

auto eth1
iface eth1 inet static
address 192.168.57.4
netmask 255.255.255.0

Restart VM and we can the static IP address of 192.168.57.4 assigned to it.

SSH from host. It should be successful.

SSH guest VM

This completes the post of enabling SSH for ubuntu 18.04 guest VM running in virtual machine.