Remotely Accessing a Virtual Machine: Port Forwarding
Tags: virtualmachine, ssh, cloud, portforwarding
Published: 2024-01-25 14:09:18

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**

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

## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kl963bkfrbyu9pjlgjru.png)

🌟 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w2ckedhmf58d61st9ep1.png)

🌟 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dmiyd2vbqskr2zqeelpt.png)

🌟 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kj5n43qzc8nvw7i02xlz.png)

:exclamation: 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:

```sh

VBoxManage startvm <VM-name> --type headless
```
![cli vm start](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vx84aslz4qd65bn75h4x.png)

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

![GUI VM start](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vh4ss1kvxam1iecz9xpt.png)

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

🌟 Type in 

```sh
ssh <your user>@127.0.0.1 -p <the host port you configured, (8081 in my case)>
```

:warning:
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bgc8xtc5qhyor8myjqxj.png)

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.

```sh
sudo apt update -y
```

![update app repo](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7t29cf82tra1gwnfes6p.png)

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

```sh
sudo apt search openssh-client -y
```

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

![Install openssh-client](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1m2dhfhjdznd9ifo3iui.png)

🌟 Next we install it using the command:

```sh
sudo apt install openssh-client -y
```

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

```sh
sudo apt install openssh-server -y
```

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

```sh
systemctl status ssh
```

![install openssh-server](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k9uhrlwz6fmteo4673lh.png)

## 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 

```sh
ssh <your user>@127.0.0.1 -p <the host port you configured, 8081 in my case)>
``` 

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xrvtuu8myvkf4vs5lail.png)

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.

```sh
cat /etc/*release
```

![Linux version](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wxjya8e9q0qvhftdqh1r.png)

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.