A Proxmox VM template is a preconfigured virtual machine image that can be used to create new virtual machines. The template is essentially a snapshot of a virtual machine that has been configured with a specific operating system, applications, settings, and any other necessary configurations, allowing you to deploy new virtual machines quickly to save time and effort. In this post, you will learn how to create a VM template and quickly clone a new VM from the template.
Avatar
Latest posts by Surender Kumar (see all)

Prepare the VM

To create a template for a Windows VM, you can run Sysprep and then directly skip to the next step. If you already have an Ubuntu VM to convert into a template, you don't need to follow all the steps mentioned in this section. Instead, you just need to remove machine-specific settings, such as machine ID, SSH host keys, and IP addresses, with the following commands:

# truncate the machine-id file
sudo truncate -s 0 /etc/machine-id

# remove ssh host keys
sudo rm -f /etc/ssh/ssh_host_*

# remove ip addresses; change enp0s3 to your own interface name
sudo ip addr flush dev enp0s3

It is worth noting that you should not delete the /etc/machine-id file but truncate it (remove its content) only, as shown in the first command above. Deleting this file could cause unexpected errors in your VM. Shut down your VM after running these commands, and go directly to the next section.

For demonstration purposes, I will set up a new VM with an Ubuntu 22.04 cloud image. Cloud images have the cloud-init package installed, so I will use it to avoid the hassle of installing an operating system manually. For those who don't know, cloud-init is a cloud instance initialization package, which provides a way of storing various settings, such as hostname, locale, ssh keys, and other user data, in a configuration file. When your VM boots for the first time, cloud-init automatically applies these settings to configure the VM for you the way you like.

  1. I will first create a VM in Proxmox with slightly unusual settings, as shown in the screenshots below:
    Create a VM without any ISO media in Proxmox

    Create a VM without any ISO media in Proxmox

    Create a VM without any hard disk in Proxmox

    Create a VM without any hard disk in Proxmox

    You can see in the screenshots that I did not choose any ISO media and removed the default disk so the VM doesn't have any disk at the start.

  2. We will now download the cloud image on our Proxmox server. The Proxmox GUI, unfortunately, doesn't have any option to download and move the image to the right place, so we need to do that on the CLI. Switch to the Proxmox server terminal and run the following command:
    wget https://cloud-images.ubuntu.com/minimal/releases/jammy/release/ubuntu-22.04-minimal-cloudimg-amd64.img -O /tmp/ubuntu-22.04.img
    Downloading the minimal cloud image of Ubuntu on the Proxmox server

    Downloading the minimal cloud image of Ubuntu on the Proxmox server

    This command downloads the minimal cloud image with Ubuntu 22.04 into the /tmp directory on the Proxmox host.

  3. Now, we need to import this image to the Proxmox storage. If you're using block-based storage (e.g., lvm or ceph), the disk will be imported as raw. If you're using directory-based storage (e.g., local or NFS), it is a good idea to import the image with the qcow2 format to utilize the live snapshot feature. To view all the storage, use the pvesm status command, as shown below:
    View all storage in Proxmox with CLI

    View all storage in Proxmox with CLI

    You can see in the screenshot that I have both types of storage available in Proxmox. The local storage is the directory type, and the lvm-datastore is the lvmthin (block) type.

  4. To import the cloud image into the lvm-datastore, use the following command:
    qm importdisk 999 /tmp/ubuntu-22.04.img lvm-datastore
    

    Make sure you replace 999 with the VM ID of your own VM.

    Importing the cloud image to the VM template

    Importing the cloud image to the VM template

    The success message at the end shows that the image was imported as an unused disk in the lvm-datastore, so we need to manually attach it later in the VM settings.

  5. To view the imported disk through CLI, you can run pvesm list lvm-datastore, as shown in the screenshot below:
    Viewing the imported disk in Proxmox storage with CLI

    Viewing the imported disk in Proxmox storage with CLI

    The format column shows that the disk format is raw, which was expected since we imported it to block storage. To import the image to a directory type storage (e.g., local), you need to run the following command instead:

    qm importdisk 999 /tmp/ubuntu-22.04.img local --format qcow2
    

    The --format qcow2 option ensures that the image is imported in the qcow2 format. That is all we need to do in the Proxmox terminal. Let's now switch back to the GUI to make further changes to the VM.

  6. In the Proxmox GUI, select the VM, click the Hardware tab, and then double-click Unused Disk.

    Manually adding the imported disk in the VM template hardware settings

    Manually adding the imported disk in the VM template hardware settings

  7. Now choose the disk image that you imported earlier. If you're using an SSD, you can also enable SSD emulation under the Advanced options. Finally, click the Add button. The disk is attached to the VM.

    Additional settings for adding disks in Proxmox

    Additional settings for adding disks in Proxmox

  8. Add a Serial Port and a CloudInit drive to the VM by clicking the Add button on the Hardware tab. See the following screenshot for reference:
    Adding a Serial Port and CloudInit Drive to the VM template

    Adding a Serial Port and CloudInit Drive to the VM template

    We are adding a serial port because it is used by most cloud-init images for display. Without this, you will not see a screen when the VM is started. The CloudInit Drive lets you define various settings, such as username, password, IP config, SSH keys, etc.

  9. After adding the serial port, double-click the Display option, set Serial terminal 0 as the graphics card, and then click OK.
    Changing the display to Serial terminal 0 in the VM template settings

    Changing the display to Serial terminal 0 in the VM template settings

    If you still don't see any display when the VM boots, you might need to revert this setting to Default.

  10. The disk we imported and attached is not automatically added to the boot order. So, click the Options tab, double-click the Boot Order option, then add the disk to the boot order, move it to the top, and click OK.
    Adding the disk to the boot order in the VM template settings

    Adding the disk to the boot order in the VM template settings

    You may also want to enable the Start at boot option to make sure the VMs cloned from this template are automatically started when the Proxmox server is started.

    Enabling the Start at boot option for the VM template

    Enabling the Start at boot option for the VM template

  11. Finally, click the Cloud-Init tab, configure a username, password, DNS server, SSH public key, and IP address, and click the Regenerate Image button when you're done. Setting a username and password is essential here; otherwise, you won't be able to log in when a VM is cloned from this template.

    Configure cloud init settings and regenerate the image

    Configure cloud init settings and regenerate the image

That's it. Your VM is now ready to be converted into a template.

Create a VM template

To create a template, right-click your VM and select the Convert to template option, as shown in the screenshot below:

Converting a VM to a template in Proxmox

Converting a VM to a template in Proxmox

You will see a confirmation prompt. Note that you cannot undo this process, so before confirming this operation, make sure you have followed all the steps.

Confirmation prompt to convert VM to template

Confirmation prompt to convert VM to template

Click Yes to proceed, and the VM will be converted to a template within a few seconds. You will notice the VM icon change a little bit to indicate that it is now a VM template instead of a regular VM.

Viewing the VM template in Proxmox

Viewing the VM template in Proxmox

Deploy a VM from template

Now that we have our first VM template ready, let's quickly create a VM using it. To do so, right-click the template and select the Clone option.

Create a new VM from the template in Proxmox

Create a new VM from the template in Proxmox

You will now see a bunch of options for the new VM that you are deploying from the template, as shown in the image below:

Clone VM template options

Clone VM template options

Select the required options, click Clone, and you are done. The most important option that I want to point out is Mode. When cloning a VM template, you get two choices: Full clone and Linked clone. A full clone, as the name suggests, is a fully independent copy of the VM template, which uses more storage than a linked clone. A linked clone requires access to the original VM template as it uses all its unchanged files. This saves a lot of disk space. Just keep in mind that a linked clone VM cannot be started without the original template.

Once your new VM is created, you can start it and log in with the username/password you defined in the CloudInit section (or with your previously defined credentials if you used an existing VM). The following screenshot shows that a new machine ID is automatically generated for our new VM, and thanks to cloud-init, my SSH public key is also copied to the .ssh\authorized_keys file.

Subscribe to 4sysops newsletter!

Viewing the settings of the newly deployed VM

Viewing the settings of the newly deployed VM

You can now configure a static IP (or use an auto-assigned IP) to connect the new VM over SSH using public key authentication.

3 Comments
  1. Avatar
    Andy 4 weeks ago

    You can clone from any other VM directly without creating a Template, so what is the point of creating a template? What are the differences?

    avatar
    • Avatar Author

      Quoting from the official Proxmox docs:

      Deploying virtual machines from templates is blazing fast, very comfortable and if you use linked clones you can optimize your storage by using base images and thin-provisioning.

      avatar
    • Avatar Author

      You don’t get an option to create linked clones when cloning a VM. Linked clones can save a lot of disk space when your host has many VMs.

Leave a reply

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

*

© 4sysops 2006 - 2023

CONTACT US

Please ask IT administration questions in the forums. Any other messages are welcome.

Sending

Log in with your credentials

or    

Forgot your details?

Create Account