How to Create a KVM Virtual Machine Template

A virtual machine template is essentially a copy of the installed virtual machine that comes in handy when you want to deploy multiple instances of virtual machines. Creating a template is a 3 step process that involves creating a virtual machine, installing all the requisites packages that you want to be installed, and finally cleaning up the template.

Let’s go ahead and see how you can accomplish this.

Step 1: Installing KVM in Linux

The first step is to install KVM on your system. We have comprehensive tutorials on:

Additionally, ensure that the libvirtd daemon is running and is enable to automatically kick on bootup.

$ sudo systemctl enable libvirtd
$ sudo systemctl start libvirtd

Verify if the libvirtd daemon is running.

$ sudo systemctl status libvirtd
Verify libvirtd Status
Verify libvirtd Status

If you are running a Ubuntu / Debian system, ensure that the vhost-net image is loaded.

$ sudo modprobe vhost_net

Step 2: Create a KVM Virtual Image

Before we get to creating a template, we need to, first of all, have an installation instance. On the command-line, we are going to create a 20G CentOS 8 KVM image using the qemu-img command as shown.

$ sudo qemu-img create -o preallocation=metadata -f qcow2 /var/lib/libvirt/images/centos8.qcow2 20G
Create CentOS KVM Image
Create CentOS KVM Image

Next, use the virt-install command to create a CentOS 8 virtual machine as shown.

$ sudo virt-install --virt-type kvm --name centos8 --ram 2096 \
--disk /var/lib/libvirt/images/centos8.qcow2,format=qcow2 \
--network network=default \
--graphics vnc,listen= --noautoconsole \
--os-type=linux --os-variant=rhel7.0 \
Create CentOS 8 Virtual Machine
Create CentOS 8 Virtual Machine

This launches the virtual machine instance. You can confirm this by heading over to the virt-manager and opening the console window as shown. What you can see is the default welcome page for the installer. Be sure to complete the installation to the very end.

CentOS 8 Virtual Machine Installation
CentOS 8 Virtual Machine Installation

Step 3: Creating KVM Virtual Machine Template Image

Once the installation is complete, log into the VM and update all the system packages.

$ sudo dnf update

Install the prerequisite packages that you feel are essential to get started with. In this case, I will install EPEL release, wget, curl, net-tools, and vim. This may be different for your case.

$ sudo dnf install epel-release wget curl net-tools vim

If you intend to deploy your template on a cloud platform, install the cloud-init packages as shown.

$ sudo dnf install cloud-init cloud-utils-growpart acpid

Next, disable the zeroconf route.

$ echo "NOZEROCONF=yes" >> /etc/sysconfig/network

Once you are done, be sure to power off your virtual machine and clean up the VM template image as shown.

$ sudo virt-sysprep -d centos8

The virt-sysprep is a command-line utility that resets a virtual machine in order for clones to be made from it. It removes entries such as SSH host keys, log files, user accounts, and some persistent network configurations. To use the command, first, you must always make sure that the VM is powered off.

$ sudo virt-sysprep -d centos8
Clone CentOS 8 Virtual Machine
Clone CentOS 8 Virtual Machine

Lastly, invoke the command shown to undefine the VM domain.

$ sudo virsh undefine centos8
Undefine VM Domain
Undefine VM Domain

The template image is now ready for cloning and deployment.

Tutorial Feedback...
Was this article helpful? If you don't find this article helpful or found some outdated info, issue or a typo, do post your valuable feedback or suggestions in the comments to help improve this article...

If You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Support Us

We are thankful for your never ending support.

1 thought on “How to Create a KVM Virtual Machine Template”

  1. Hello,

    Could you please provide the next step? I mean how to find a template for cloning? I cannot do it. After undefined, I don’t see vm at all.

    Thank you


Got something to say? Join the discussion.

Have a question or suggestion? Please leave a comment to start the discussion. Please keep in mind that all comments are moderated and your email address will NOT be published.