Canonical Multipass

Posted on October 2, 2023 • 6 min read • 1,173 words

Create virtual maschines using Multipass

Canonical Multipass
Photo by Growtika  on Unsplash 

What is Canonical Multipass?

If no graphical user interface is required, this is really super easy.

At the end of this article we have the option of running a virtual machine that is accessible via SSH or public key. With such a VM you can, for example:

  • Try out the steps of a software installation without risk. If you have uninstalled yourself, delete the VM and try again.
  • Try out server deployments locally. The local network is much faster than the connection to the hosted server, which means that late errors in the script can be reproduced more quickly.
  • Try out Ansibel playbooks without risk before running them on a hosted server.
  • find out how many GB of RAM an application needs for stable operation

Installing Multipass

The tool is installed on an Ubuntu Linux machine via snap:

sudo snap install multipass

Handling VMs with Multipass

Show pre-built Images

multipass find
In October 2023, you could choose between these images:

Image Aliases Version Description
core core16 20200818 Ubuntu Core 16
core18 20211124 Ubuntu Core 18
core20 20230119 Ubuntu Core 20
core22 20230119 Ubuntu Core 22
20.04 focal 20230922 Ubuntu 20.04 LTS
22.04 jammy,lts 20230927 Ubuntu 22.04 LTS
22.10 kinetic 20230524 Ubuntu 22.10
23.04 lunar 20230926 Ubuntu 23.04
appliance:adguard-home 20200812 Ubuntu AdGuard Home Appliance
appliance:mosquitto 20200812 Ubuntu Mosquitto Appliance
appliance:nextcloud 20200812 Ubuntu Nextcloud Appliance
appliance:openhab 20200812 Ubuntu openHAB Home Appliance
appliance:plexmediaserver 20200812 Ubuntu Plex Media Server Appliance
Blueprint Aliases Version Description
anbox-cloud-appliance latest Anbox Cloud Appliance
charm-dev latest A development and testing environment for charmers
docker 0.4 A Docker environment with Portainer and related tools
jellyfin latest Jellyfin is a Free Software Media System that puts you in control of managing and streaming your media.
minikube latest minikube is local Kubernetes
ros-noetic 0.1 A development and testing environment for ROS Noetic.
ros2-humble 0.1 A development and testing environment for ROS 2 Humble.
The selection of different Ubuntu versions is certainly interesting, but also the docker (incl. Portainer) or minikube variant. Otherwise, you can install additional applications or tools as usual via the shell.

Crating a new Virtual Machine

multipass launch <name of image>
multipass launch jammy

Create a Virtual Machine with custom name

multipass launch <name of image> --name <your VM name>

User-defined settings for number of CPUs, disc size and memory

multipass launch <name of image> --cpus 4 --disk 20G --memory 8G

You can also define your own network properties at startup (see Multipass Documentation )

List active Virtual Machines

multipass list
More Info
multipass info <name of VM>

Shell-Connection

The default and easiest way to work with a running VM is to open a shell in the virtual machine via its instance name.

multipass shell <name of VM>
Logout using CTRL-D

An SSH connection is not provided in the basic settings. It will be shown later how this can be configured (SSH connection via public key or via user & password ).

Run a Command from outside the Virtual Machine

multipass exec <name of VM> -- <Command>
multipass exec <name of VM> -- lsb_release -a   # e.g., gather information about the OS

Shutting down a Virtual Machine

A running VM can be shutdown using stop.

multipass stop <name of VM>
Installed content stays unchanged.

Start / Boot a stopped Virtual Machine

A stopped VM can be restarted/booted via start.

multipass start <name of VM>

Delete an Virtual Machine

A specific instance of a VM can be deleted using:

multipass delete <name of VM>

Purge completely

Instances that have already been deleted are permanently removed from the system via:

multipass purge

Connecting via SSH (Public Key) to a Virtual Machine

If you want to try out Ansible Playbooks in a VM, for example, you need a user who can log into the VM via SSH using the local SSH public key, i.e. without entering a password.

SSH Key Pair

On Linux systems, the private and public SSH keys are often located in the hidden directory ~/.ssh. The pair consists of two files that can be named as desired. The public key is often called the same as the private key with the suffix .pub appended, e.g. id_rsa (private) and id_rsa.pub for the public key.

However, it is possible that the directory and therefore the SSH keys do not yet exist, simply because they have not yet been used. In this case, the SSH key pair can simply be generated.

ssh-keygen -t rsa -b 4096
Generating public/private rsa key pair.
Enter file in which to save the key (/home/ubuntu/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/ubuntu/.ssh/id_rsa     #<< private ssh key
Your public key has been saved in /home/ubuntu/.ssh/id_rsa.pub     #<< public ssh key
The key fingerprint is:
[...]

Copy local SSH Public Key to Virtual Machine

  • Open the file ~/.ssh/id_rsa.pub locally and copy the content.
  • Open the file ~/.ssh/authorised_keys within the Multipass instance, e.g. with nano and copy the public key from the clipboard to the end of this file.

Connecting

The user of the public key just copied can now log into the instance from the local computer.

ssh ubuntu@<IP-of-multipass-VM>

The connection is established without requesting a password, as authentication is based on the public key.

Create a VM using User and Password for SSH-Connection

When the instance is created, a configuration file (Cloud-Init) is transferred, which ensures the creation of the user with password and enables the SSH login via user and password.

Configuring User and Password

---
users:
  - name: myadmin
    plain_text_passwd: 'myAdminPassword'
    shell: /bin/bash
    groups: users, admin
    lock_passwd: false
    chpasswd: { expire: False }
ssh_pwauth: True 

The instance is created in the same way as before, only the cloud init script is also transferred

multipass launch <name of image> --cloud-init cloud-init.yaml --name <your own name>

Connect

The SSH connection is established in exactly the same way as using the public SSH key,

ssh myadmin@<IP-of-multipass-Instance>
myadmin@<IP-of-multipass-Instance>'s password:

but this time the user password is requested.

Résumé

Whenever you want to try something out, under different operating system versions, with different versions of software libraries, locally optimize an Ansible playbook or much more, even the ambitious home lab user often runs out of physical machines. With virtual machines based on Multipass, the boundaries of your own home lab can be significantly pushed back.
…as long as you have enough main memory…

Multipass Documentation