How to SSH into Ubuntu in Virtualbox (Part 1 – Setup) ๐Ÿ’ป

1. Overview

While the point of having a virtual machine is its separation and isolation from the host machine you also want to interact with the system. VirtualBox offers tools like shared clipboard and folders but many users are used to SSH for administration.

Here we show to ssh into a virtual machine running with VirtualBox. To be specific, we have installed Ubuntu 16.04

Our scenario:

  • Ubuntu 16.04 64 bit running on a virtual machine
  • Virtual Box 5.2.6
  • Host-Machine: Windows 10 + BitVise SSH Client 7.2

2. Set up ssh server on guest machine

The first step is to make sure, ssh server is running on our virtual system.
You can do this by checking if ssh daemon sshd listening on port 22

sudo netstat -tulpn | grep ssh

The result should look like this:

Our SSH server is up and listening on port 22 for ipv4 and ipv6.

If ssh is not installed and running you can do this with apt-get:

sudo apt-get install openssh-server

3. Enable SSH port forwarding in host machine

In VirtualBox we navigate in the Menu to Devices->Network->Network Settings … and from there we have the adapter settings (NAT) and click on advanced and then on port forwarding

We set up the host port as 2222 and the guest port as 22. In other words, when we save this setting VirtualBox will be listening on our host machine on port 2222 and will forward any incoming traffic to our guest machines ip and port 22.

4. Connect with host’s SSH client to virtual machine

So what is left is only to connect via a SSH client to our host machine’s ip address and port 2222. What about credentials? Since our ssh request will be forwarded we use the virtual machine’s ssh login credentials and authentication settings.

And there is our desired result:

5. Conclusion / Troubleshooting

This process was pretty straight forward but there are things that might go wrong. So we have a dedicated troubleshooting article set up if things are no working quite well.

Leave a Reply

Your email address will not be published. Required fields are marked *