Tech Quickie — Connecting To A Virtual Machine From The Host Using SSH
For a GUI-less server, the shared clipboard functionality of VirtualBox Guest Additions does not work, as a text-based server does not have a clipboard. Therefore, if you want to use copy and paste, you’ll have to connect to it using some kind of SSH client.
18.104.22.168 is a special IP address known as the loopback address. When connecting to 22.214.171.124, you are in fact connecting to your own machine. The loopback address is typically used to test whether the network hardware (i.e. Network Interface Card) is functioning correctly. We’ll be using it to connect to a virtual machine running on our host.
When configured to NAT (Network Address Translation), VirtualBox will place the virtual machine inside of a private network (10.0.2.x).
In consequence, if we want to connect to a VM inside of the private network from the host’s network, we’ll have to set up port forwarding.
You can configure port forwarding by opening up the settings for the VM and then navigating to the network tab.
Because we are connecting to and from machines on the same host, they can’t both be using post 22 for SSH. Therefore, we set the port the host port to some arbitrary port (i.e. 2222).
It’s important to note that in order for this to work, your VM has to be able to accept incoming requests from SSH clients. If you haven’t already, install the proceeding package.
sudo apt-get install openssh-server
Finally, open up a command line on your host machine and connect to your VM using the same credentials you’d use to login.
ssh -p 2222 firstname.lastname@example.org