How To: Ssh Into Ubuntu VM Virtualbox From Host Machine
Tags: beginners, ubuntu, linux
Published: 2019-07-14 12:30:25

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. 

{% embed https://dev.to/ysellami/how-to-install-ubuntu-on-virtualbox-562 %}


### 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 
```javascript
$ sudo apt update
$ sudo apt upgrade
```
To install openssh-server package, run:
```javascript
$ sudo apt install openssh-server
```

Once installed, the SSH service should be started automatically. If necessary, you can start (or stop, restart) the service manually via command:
```javascript
$ sudo service ssh start
```
Verify that ssh service running
```javascript 
$ sudo systemctl status ssh
```
![](https://thepracticaldev.s3.amazonaws.com/i/c4ftfr693zcmlwj7bb5s.png)

### 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:
```javascript 
$ sudo ufw allow ssh
```
Now we can enable UFW firewall by typing:
```javascript 
$ sudo ufw enable
```
You can check the status of UFW with the following command:
```javascript 
$ sudo ufw status
```
## SSH to a VM VirtualBox

The best way to login to a guest Linux VirtualBox VM is [port forwarding](https://www.virtualbox.org/manual/ch06.html#natforward). 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.
![](https://thepracticaldev.s3.amazonaws.com/i/jejr1b3542qqkx9pw1f8.png)

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 :
```javascript
$ 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
```
Everything else of the rule can be left blank.
![](https://thepracticaldev.s3.amazonaws.com/i/tqreaxpi5w1igvnfn1yk.png)

This way, we can point [putty](https://www.putty.org/) or [MobaXterm](https://mobaxterm.mobatek.net/) to Port 22 of 127.0.0.1 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:
```javascript
$ ssh -p 2222 [user]@127.0.0.1
```
*Where user is your username within the VM.
![](https://thepracticaldev.s3.amazonaws.com/i/uzh50o7jr1ire4vxlveu.png)

## 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.
![](https://thepracticaldev.s3.amazonaws.com/i/8066e5pbgju7124vb4vi.png)

Then you can close VirtualBox GUI, by choosing "Continue ruining in the background":
![](https://thepracticaldev.s3.amazonaws.com/i/w5awy2m3b1avtd4cfbae.png)

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

{% embed https://dev.to/ysellami/how-to-run-mobaxterm-on-ubuntu-linux-with-wine-ohf %}

{% embed https://dev.to/ysellami/rhel-ubuntu-useful-command-433f %}