Multipass
Multipass is suitable for creating local persistent VM easily, with image creation automation by cloud-init. It is not designed for ease of changing VM config, moving VM to another host.
Basic setup
sudo snap install multipass
sudo systemctl restart snapd
sudo snap restart multipass.multipassd
# Show all commands
multipass help
# List available images
multipass find
# Create new vm instance with image
multipass launch --name <vm-name> <image-name: default latest LTS image>
multipass launch --name <vm-name> 22.04 --cpus 2 --memory 2G --disk 20G
# List vm instances
multipass list
# Configure your default storage (in case of small system drive)
# Follow official guide if possible: https://multipass.run/docs/configure-multipass-storage#heading--linux
# Here is a quick cache of above link
sudo snap stop multipass
sudo snap connect multipass:removable-media # for /mnt or /media
sudo snap connect multipass:all-home # for /home/*
mkdir -p <path>
sudo chown root:root <path>
sudo mkdir /etc/systemd/system/snap.multipass.multipassd.service.d/
sudo tee /etc/systemd/system/snap.multipass.multipassd.service.d/override.conf <<EOF
[Service]
Environment=MULTIPASS_STORAGE=<path>
EOF
sudo systemctl daemon-reload
sudo snap start multipass
Advanced setup
## Password
# By default there is no password and password authenication is disabled
# $ passwd --status ubuntu
# ubuntu L 04/03/2021 0 99999 7 -1
# As shown above, it is locked, additionally default sshd block password login
# You can set password with sudo passwd ubuntu,
# note that no password is needed to sudo even after setting a password
# you would be using private key to login
ssh ubuntu@ip -i /var/snap/multipass/common/data/multipassd/ssh-keys/id_rsa
## Briding the VM
# Warning: it does not seems to work...
sudo snap install lxd
sudo snap connect multipass:lxd lxd
multipass set local.driver=lxd
# Wait a moment while it is connecting
# You can use `multipass list` to test if it has finish loading
# Message while it is still loading:
# list failed: cannot connect to the multipass socket
# Message while it has finished loading:
# No instances found. OR a valid list
# After loading, you can list the available networks
multipass networks
multipass launch --name <vm-name> 22.04 --cpus 2 --memory 2G --disk 16G --network eno1
# Now check the ip of new instances
multipass list
Notes:
1) If you have trouble, this page might be helpful:
https://gist.github.com/ynott/f4bdc89b940522f2a0e4b32790ddb731
2) Driver lxd and qemu are separated,
you cannot see instances of qemu when using lxd
3) Could not find a way to modify existing instance network now
VM operations
multipass start <vm-name>
multipass stop <vm-name>
# Remove vm instance
multipass delete <vm-name>
multipass purge
# Open interactive shell to VM
multipass shell <vm-name>
# Directly execute command in VM
multipass exec <vm-name> -- <command>
# Get VM config
multipass get local.<vm-name>.cpus
multipass get local.<vm-name>.disk
multipass get local.<vm-name>.memory
# Set VM config (VM needs to be stopped first)
multipass set local.<vm-name>.cpus=4
multipass set local.<vm-name>.disk=60G
multipass set local.<vm-name>.memory=7G
# Move files
multipass transfer <local-path> <vm-name>:.
multipass transfer <vm-name>:<vm-path> .
# Mount directory onto VM
multipass mount <local-path> <vm-name>:<vm-path>
multipass umount <vm-name>:<vm-path>
Exposing port
By default multipass vm are covered by host NAT. The VMs can see each other in a private network, the host and the VMs can see each other, but the VMs are not visible to the network the host resides in. This seems to be an intented design that bridged network is not added.
Here are solutions:
- LXD briding mentioned in above advanced setup section
Note: it does not work for me - ssh tunneling
You will need to find the ssh key used by multipass shell, or get your own key configured.
This may not be the intended solution since extra encryption layer is needed in a secure local communication,
which is a meaninglessly wasted overhead.
Use shell to get into VM and paste your public key in ~/.ssh/authorized_keys.
Then check the IP of the VM and do ssh tunneling:
ssh -N -L \*:3632:localhost:3632 ubuntu@vm-ip-in-nat
Note that bind address is set to\*to allow non localhost from accessing the tunnel. - Use iptable/ufw to perform port forwarding
TODO. Refer to:- https://maurow.bitbucket.io/notes/multipass-vm-port-forwarding.html
- https://www.cyberciti.biz/faq/how-to-configure-ufw-to-forward-port-80443-to-internal-server-hosted-on-lan/