TL;DR: multipass is a tool that uses available hypervisors to manage VM instances conveniently. VS code is a code editor that comes with many plugins. In this guide, we will create a VM instance via multipass and use VS Code to coding by ssh-ing into the instance. You will install multipass on your host machine and install/manage VM instances via the multipass interface.
Follow the official guide to install multipass on your host machine.
Download and install VS Code from the official website.
After installing the VS Code, install the "Remote - SSH" extension pack from here.
Note: The latest version of Multipass has introduced a GUI for configuring the VMs. You are free to use GUI. However, this article will focus on the command line interface.
Open the terminal of your choice.
Run multipass launch 24.04 -n <your-instance-name> -c <number-cores> -m <memory-size> -d <disk-size> to create an Ubuntu instance.
For example, multipass launch 24.04 -n dist-sys -c 6 -m 6G -d 40G will create an Ubuntu 24.04 instance named "dist-sys-test" with 6 CPUs, 6G memory, and 40G disk.
After installation, you can return to the terminal and run multipass shell <your-instance-name> to check if the instance has been installed correctly. This command will log into the newly created VM and connect to the shell session in the instance.
Generate public/private key pair on your host machine. Skip this step if you already have a public/private key pair on your host machine.
Run ssh-keygen -t rsa to generate the key pair.
By default, the public/private key pair will be created as files "~/.ssh/id_rsa.pub" and "~/.ssh/id_rsa" respectively. Open the public key "~/.ssh/id_rsa.pub" and copy the key.
Run multipass shell <your-instance-name> to enter the shell environment in your VM. In your VM, paste the copied public key to the file "~/.ssh/authorized_keys" (create the file if it is not presented). Save the changes.
Alternatively, if the "ssh-copy-id" program is installed on your host machine, you can run multipass info <your-newly-created-instance-name> to obtain the IP address of the instance. Then, run ssh-copy-id ubuntu@<ip-of-your-instance> and follow the prompt to transfer the public key.
Return to your host machine's terminal and run multipass info <your-newly-created-instance-name>. You shall see a list of information regarding the virtual machine. Note down the IPv4 address listed. The output can be something like this:
Name: dist-sys
State: Running
IPv4: 172.17.149.29
Release: Ubuntu 24.04 LTS
Image hash: 870bd58b5c1e (Ubuntu 24.04 LTS)
CPU(s): 4
Load: 0.00 0.00 0.00
Disk usage: 1.5GiB out of 9.6GiB
Memory usage: 227.7MiB out of 3.8GiB
Mounts: --
Open VS Code and open the "Remote Explorer" located on the sidebar. You will see a section "SSH" and right-click to "Open SSH Config". Enter the following information in the SSH config file:
Host <give-a-name>
HostName <ipv4-address-of-your-VM>
User ubuntu
PreferredAuthentications publickey
IdentityFile "~/.ssh/id_rsa"
Note:
By default, the "User" is "ubuntu". The "IdentityFile" refers to the private key you just generated. By default, it will be located in the path "~/.ssh/id_rsa".
The path separator used above is for Unix-like path. For Windows, you need to use "~\.ssh\id_rsa"
Save the changes and refresh the remote connection list in the VS Code. You shall see a newly created connection profile. You can right-click on the corresponding profile and establish the connection.
Note: Your instance's IP address might change after every reboot. So, you might need to manually adjust the IP address in the SSH config file.