Remotely Accessing a Virtual Machine: Port Forwarding
ChigozieCO
Posted on January 25, 2024
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.
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.
š 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.
š 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.
š 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).
ā 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
š You can also start your vm via the GUI that you're familiar with.
š Once the VM starts up, head over to your terminal.
š Type in
ssh <your user>@127.0.0.1 -p <the host port you configured, (8081 in my case)>
ā ļø
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:
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
š You can search for the package before installing it with the command:
sudo apt search openssh-client -y
The image below shows the one we want to make use of.
š Next we install it using the command:
sudo apt install openssh-client -y
š In the same way we install the openssh-server using the command
sudo apt install openssh-server -y
š When your installations are complete, confirm that the SSH service is running with the command
systemctl status ssh
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>@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.
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
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.
Posted on January 25, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.