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.
Create an Ubuntu VM, run:
multipass launch 24.04 -n dist-sys -c 6 -m 8G -d 40G
This command will create an Ubuntu 24.04 instance named dist-sys with 6 CPUs, 8G memory, and 40G disk.
After installation, you can return to the terminal and run multipass shell dist-sys 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 a 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 dist-sys 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 present). 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 dist-sys. 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 the section "REMOTES (TUNNELS/SSH) -> SSH" and right-click to pull up the pop-up panel and click "Open SSH Config". As shown in the screenshot below.
VS Code will have a pop-up for selecting the SSH configuration file to edit. Click on the one that ends with .ssh/config as shown below.
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 paths. For Windows, you need to use "~\.ssh\id_rsa".
Save the changes and refresh the remote connection list in 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. Therefore, you may need to manually adjust the IP address in the SSH configuration file.