DEV Community

Cover image for Remotely Accessing a Virtual Machine: Port Forwarding

Posted on

Remotely Accessing a Virtual Machine: Port Forwarding

Virtualization technology give us the ability to construct virtual versions of servers, storage, networks, and other physical machines. Through virtualization, a single computer's hardware resources can be split up into several virtual machines (VMs).

A virtual machine functions as a software program executed on a computer, simulating the behavior of an independent computer. Essentially, it provides a means to establish a computer within another computer. When running in a window on the host computer, a virtual machine offers users an experience similar to that of using an entirely distinct computer.

Accessing your VM

Usually when you setup your VM you have the GUI (Graphic user interface) available to you to use in accessing the machine in other to conduct your business as you do your physical computer.

However this gets old, not only does it get old you might not always be able to work from the GUI for one reason or the other. Or you might just want to show off your badass technical skills by accessing and working remotely with your VM.

There are several ways to gain remote access to a VM and they all have one thing in common, they use SSH.

Make sure the SSH client & server are installed in the Linux OS

✨ You can use SSH with a key pair (private and public key) for authentication.
✨ You can install your VM via vagrant and access it via vagrant.
✨ You can also gain remote access via port forwarding.


Port forwarding, sometimes called port mapping, allows computers or services in private networks to connect over the internet with other public or private computers or services.

With port forwarding, when you access the particular port you will be directed and given access to the configured matching port in the VM that was defined in the port forwarding rule.

To be able to gain remote access to a VM via port forwarding you will need to first configure your Type2 hypervisor (whichever you use, either VirtualBox or VMWare) to accept the connection using a port forwarding rule. This is done by following the process below:

Set Port Forwarding Rule

I will be using Oracle VirtualBox hypervisor

🌟 Open VirtualBox.

🌟 Select the VM you would want to gain remote access to and click in settings on the top right side.

Open VirtualBox

🌟 Once the setting opens up, select the network setting from the options by the left hand side, scroll down to the bottom and click on advanced for drop down menu.

🌟 From the drop down menu, click on port forwarding.

Port Forwarding

🌟 A popup like that below will appear when you click on port forwarding, click on the green plus symbol shown below to add your port forwarding rule.

Add Port forwarding rule

🌟 Now configure your port forwarding rule, be careful not to make use of ports that are already in use. Give your rule a name and assign the host port and the guest OS (your VM) port.

🌟 As is shown in the image below, I named my rule SSH and configured port 8081 for my host OS and 22 (the default SSH port) for my guest OS (which is the VM).

Port forwarding rule

❗ To better tighten the security you can map the host port to the host IP address. We won't do that here.

🌟 Ensure you save your changes.

Time to Connect

🌟 Before you can connect to your VM from your host OS you have to ensure that your VM is started so start the VM.

🌟 I'm using a headless start because I do not need the VM GUI so a headless start will work just fine. If your antivirus alerts you, just allow access.

🌟 You can do this via your CLI or from VirtualBox GUI. To start up a virtual machine from the CLI ensure your Oracle VirtualBox has been added to your environment Path and run the command below:

VBoxManage startvm <VM-name> --type headless
Enter fullscreen mode Exit fullscreen mode

cli vm start

🌟 You can also start your vm via the GUI that you're familiar with.

GUI VM start

🌟 Once the VM starts up, head over to your terminal.

🌟 Type in

ssh <your user>@ -p <the host port you configured, (8081 in my case)>
Enter fullscreen mode Exit fullscreen mode

Ensure you already have the SSH client and server installed and running in your VM if not it won't be possible to remotely access the server and when you try to connect you will have the error shown below:

SSH error

If you don't have SSH installed follow the steps below to install it, if you already do ignore the next few steps and do continue along to connect.

Installing SSH Client & Server

🌟 Start your VM in a normal start.

🌟 Open the terminal and type the command below and enter your password when prompted for it.

sudo apt update -y
Enter fullscreen mode Exit fullscreen mode

update app repo

🌟 You can search for the package before installing it with the command:

sudo apt search openssh-client -y
Enter fullscreen mode Exit fullscreen mode

The image below shows the one we want to make use of.

Install openssh-client

🌟 Next we install it using the command:

sudo apt install openssh-client -y
Enter fullscreen mode Exit fullscreen mode

🌟 In the same way we install the openssh-server using the command

sudo apt install openssh-server -y
Enter fullscreen mode Exit fullscreen mode

🌟 When your installations are complete, confirm that the SSH service is running with the command

systemctl status ssh
Enter fullscreen mode Exit fullscreen mode

install openssh-server

Back to Connect to our VM Remotely

🌟 If you stopped your VM, start it back up. You can use a headless start now (as earlier shown) if you like.

🌟 Type in

ssh <your user>@ -p <the host port you configured, 8081 in my case)>
Enter fullscreen mode Exit fullscreen mode

When asked if you want to continue connecting enter yes then the password to the user you are using.

🌟 You should have successfully connected to the VM now if you followed all the steps correctly.

Successful SSH connection

Notice the name of the user before we remotely accessed our VM and the user at the bottom of the screen shot after connecting to the VM.

🌟 We can run commands on the VM now like we normally would when directly working on the machine.

🌟 You can run the command below to see the OS information.

cat /etc/*release
Enter fullscreen mode Exit fullscreen mode

Linux version

This whole process shows how to use a password based authentication, however note that password based authentication is highly discouraged.

When you SSH into a server you should use a key pair (private and public keys), in another post in this series I will show you how to use a key pair to SSH into a server and I will also show you how to install Vagrant and remotely access that VM via vagrant.

Top comments (0)