Create a VM with Incus
Creating a basic Virtual Machine with Incus is a simple command via the terminal.
Create VM using the terminal
Using a user that is part of the incus-admin group execute the following command to get a basic Ubuntu 22.04 VM:
incus launch images:ubuntu/22.04 vm-name --vm --type c1-m4
WTF do the flags means?!
--vmTells Incus we want a Virtual Machine and not a container--typeSpecifies the core count (cX) and amount of memory (mX). You can also use AWS image sizes e.g. t1.mirco
Disk Size
By default the VM will have a very small disk, because of this I tend to customise the size by modifying the above command with the --device command like so:
incus launch images:ubuntu/22.04 vm-name --vm --type c1-m4 --device root,size=30GiB
This will give me a 30 GiB root partition.
Cloud Init
One of the benefits of LXD/Incus is the ability to use cloud-init.
This allows VMs to be configured on boot to add things like:
- Add Users
- Packages
- Files
With Incus I tend to always use a base cloud-init profile to add a user, setup sudo and ensure OpenSSH server is installed.
Create a cloud-init profile
Creating a cloud-init profile is simple, at it's core cloud-init uses a YAML file.
My cloud-config.yml looks similar to this, obviously changed in places for security purposes:
#cloud-config
package-update: true # Run a package update
packages: # The packages you want to install
- sudo
- openssh-server
users:
- default # Default user, worth keeping unless you know what you are doing
- name: andy # Username
shell: /bin/bash # Default shell
lock_passwd: false # Setting this to true means you can only SSH using public, private key
passwd: $6$rounds=4096$dtOF9U0rJEXp5sog$Z2So4IjqhC2qOoJQ1VCTeZh6QL5pkogbtHN/DMHJ5jsTgSigIdY5ivJzvi77VdFZotXTVma28jgOq2OSncBkh0 # password01 - Yes totally my real password ;)
ssh_authorized_keys:
- ssh-ed25519 SomeRandomNumbersAndLetters # Public SSH key
groups:
- sudo # Add the user to the sudo group
sudo: ALL=(ALL:ALL) ALL # Sudo config for the user, this means password is required to elevate
Once you have created your version it is worth validating it by using the cloud-init schema command:
sudo cloud-init schema --config-file cloud-config.yml
How do I create a password hash?
On a Linux machine use the tool mkpasswd1 like so:
mkpasswd --method=SHA-512 --rounds=4096
OK so how do I create a VM using this cloud-init file?
By doing two modifications to the command above you can launch a VM that is configured using cloud-int.
Firstly we need to use a different image as the one above doesn't have cloud-init support. This isn't too difficult as most of the linuxcontainer images have a cloud variant that can be used by appending /cloud to the image, for example:
images:ubuntu/22.04 has no cloud-init
images:ubuntu/22.04/cloud has cloud-init
We also need to specify the cloud-init file by passing the --config flag:
incus launch images:ubuntu/22.04 vm-name --vm --type c1-m4 --config=user.user-data="$(cat cloud-config.yml)"
Connecting to VM
There are three options to connect to this VM:
- Incus exec
- SSH
- Console
Incus Exec
If you haven't enabled SSH you can connect to the VM using the Incus command:
incus exec vm-name bash
This will drop you into the machine as the root user.
SSH
If you have installed OpenSSH server and added a user you can connect to the VM using SSH.
First find the IP Address of the Virtual Machine. The easiest way to do this is using the command:
incus list
You'll get an output similar to this:
+---------------+---------+-------------------------+------------------------------------------------+-----------------+-----------+
| NAME | STATE | IPV4 | IPV6 | TYPE | SNAPSHOTS |
+---------------+---------+-------------------------+------------------------------------------------+-----------------+-----------+
| test-instance | RUNNING | 192.168.40.217 (enp5s0) | fd42:4a06:cc15:9d0:216:3eff:feab:77db (enp5s0) | VIRTUAL-MACHINE | 0 |
+---------------+---------+-------------------------+------------------------------------------------+-----------------+-----------+
Then you can connect over SSH using the IPV4 address.
Console Access
It's also possible to connect over console if you have a user setup on the VM. This can be accessed with the command:
incus console vm-name
You'll then be dropped into a console, to exit the console use the key combination Ctrl+a q.
Footnotes
-
You may need to install this tool, on Ubuntu it wasn't installed by default and required the install of whois (
apt install whois) ↩