DEV Community

Cover image for How to connect your local host to your Virtual Machine using Virtual Box
Ahmed Adel Fahmy
Ahmed Adel Fahmy

Posted on

How to connect your local host to your Virtual Machine using Virtual Box

Hi, in this article I am going to simplify how to connect your local host to the Virtual Machine your created using Oracle Virtual Box, using SSH.
it's not an everyday job that you want to connect between your local host and VM, however, sometimes you want to access it and doing stuff from your local host using the terminal, in this article I am going to give you a neat way to setup a SSH connection between the local host and the Virtual Machine.

First of all, I am using Ubuntu 20.04 LTS and I installed virtual box for some practicing on ubuntu - I don't want to miss with my guy actually - and after installing Ubuntu on the virtual machine, I faced a problem while connecting my local host to the VM as most of us do while using VMs over the cloud or connecting to remote servers using SSH.

Below, is the steps in a simplified way to be able to connect to your VM from your local host using SSH

1- open the Virtual Box and select your virtual machine and click on settings.
Image description

2- you will go to networks and click on it, then click on advanced, then click on Port Forwarding.
Image description

3- once you click on Port Forwarding, a small window will come up with two options on the right (adding a new port forwarding rule, removing a new port forwarding rule).
Image description
N.B: I blured my local host IP and the VM IP

4- here comes the tricky part and stick with me please

  1. under the name field, your will write ssh
  2. under the protocol field, you will leave it as TCP
  3. under the host IP, you will write your the IP of the local host (your laptop or PC)
  4. under the host port, type 3022 , I tried 2222 but it didn't work for me, so try both.
  5. under the guest IP, type the IP address of the Virtual machine, the IP is given automatically within the Operating system, so you just need to check the setting of the Operating system #NOT THE SETTINGS OF THE VIRTUAL MACHINE
  6. under the Guest port, type 22
  7. click ok and then ok. Image description

Bingo, That's the first part.
The second part is simply you will go inside the virtual machine, in the virtual environment and type these commands in your terminal to install ssh server :
sudo apt update
sudo apt install openssh-server
sudo service ssh start
Image description
to check that everything is working fine, type:
sudo systemctl status ssh
Image description

Congratulations, you are about to make it.
The Last step is to connect from your local host machine to the Virtual Machine using this command.
ssh <username on the Virtual machine>@<local host IP> -p 3022

and just type your credentials, and here you are.
Congratulations, you are now on your VM using your terminal only.
Thank you for your time 🙏️

Please leave your opinion in the comment section, also if there is anything that happened wrong during any step, please let me know, chao ❤️❤️❤️

you can follow me on twitter: https://twitter.com/ahmadzaabal
also you can find me on instagram: https://www.instagram.com/ahmed_a_fahmy/
last but not least,My Linkedin: https://www.linkedin.com/in/ahmedhedia/

Top comments (0)