KVM refers to the Kernel-based Virtual Machine which helps to run multiple Linux or window-based isolated guests along with their own OS and virtual dedicated hardware. To run KVM your system must be compatible with hardware virtualization extensions, such as AMD-V or Intel-VT.

The VM template is a copy of the virtual machine including specific virtual machine configuration as well as guest OS. If you need to deploy multiple VM of the same instance then creating a virtual machine using a template comes in handy which plays a huge role in saving time as well as storage.

This tutorial shows you how to create a VM template from the existing VM on KVM. In this article, the installation of KVM and creating Linux OS templates are done on Ubuntu 20.04 system. Before continuing to the process you are pre-requested to ready the system with KVM installed on it.

Creating a KVM Virtual Machine

First, make sure libvirtd daemon is running on your system. If not then use systemctl command to start and enable the daemon.

$ sudo systemctl start libvirtd
$ sudo systemctl enable libvirtd

Checking libvirtd daemon status,

$ sudo systemctl status libvirtd

If your current system is Ubuntu/Debian, enable the vhost-net kernel module using the following command.

$ sudo modprobe vhost_net

To create the template we first needed to have an installation instance on the system. We will be creating 15 G centos 8 KVM images using the qemu-img command given below.

$ sudo qemu-img create -o preallocation=metadata -f qcow2 /var/lib/libvirt/images/centos8.qcow2 15G

Create qemu image

Then, create the virtual machine using the virt-install command mentioned below. I will use CentOS 8 for the guest OS, but you may use any other Distribution. Just take care to adjust the path to the ISO image which contains the OS that shall be installed (/home/janak/Downloads/CentOS-8.4.2105-x86_64-boot.iso) to match the path of the OS you like to install in the VM.

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

Install OS in KVM qemu image

In the above command replace the parameter of –cdrom option with your downloaded centos iso image. Then the command will install and run the VM in your KVM so open your KVM using virt-manager.

$ sudo virt-manager

Once you open the KVM you can see your VM is in a running state so open the VM then the system will begin the boot process and you can see the following initial setup page of the centos. Then, go through the setup process and finish the setup.

Linux OS installer

Creating Virtual Machine Template in KVM

Once you finish up the setup login into your system, update your system, and install all the basic and required packages.

To update packages info in the system repository, use:

$ sudo yum update -y

Install some basic packages you think will be necessary. In my case, I have installed the following packages.

$ sudo yum install curl epel-release unzip telnet wget -y

Then, disable the zeroconf route. Zeroconf is the networking method to communicate between two computers and allow them to share each other’s resources. It uses Internet Protocol (IP) to establish communication between two computers.

$ echo "NOZEROCONF=yes" | sudo tee -a /etc/sysconfig/network

Now, power off your virtual machine to continue to further process.

$ sudo poweroff

In your normal terminal execute the following virt-sysprep command to clean the instance. The virt-sysprep is the command-line utility for reconfiguring or resetting the VM in order to make it cloneable.

$ sudo virt-sysprep -d centos8

CentOS sysprep

In the command, centos8 refers to the name of your installation VM so replace it with your VM name if you have a different one. If your system doesn’t have virt-sysprep installed you can install it using the command given below.

$ sudo apt update
$ sudo apt install libguestfs-tools

Lastly, execute the following virsh undefined domain command to remove the guest virtual machine configuration.

$ sudo virsh undefine centos8

Remove virtual machine

Congratulations, your template has been created. Now you can clone and deploy many multiple instances of it.


Thank you for reading this article. In this article, we create the Virtual Machine and learn how we can create the template for creating multiple instances of it and deployment.

How to Create Linux OS Templates with KVM on Ubuntu 20.04

Karim Buzdar

About the Author: Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. You can reach Karim on LinkedIn