Visual Studio Code Remote SSH Multipass
Tags: ubuntu, ssh, vscode, multipass
Published: 2021-05-30 18:31:58


This guide will show you how to remote SSH into a Ubuntu VM instance in Visual Studio Code.

![multipass](https://i1.wp.com/jonamdall.com/wp-content/uploads/2018/05/leeloo-dallas-multipass.gif?resize=480%2C220&ssl=1)

Multipass is a platform developed by [Canonical](https://canonical.com/) to prototype cloud launches locally for free. Learn more about Multipass and its capabilities by visiting [multipass.run](https://multipass.run/)

> This guide assumes you are comfortable using the terminal and connecting to a remote server environment without a GUI. Review the prerequisites to follow along.

## Prerequisites

- Homebrew for macOS
- Visual Studio Code (VS Code)
- Terminal, iTerm, or VS Code integrated terminal 
- Remote SSH extension for VS Code 
- SSH key

Let's begin.

## Installing Multipass on macOS

Open your preferred Terminal app and copy-paste the following command:

```
$ brew install --cask multipass
```

Verify the installation was successful:

```
$ multipass version
```
A sample output should look similar to this: 
```
$  multipass version
multipass  1.6.2+mac
multipassd 1.6.2+mac
```

That's it! 

Now you can start launching Ubuntu VM instances on the fly with Multipass.

# Launch a VM Instance

To quickly launch your first instance, type the following command:

```
$ multipass launch

// example output
Starting fitting-diplodocus |
```
Multipass will randomly create an alias name and provision your instance using the latest LTS Ubuntu image. 

Using this command also provisions your instance with default resources and a network such as:

- Storage: 5 GB 
- User: Ubuntu
- Password: N/A (can be modified)
- Network: Bridged (w/ Internet access)

Or you can type a command to view more information about your instance like so:

```
multipass exec keen-yak -- lsb_release --description

// example output
Description:  Ubuntu 20.04.2 LTS
```

If you want to list stopped or running instances, type :
```
$ multipass ls

// example output
Name                    State             IPv4             Image
Imag-Name               Running           191.128.04.24    Ubuntu 20.04 LTS
```

I personally like the robust horizontal output with more info. Also, make sure to copy the IPv4 Address shown below so you can use it later.
```
multipass info samcli

// example output
Name:           Image-Name
State:          Running
IPv4:           100.100.04.13
Release:        Ubuntu 20.04.2 LTS
Image hash:     9dd5cb9f73c4 (Ubuntu 20.04 LTS)
Load:           1.01 0.68 0.27
Disk usage:     1.3G out of 4.7G
Memory usage:   138.5M out of 981.3M
```

And finally, access the Ubuntu instance you just created by typing:
```
$ multipass shell Image-Name
```

### Deleting your instance

Once you decide you no longer need your VM instance, you can delete it by typing the following command:
```
$ multipass delete Image-Name
```
Deleting a VM instance only removes it from queue. Conceptually you can think of deleting a file and sending to the trash bin. 

> **Warning: You will not be able to recover your instance after executing this command.**

To completeley remove an instance from disk use the following command:
```
multipass purge
```

> Visit the [Multipass documenation](https://multipass.run/docs#command-line-reference) to get familiar with advanced command combinations to create and manage your Ubuntu VM instances.

Now, it's time to SSH into your instance using Visual Studio Code so you can, well, code.

# VS Code Remote SSH

> To follow along, make sure you have VS Code and the [remote SSH extension](https://code.visualstudio.com/docs/remote/ssh-tutorial#_install-the-extension) installed. For detailed instructions and troubleshooting, visit the [VS Code docs](https://code.visualstudio.com/docs/remote/ssh-tutorial).

Let's begin...

## Add SSH Host

Before we can SSH into our Multipass VM instance, you need to add a new Host in the user's SSH config file. You can directly access the file by locating the directory it's in.

```
// on macOS
/Users/<user-name>/.ssh/config
```
```
// on Linux
/home/ubuntu/.ssh/config
```
Edit the file by adding or appending these three lines:
```
Host Alias-Name
  HostName 100.100.04.13
  User ubuntu
```
What's going on here?
1. Host: A random name you chose.
2. HostName: Is the IPv4 address of the VM instance.
3. User: The user name of the instance.

## Add Authorization Key

Below are two links if you need a quick refresher on how to install and create SSH keys.

> Install Remote SSH extension [tutorial](https://code.visualstudio.com/docs/remote/ssh-tutorial#_install-the-extension).

> How to [generate SSH keys](https://code.visualstudio.com/docs/remote/ssh-tutorial#_set-up-ssh).

Log into your VM instance using the Multipass shell command and append your host SSH key to the **authorization_keys** file in the VM guest instance.

Finally, change to your home directory and create a directory of your choice.

Phewww! We're almost there...

## Connect to a VM Instance in VS Code

To SSH into your Multipass VM instance, navigate to the  **"Open a Remote Window"** button on the bottom left corner in VS Code. 

This will open a dropdown menu. Click on **"Connect Current Window to Host..."**. Next, select the alias or Host that you created earlier in the guide. VS Code will refresh the current window and establish an SSH connection. 

If it's not already open, expand the file explorer on the left pane and click on the green button called **"Open Folder"**. Use the command palette to navigate to the directory you created.

![shell](https://cdn.josuebustos.com/static/vscode-ssh/ssh_connect.gif)

> If you see this warning *"Visual Studio Code is unable to watch for file changes in this large workspace" (error ENOSPC)"* It means Ubuntu doesn't have enough memory to watch file changes. You have to increase the limit to its max 524,288. Refer to the VS Code docs to [workaround](https://code.visualstudio.com/docs/setup/linux#_visual-studio-code-is-unable-to-watch-for-file-changes-in-this-large-workspace-error-enospc) this issue.

AND that's it! You've successfully SSH into a Multipass Ubuntu instance using VS Code.

Enjoy prototyping the cloud!

Until next time...