Cloud template and Virtual Machine
Proxmox is installed and running, the next step is to create first Virtual Machine (VM). This guide walks you through the process step by step.
Cloud-Init in Proxmox VE
A cloud template in Proxmox is useful for quickly deploying pre-configured virtual machines with minimal setup. Here’s why you might need one:
Cloud-Init is the standard multi-distribution package designed to handle the initial setup of a virtual machine (VM). It allows configuration of network settings and SSH keys directly from the hypervisor. When the VM boots for the first time, Cloud-Init applies these settings within the guest system.
Many Linux distributions offer pre-built Cloud-Init images, primarily intended for OpenStack, but they are also compatible with Proxmox VE. While using these images may be convenient, it is generally recommended to create your own. This ensures full control over the installed components, making it easier to customize the image to fit specific needs.
Once a Cloud-Init image is prepared, it is best practice to convert it into a VM template. Using this template, new VMs can be rapidly deployed as linked clones, requiring only minor configurations such as network settings and SSH keys before launch.
For secure authentication, it is advisable to use SSH key-based authentication rather than passwords. While setting a password is possible, it is less secure, as Proxmox VE must store an encrypted version within the Cloud-Init data.
Proxmox VE generates an ISO image to pass Cloud-Init configuration data to the VM. Therefore, all Cloud-Init-enabled VMs require an assigned CD-ROM drive. Additionally, a serial console should be used as the default display, as many Cloud-Init images depend on it—especially those intended for OpenStack. However, if a specific image has display issues, reverting to the default display configuration may be necessary.
Refer the official website for the more information on the cloud-init and the installation steps.
Let us use the cloud image provided by Ubuntu.
Preparing Cloud-Init Templates
# Download the latest ubuntu cloudimg
wget https://cloud-images.ubuntu.com/noble/current/noble-server-cloudimg-amd64.img
QEMU Guest Agent in Proxmox VE
The QEMU Guest Agent is a service that runs inside virtual machines (VMs) to enable better communication between the guest OS and the Proxmox host. It helps with VM management, improves performance, and provides additional functionality.
Install qemu guest agent on the image downloaded
# Install virt-cutomise to make modifications to the cloud image
sudo apt install libguestfs-tools -y
# Install the agent
sudo virt-customize -a noble-server-cloudimg-amd64.img --install qemu-guest-agent
ZSH
and not Bash
Oh My Zsh is a popular, open-source framework that I use to improve command-line experience.
sudo virt-customize -a noble-server-cloudimg-amd64.img --install zsh
sudo virt-customize -a noble-server-cloudimg-amd64.img --run-command 'chsh -s $(which zsh)'
sudo virt-customize -a noble-server-cloudimg-amd64.img --run-command 'sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"'
Timezone
Not a mandatory stuff, but I like to keep all my VM’s in the timezone that I liike
sudo virt-customize -a noble-server-cloudimg-amd64.img --timezone "America/New_York"
The command truncate -s 0 /etc/machine-id /var/lib/dbus/machine-id is used to empty the contents of both files, effectively setting their size to zero. These files store a unique machine identifier that is utilized by various system services, including D-Bus. By truncating them, the system ensures that a new unique ID is generated upon the next reboot. This is particularly useful from multiple instances from sharing the same machine ID.
sudo virt-customize -a noble-server-cloudimg-amd64.img --run-command 'truncate -s 0 /etc/machine-id /var/lib/dbus/machine-id'
The virt-customise command generates a machine ID for the VM, which must be removed to prevent all virtual machines from sharing the same identifier.
Virtual Machine
# Create a new VM with VirtIO SCSI controller
sudo qm create 9001 --name "noble-cloud-qemu" --memory 2048 --cores 2 --net0 virtio,bridge=vmbr0
# Import the image as a local disk on local-lvm
sudo qm importdisk 9001 noble-server-cloudimg-amd64.img local-lvm
# Attach the disk to the virtual machine
sudo qm set 9001 --scsihw virtio-scsi-pci --scsi0 local-lvm:vm-9001-disk-0,discard=on
Ubuntu Cloud-Init images require the virtio-scsi-pci controller type for SCSI drives.
Add Cloud-Init CD-ROM drive
The next step is to set up a CD-ROM drive, which will serve as the medium for passing Cloud-Init data to the VM.
sudo qm set 9001 --ide2 local-lvm:cloudinit
To enable direct booting from the Cloud-Init image, set the boot parameter to order=scsi0, ensuring the BIOS boots only from this disk. This optimizes startup time by skipping the bootable CD-ROM check.
sudo qm set 9001 --boot c --bootdisk scsi0
Many Cloud-Init images require a serial console to be configured and used as the display. If this setup doesn’t work for a particular image, revert to the default display configuration.
sudo qm set 9001 --serial0 socket --vga serial0
Set the default network to dhcp
sudo qm set 9001 --ipconfig0 ip=dhcp
I want my working machine public key to be copied into every VM that is created. Copy your working machine ~.ssh/id_rsa.pub to proxmox ~/.ssh/id_mac.pub key on the proxmox
sudo qm set 9001 --sshkeys ~/.ssh/id_mac.pub
Enable the qemu agent that is installed earlier, so that every VM that is cloned will have this enabled by default.
sudo qm set 9001 --agent enabled=1
As a final step, converting the VM into a template is beneficial. This allows for the rapid creation of linked clones, making deployment significantly faster compared to generating a full clone.
sudo qm template 9001
The template has been created, so the image is no longer needed and can be deleted.
rm noble-server-cloudimg-amd64.img
Deploy VMs from Cloud-Init Template
Creates a full clone (independent copy, not linked to the template) virtual machine If any space constraint –linked can be used
sudo qm clone 9001 501 --name firstvm --full
Configure this virtual machine to start automatically when Proxmox reboots.
sudo qm set 501 --onboot 1
Increase the hard disk size by adding 50GB to the existing 2GB, as shown in the example below.
sudo qm resize 501 scsi0 +50G
Set the required RAM for the VM
sudo qm set 501 --memory 8096
Finally, you can either start the VM from the command line with the command below or use the web UI to start it.
sudo qm start 501
Obtain the assigned IP address from the summary view of the Virtual Machine and log in using the username ubuntu. No password is required since the SSH key has already been copied to the VM.
ssh ubuntu@ipaddress
Some helpful commands if required.
#Stops the VM
sudo qm stop <id>
# Destroys the VM
sudo qm destroy <id>
# Lists all the VMs
sudo qm list