This guide will show you how to remote SSH into a Ubuntu VM instance in Visual Studio Code.
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.
- Homebrew for macOS
- Visual Studio Code (VS Code)
- Terminal, iTerm, or VS Code integrated terminal
- Remote SSH extension for VS Code
- SSH key
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
Now you can start launching Ubuntu VM instances on the fly with Multipass.
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
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:
Visit the Multipass documenation 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.
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?
- Host: A random name you chose.
- HostName: Is the IPv4 address of the VM instance.
- User: The user name of the instance.
Below are two links if you need a quick refresher on how to install and create SSH keys.
Install Remote SSH extension tutorial.
How to generate SSH keys.
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...
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.
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 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...