DEV Community

Cover image for How To: Ssh Into Ubuntu VM Virtualbox From Host Machine
Yassine Sellami
Yassine Sellami

Posted on • Updated on

How To: Ssh Into Ubuntu VM Virtualbox From Host Machine

Hi guys, In this article we will see how to connect to VM virtualBox from host systeme

What is SSH?

SSH, Secure Shel also known as Secure Socket Shell, is a network protocol that gives system administrators, a secure way to access a computer over an unsecured network.

How to install openSSH server on Ubuntu

sshd : is the OpenSSH server process. It listens to incoming connections using the SSH protocol and acts as the server for the protocol. It handles user authentication, encryption, terminal connections, file transfers, and tunnelling.
Let's start to install openssh-server.
First update the system

$ sudo apt update
$ sudo apt upgrade
Enter fullscreen mode Exit fullscreen mode

To install openssh-server package, run:

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

Once installed, the SSH service should be started automatically. If necessary, you can start (or stop, restart) the service manually via command:

$ sudo service ssh start
Enter fullscreen mode Exit fullscreen mode

Verify that ssh service running

$ sudo systemctl status ssh
Enter fullscreen mode Exit fullscreen mode

Configure firewall and open port 22

Before enabling the UFW firewall we need to add a rule which will allow incoming SSH connections. If you’re connecting to your server from a remote location, which is almost always the case and you enable the UFW firewall before explicitly allow incoming SSH connections you will no longer be able to connect to your Ubuntu server.

To configure your UFW firewall to allow incoming SSH connections, type the following command:

$ sudo ufw allow ssh
Enter fullscreen mode Exit fullscreen mode

Now we can enable UFW firewall by typing:

$ sudo ufw enable
Enter fullscreen mode Exit fullscreen mode

You can check the status of UFW with the following command:

$ sudo ufw status
Enter fullscreen mode Exit fullscreen mode

SSH to a VM VirtualBox

The best way to login to a guest Linux VirtualBox VM is port forwarding. By default, you should have one interface already which is using NAT.
Then go to the Network settings and click advanced, Click on the Port Forwarding button.

Add a new Rule. As value :
rule name = "ssh", "Protocol' = "TCP", "Host port" = 2222, "Guest port" = 22.
VirtualBox will create a private network (10.0.2.x) which will be connected to your host network using NAT, To get Guest IP, type the following command in you VM ubuntu :

$ sudo ifconfig [interface_name] (for me:  ifconfig enp0s3)
if use latest version of ubuntu, it use #Netplan, You can try this command
$ sudo ip a
Enter fullscreen mode Exit fullscreen mode

Everything else of the rule can be left blank.

This way, we can point putty or MobaXterm to Port 22 of and VirtualBox will redirect this connection to our VM where its ssh daemon will answer it, allowing us to log in.
In my case I use MobaXtrem, To SSH into the guest VM, write:

$ ssh -p 2222 [user]@
Enter fullscreen mode Exit fullscreen mode

*Where user is your username within the VM.

Close GUI session in virtualbox without shutting down the VM

There is an extra dropdown menu next to the Start button with an even better option:
Detachable Start! It is a hybrid start option where they run the VM process in the background, and simply display a headless VM client that you can easily close.

Then you can close VirtualBox GUI, by choosing "Continue ruining in the background":

Enjoy the fact that you can now SSH into your VM!

Top comments (10)

samerickson profile image
Sam Erickson • Edited

Excellent tutorial! Well written, to the point, and easy to understand. There is an abundance of blog posts written on the subject that treat you like a complete beginner (too much reading for something so simple) or use a complicated bridged network setup, which is a solution, but not what I was looking for. Thank you for this post.

selllami profile image
Yassine Sellami

Thank You

bijeshmohan profile image
Bijesh Mohan

Thanks a lot!!!

linuxguist profile image
Nathan S.R.

Very Helpful Post. Thanks.

The OpenSSH server reads a configuration file when it is started. Usually, this file is /etc/ssh/sshd_config

In this file, we need to change the following settings, for password based login to work :

PasswordAuthentication yes
ChallengeResponseAuthentication yes

Hope this helps. Additionally, you can easily configure a host name resolution, for the LAN network, instead of knowing ip address each time, by following the guide at :

bharatv profile image

How can I use internal docker containers via IPs like of it in the guest machine
Example - I am making an application which requires mysql/mongodb which I've installed as a container of guest machine which is Ubuntu 20.04
I want to add server properties in the app running on windows ... what ip can be used?

selllami profile image
Yassine Sellami • Edited

You can't add PF for docker container IPs, Because each time u get a new IP for ur container.
--> 1 windows hosting VBox.
--> 2 Ubuntu 20.04 in VBox.
--> 3 Mysql in docker (port 3306).

# ex: inside (2) make sure mysql run on 5200
$ docker run --name some-mysql -p 5200:3306 -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:5.7.40
# MySql now is reachable from (2) localhost:5200
# Check ur Firewall inside (2)
$ ufw allow 5200
$ ufw status
# Get ur VMs name
$ VBoxManage list vms 

# Add new port forwarding, with empty guest ip
$ VBoxManage modifyvm "MyVmName" --natpf1 "tcp-pf5200,tcp,,5200,,5200"
Enter fullscreen mode Exit fullscreen mode
ndrean profile image
NDREAN • Edited

Thank you very much, you gave the clue I was looking for: "Where user is your username within the VM"
ssh -p 2222 [VM-username]@

unknown850 profile image
Mr Unknown

Example, your guest machine(the VM) name is "User", so the command going to be
ssh -p 2222 user@

patarapolw profile image
Pacharapol Withayasakpunt • Edited

Just what I am looking for. Coupled with How To Use Visual Studio Code for Remote Development via the Remote-SSH Plugin, it is possible to hack into Ubuntu via VSCode, without needing head at all.

I am using Installation/MinimalCD as well.

selllami profile image
Yassine Sellami

Yes, it's possible. you just have to configure your Remote-SSH Plugin for VSCode and follow this guide as well.

Host my_remote_server
    HostName your_server_ip_or_hostname
    User sammy
    IdentityFile /location/of/your/private/key