SSH To Guest or Remote OS
Published: 2020-04-22 17:17:10

I often have to work with virtual machines and/or remote servers. When its more convenient to use local developer tools, I rely on SSH and SSHFS. This post describes how to setup SSH to connect to a Ubuntu VirtualBox VM. The information can also be applied to other remotes hosts. Connecting to a remote OS via SSH is the first step in extending the use of your local tools for remote development. A subsequent blog post describes how to configure SSHFS.

<div className="gap-30"></div>

### Table of Contents

<br>

<div>
<ul>
<li><a href="#setup-virtual-box">Setup Virtual Box</a></li><li><a href="#communicate-with-the-guest-os-ssh">Communicate with the guest OS (SSH)</a></li><li><a href="#login-with-ssh-keys">Login with SSH keys</a></li>
</ul>
</div>

<br>
### Setup Virtual Box

Download and install [VirtualBox](https://www.virtualbox.org/wiki/Downloads) to the host operating system (host OS). Create or import a new virtual machine with your preferred OS. For this post, Ubuntu OS (guest OS) will be running inside of Oracle VirtualBox.

![VirtualBox Manager Window with VMs](https://curiousstemlovingfellow-blog-data.s3.amazonaws.com/blog-posts/manager-window-1.png)

### Communicate with the guest OS (SSH)

SSH provides secure login to remote machines over a network, and [more](https://en.wikipedia.org/wiki/Secure_Shell#Uses). From the VirtualBox Manager start the Ubuntu VM (graphically) and open the terminal application in Ubuntu. 

**Install the OpenSSH daemon/server (sshd)** with the command `sudo apt-get install openssh-server`. Then run the following commands to - enable SSH, **start the SSH daemon**, and check it's status. 

> Check the Ubuntu OS version per the output of the `cat /etc/*release*` command.

<div className="gap-20"></div>

##### Ubuntu version 16.04 LTS or newer
1. `sudo systemctl enable ssh`
2. `sudo systemctl start ssh`
3. `sudo systemctl status ssh`

<div className="gap-10"></div>

##### Ubuntu versions 14.04.4 LTS
1. `sudo service ssh start`
2. `sudo service ssh status`

<div className="gap-20"></div>

Allow SSH through **the Ubuntu firewall (ufw)**, if active. Check firewall status with the command `sudo ufw status`. Allow SSH through the firewall (port 22) using the command `sudo ufw allow ssh`.

To find the guest OS on the network we can use its IP address or use port forwarding to send all traffic from host to guest via a designated port.

<div className="gap-20"></div>

##### Connect via port forwarding

Select *settings* from the VirtualBox manager then *network -> advanced -> port forwarding*.

![Settings window for port forwarding](https://curiousstemlovingfellow-blog-data.s3.amazonaws.com/blog-posts/manager-window-settings.png)

Add a new port forwarding rule that maps host OS port 2222 to guest OS port 22 such that;

* Protocol: TCP
* Host IP: 127.0.0.1
* Host Port: 2222
* Guest IP: *N/A (empty)*
* Guest Port: 22

![Port forwarding rules window](https://curiousstemlovingfellow-blog-data.s3.amazonaws.com/blog-posts/manager-window-settings-2.png)

From the host OS - open a terminal/command prompt window and **SSH to the guest OS**; 

```c
ssh <guest username>@localhost -p 2222
```

##### Connect via IP address (optional)

If port forwarding doesn't work or you prefer to connect via IP address, find the guest OS IP from the output of the `ifconfig` command. Open the *settings* window, shown above, select *network -> attached to -> host-only adapter* save selection. Using the guest OS IP address, create a SSH connection;

```c
ssh <guest os username>@<guest os ip>
```

### Login with SSH keys

Copy your public key to the guest/remote host using `ssh-copy-id`. See references for generating a [RSA key pair](https://help.github.com/en/github/authenticating-to-github/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent#generating-a-new-ssh-key). To facilitate the use of scripts, don't include a passphrase while generating keys. 

```c
ssh-copy-id <remote username>@<remote ip> -i ~/.ssh/<public key>.pub
```

Example command for a guest OS running in VirtualBox:

```c
ssh-copy-id ubuntu@localhost -i ~/.ssh/gen-nopass_ras.pub -p 2222
```

User authorization with RSA public key has been achieved. The command `ssh ubuntu@localhost -p 2222` no longer prompts for a password.

<div className="gap-30"></div>

> ## References
> * [The Secure Shell Protocol](https://tools.ietf.org/html/rfc4251)
> * [SSH and SSH2](https://kb.iu.edu/d/aelc)
> * [Ubuntu 18](https://www.ubuntu18.com/install-openssh-ubuntu-18/)