Back

Setup virtual ubuntu servers (using Multipass)

Use canonical Multipass to setup disposable ubuntu instances

Multipass by Canonical is a fantastic tool used to setup Ubuntu virtual machines on a guest machine.

If you prefer a video guide, here’s the link to the video on YouTube.

Why is this needed?

Using virtual Ubuntu instances has lots of advantages

  1. Safely experiment with the Ubuntu OS without having to purchase new hardware.
  2. Test locally before deploying to a server.
  3. Use the OS hypervisor directly instead of installing third party software such as VirtualBox or VMWare.

Caveats

Multipass currently only works for Ubuntu images since it’s created by the Canonical foundation (who also provide Ubuntu).

Architecture

Installation

Head over to Multipass and run the necessary steps to install Multipass for your system.

The rest of this example uses the macOS installation. The easiest way is to install it via Homebrew.

~ brew install multipass

Note: Your password may be required for the installation.

Managing the First VM

To check what options are supported by the Multipass CLI, run the following

~ multipass --help

Let’s now setup our first VM. The launch option without any argument creates an instance with a random two word name.

~ multipass launch
Retrieving image: xxx%

When run for the first time, this command downloads the latest available Ubuntu LTS release. We’ll customize this in the next section but for now, let’s run this as it is.

List available instances using the following command.

~  multipass list
Name                    State             IPv4             Image
guiltless-arapaima      Running           192.168.64.4     Ubuntu 20.04 LTS

Note: guiltless-arapaima is a randomly generated instance name that will change every time a new instance is created if a name is not specified.

To get more information on this instance, use the info command.

~ multipass info guiltless-arapaima
Name:           guiltless-arapaima
State:          Running
IPv4:           192.168.64.4
Release:        Ubuntu 20.04.4 LTS
Image hash:     147e0cea207e (Ubuntu 20.04 LTS)
Load:           0.03 0.06 0.02
Disk usage:     1.3G out of 4.7G
Memory usage:   143.5M out of 969.8M
Mounts:         --

And finally we may want to delete an instance when it’s purpose is served.

Multipass has a concept of soft v/s hard delete.

To soft delete an instance, use the delete option.

~  multipass delete guiltless-arapaima
~  multipass list
Name                    State             IPv4             Image
guiltless-arapaima      Deleted           --               Not Available

The reason this is a soft delete is because we can use the recover option if necessary to recover this instance.

To properly delete this instance, use the purge option. It takes no arguments since it purges all deleted instances at once.

~ multipass purge
~ multipass list
No instances found.

Customization

The default options for managing VMs are quite nice to use but let’s improve the usability by some customization.

Let’s check the available customization options.

~ multipass launch --help

We’re now going to only look at the following options and discuss the rest later.

  • cpus: The default 1 CPU is used in this post. Adjust this based on your needs.
  • mem: The default 1G is used in this post. Adjust this based on your needs.
  • disk: The default 5G is used in this post. Adjust this based on your needs.
  • name: Choose a clear identifiable name. For this post, let’s use web-server.
  • image: For this post, let’s use Ubuntu 21.10 (Impish Indri)
multipass launch --name web-server impish
Launched: web-server

Let’s get some information on this server.

~ multipass list
Name                    State             IPv4             Image
web-server              Running           192.168.64.5     Ubuntu 21.10

~ multipass info web-server
Name:           web-server
State:          Running
IPv4:           192.168.64.5
Release:        Ubuntu 21.10
Image hash:     291314e41af9 (Ubuntu 21.10)
Load:           0.12 0.07 0.02
Disk usage:     1.3G out of 4.7G
Memory usage:   115.0M out of 964.5M
Mounts:         --

Common Operations

Executing shell commands

There are two ways of executing shell commands

  1. Using Multipass exec.
~ multipass exec web-server ls /
bin   dev  home  lost+found  mnt  proc	run   snap  sys  usr
boot  etc  lib	 media	     opt  root	sbin  srv   tmp  var

In the above example, we list the root directory of the web-server Ubuntu instance.

  1. Entering a shell session.

Running individual commands using exec is quite a cumbersome process. Instead, we can enter a shell session on the VM and have full access to run commands. This option essentially wraps the ssh command with some default options and credentials for ease of use.

~ multipass shell web-server

We can now run the same command inside the VM shell

ubuntu@web-server:~$ ls /
bin   dev  home  lost+found  mnt  proc  run   snap  sys  usr
boot  etc  lib   media       opt  root  sbin  srv   tmp  var

Note: The hostname is web-server and the ssh user is ubuntu.

Sharing folders

A common use for a local VM is to share some folders with it to run operations in the folder from the VM.

Important: If you’re using macOS, first head over to Settings -> Security & Privacy -> Privacy -> Full Disk Access and check multipassd.

Let’s create a folder in the host and share it.

~ cd Downloads
~ mkdir share 
~ cd share
~ multipass mount $(pwd) web-server:/share

We can check the status of the mount using the following.

~ multipass info web-server
...
Mounts:        $HOME/Downloads/share => /share

Now in the multipass shell, we can access this folder at its share point.

~ multipass shell web-server
ubuntu@web-server:~$ cd /share
ubuntu@web-server:/share$ ls

Now we can create files in either and they will be available in the other.

Accessing services

To access a service that’s running the VM from our machine, we simply have to find the IP address of the VM and port where the service is available and call it.

Let’s first setup a simple web server in the VM.

Python3 is bundled by default with most Ubuntu distros. We can use the http.Server package](https://docs.python.org/3/library/http.server.html) to spin up a simple HTTP Server.

Start a web server using the following command.

~ multipass shell web-server
ubuntu@web-server:~$ cd /share
ubuntu@web-server:/share$ python
ubuntu@web-server:/share$ python3 -m http.server

This creates a simple web server that lists the contents of the /share directory and is exposed on port 8000.

To access this rom our machine, we first need the IP address of the VM. This is available via the info command.

~ multipass info web-server
Name:           web-server
State:          Running
IPv4:           192.168.64.5
Release:        Ubuntu 21.10
Image hash:     291314e41af9 (Ubuntu 21.10)
Load:           0.02 0.04 0.01
Disk usage:     1.4G out of 4.8G
Memory usage:   115.4M out of 964.5M
Mounts:         /Users/krishnaiyereaswaran/Downloads/share => /share

192.168.64.5 is the VM IP address. To check if this is reachable, we can optionally ping this address.

Note: This value may be different when you run this example. Make sure to use the IPv4 address from the output of the info command.

To access the http server from our Host (macOS), we can use the curl command on use a browser.

curl  http://192.168.64.5:8000
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Directory listing for /</title>
</head>
<body>
<h1>Directory listing for /</h1>
<hr>
<ul>
<li><a href="test.txt">test.txt</a></li>
</ul>
<hr>
</body>
</html>

This is better formatted when using a web browser.

Uninstalling

If you want to uninstall Multipass, head over to Multipass and run the necessary steps to uninstall Multipass for your system.

If you used macOS with Homebrew as in this post, then run the following

~ brew uninstall multipass

References

  1. Multipass Documentation: https://multipass.run/docs
Last updated on May 29, 2022 11:20 +0200