It is easy if we can access our Virtual Machines using ssh that was created on VirtualBox from your host machine. But it is not possible with the default VirtualBox settings.
You can find the VM IP address and try to
telnet to ssh port from your host machine.
$ ping 10.0.2.15
The ping command will give you 100% packet loss as below.
telnet command you can check the connectivity with the ssh port.
We can change some settings in the Virtualbox to overcome this issue and connect to the VM using ssh. Let’s see what are changes need to be done.
Step by step guide
- Make sure your VM on shutdown state.
- Open VirtualBox and select File > Host Network Manager from top menu.
- Click on create host-only network (Ctrl + Shift + C) button. It will add an adapter to the list.
- Double-click on the adapter you just created to configure the adapter manually. Add Adapter IP address and DHCP details.
- Click on Apply to save changes and close the window.
- Now open Virtual machine settings.
- Navigate to the Network section.
- Keep Adapter 1 as NAT
- Go to the Adapter 2 tab and Enable the network adapter by ticking the checkbox.
- Select the Host-only Adapter from attached to drop-down menu.
- Select the name of Host-only network name you created in 3rd step.
- Click on OK to save changes.
Now it’s time to check your changes done the magic. Start the VM and let’s try to find the IP address assigned to the VM.
ip command to see all the network interface details associated with the VM.
$ ip a
A new IP address has been assigned to the VM according to the DHCP we have configured in the host-only adapter. You can ensure the connection by using
$ ping 192.168.5.3
Let’s try to access the virtual machine from the host using SSH.
$ ssh [email protected]
Finally, it works.
sometimes you may be experienced the following error.
ssh: connect to host 192.168.5.3 port 22: Connection refused
This is most probably due to the absence of the openssh-server. you can simply install this package in the VM by executing the following command.
$ sudo apt-get install openssh-server