Canonical Multipass

Multipass is a simple but powerful utility from Canonical that at its core lets you easily launch Ubuntu virtual machines from the command line.

There is a lot more to Multipass than just launching Ubuntu images, but I think if you need more features than that, you’re probably better off looking at something like Vagrant which is a lot more advanced.

But for a simple workflow of: “I need an Ubuntu VM to test something…”, Multipass is a great, lightweight utility. I’m not going to provide a thorough review or walk-through, but here are some tips and tricks that may help you and may show you if it’s the right tool for you.

Launching a VM

The most basic command to provide Multipass is the following which launches a VM with 1 core, 1GB memory, and a randomly-generated name:

$ multipass launch

Once completed, you can list the VMs using the following command, and you’ll see some similar looking output:

$ multipass list
Name                    State             IPv4             Image
hearty-stonechat        Running    Ubuntu 18.04 LTS

Some notes about the output…

  • The name of the VM is humorous but unnecessarily long.
  • There is no control over the networking, by default Multipass uses a NAT network and this cannot be changed (although there is a request to enable bridged mode).
  • The default Ubuntu version is the latest LTS.

Let’s launch another VM with some more options defined:

$ multipass launch --cpus 2 --mem 4G --disk 20G --name testy

For my use case, I very rarely need to spin up a VM with more than the defaults so I don’t usually specify the cpus, mem or disk options, but specifying the name is almost always a good thing.

The other option which I find very useful is “--cloud-init” which lets you pass a cloud-init file with some setup functionality that gets executed each time you launch a VM. You can read more about cloud-init files here: I’m only using a tiny subset of functionality but I find the following cloud-init file helps me to SSH into the VMs, updates all packages, and installs a couple of essentials:

- name: stuart
  shell: /bin/bash
  - <my really long SSH public key goes here> 

package_upgrade: true

 - unzip
 - git
 - curl

Just a note about SSH… by default you can’t easily SSH into a VM. Instead, you’re expected to use the multipass shell command to connect into the VM using the default ubuntu account that’s created for you. What the above cloud-init file does is create a new user called stuart configured with the public key of my SSH key so that I can easily SSH into the VM. The main reason I do this is so that I can use Ansbile to run commands in my VMs once running.

To help me work with Multipass, I created three bash aliases that I use to launch or list instances:

alias mp='multipass'
alias mpll='multipass list'
alias mpl='multipass launch --cloud-init /home/stuart/Documents/Multipass/base-cloud-init --name'

The first two are fairly straightforward and exist solely because I’m lazy and don’t want to type multipass each and every time. The mpl command passes the cloud-init file mentioned above and also forces me to give a name for each VM, if I don’t give a name the command will fail. So the following command creates a VM with the default specs, gives it the name ‘test1’ and sets it up with the cloud-init file:

$ mpl test1

I can then get the IP address of the VM:

$ mpll
Name                    State             IPv4             Image
hearty-stonechat        Running    Ubuntu 18.04 LTS
test1                   Running     Ubuntu 18.04 LTS

And then SSH straight in:

$ ssh

So this is as much of the functionality of Multipass that I’ve found useful. There’s a lot more that can be done including launching other Ubuntu images as well as custom images. You can read further documentation here:

Leave a Reply

Your email address will not be published. Required fields are marked *