How to Create Virtual Machines on KVM – The Ultimate Guide


In this article we will look at how to create virtual machines on kvm, but let’s first look at what kvm actually is.

KVM stands for Kernel-based Virtual Machine. It is a virtualisation solution inbuilt into the linux kernel. It is a loadable kernel module that provides the virtualisation capabilities to the kernel and transforms it into a Type-1 bare-metal hypervisor. You can run multiple virtual machines on a host using KVM running on either Linux or Windows OS images.

Linux is an opensource kernel and hence KVM is a free hypervisor solution unlike ESXi, Windows Hyperv etc. It is available since the version 2.6.20. The userspace component of KVM is available in QEMU since version 1.3.

KVM is one of the widely used hypervisor of choice for the following features:

  • Free: It is free unlike other virtualisation solutions as it is an opensource software.
  • Over-committing: More CPUs and RAM can be allocated to the VMs than there are resources available on the system. This can be done as most of the processes don’t consume all the resources allocated to them at all times.
  • Thin Disk Provisioning: Allocates only that amount of disk space to a VM that it is consuming, contrary to thick provisioning which allocates all the disk space required by a VM, allowing to save up the disk space.
  • Hardware Support: KVM supports a wide variety hardware platforms. Many hardware vendors contribute to the Linux kernel development, hence their support is often added to it.
  • Live Migration: KVM allows for live migration of VMs, which is a very handy feature. Live migration refers to the transfer of running VM from one KVM host to another, without shutting it off, affecting its network connection or applications running on it.

How to Install KVM

Follow these instructions step-by-step to install KVM on your CentOS 8 or Ubuntu box, but before that let’s check if you pass the following prerequisites for the installation.

  • A user account with sudo privileges to the system.
  • Access to the system terminal.
  • Ensure your system has Hardware Virtualisation enabled in the BIOS configuration.
  • If you pass the above checks, run these command to verify your CentOS or Ubuntu system is ready for Hardware Virtualisation.
$ cat /proc/cpuinfo | egrep "vmx|svm"

You are good to go for the installation if you see vmx(for Intel processors) or svm(for AMD processors) in the output flags.

Run the following commands to install KVM on CentOS. We will be running the commands using the root user however you may run them either with root or a user with sudo privileges.

Switch to the root user using

$ sudo su
$ dnf update
$ dnf module install virt

Install KVM management and vm provisioning tools:

$ dnf install virt-install virt-viewer libguestfs-tools
  • virt-install: A command line tool for creating new KVM, Xen or Linux container guests using the “libvirt” hypervisor management library.
  • virt-viewer: An application with a desktop user interface for managing virtual machines through libvirt.
  • libguestfs-tools: A set of tools for accessing and modifying virtual machine disk images.

Now let’s start the libvirtd service and enable it to start up on every boot. View its status to check if it is running.

$ systemctl start libvirtd
$ systemctl enable libvirtd
$ systemctl status libvirtd

Finally let’s check if kvm module is loaded. Run this command to verify it.

$ lsmod | grep kvm 
Configure Bridged Network Interface

The libvirt service by default creates a virtual network interface called virbr0 which works as a NAT adapter. Hence your VM will not be visible to the other machines in the host KVM’s network and they won’t be able to connect to it. We can overcome this by creating a network bridge so that your VM becomes accessible to your host’s network. Follow these steps to achieve this:

First list out the network interfaces attached to your KVM box. We will create a bridged network for one of these.

$ nmcli con show

Before we proceed you will need to have the following information to setup the new bridge interface.

  1. Bridge Name: This will be the name of the new bridge interface. We will use br0.
  2. Interface Name: The name of the network interface which will serve as the bridge slave. This should one of the interface from the output of the above command.
  3. IP Address: IP address for the bridge interface.
  4. Subnet Mask: The subnet mask for the network interface you have chosen.
  5. Gateway IP: The IP of the gateway for the network.
  6. DNS Addresses: The IPs of the DNS servers you wish to use.

You can get this information from your network interface’s configuration file located at /etc/sysconfig/network-scripts/ifcfg-<INTERFACE_NAME>

Now that we have everything we need ready, let’s create the bridge interface.

First add a new connection for the bridge in the network configuration.

$ nmcli con add ifname br0 type bridge con-name br0

Now set the IP, Gateway and DNS for br0.

$ nmcli con modify br0 ipv4.addresses IP/SUBNET_MASK ipv4.method manual
$ nmcli con modify br0 ipv4.gateway GATEWAY
$ nmcli con modify br0 ipv4.dns DNS_IP

Now set the network interface you have chosen as the slave for the bridge.

$ nmcli con add type bridge-slave ifname INTERFACE_NAME master br0

Finally bring the interface down and the bridge up.

$ nmcli con down INTERFACE_NAME
$ nmcli con up br0

NOTE: The interface has to be brought down first to bring the bridge br0 up. If you are accessing your KVM machine remotely this may cause you to lose connectivity to the machine after you bring down the interface, as the interface you were using to connect to the machine was brought down. This can avoided by running the above commands through a script like this:


nmcli con down INTERFACE_NAME
nmcli con up br0

You would momentarily lose connectivity to the machine when the interface goes down but it would re-established after the bridge comes up.

$ grep -Eoc '(vmx|svm)' /proc/cpuinfo

This command outputs the number of cpu cores. If the output is great than 0 then your hardware supports hardware virtualisation otherwise not. Now, we will verify if your system supports kvm. Install the cpu-checker package which includes the kvm-ok command which verifies the same.

$ apt install cpu-checker
$ kvm-ok

If virtualisation is enabled in the BIOS you will get following output:

INFO: /dev/kvm exists
KVM acceleration can be used

If everything worked out for you till now, we can proceed ahead with kvm installation.

$ apt install qemu-kvm libvirt-daemon-system libvirt-clients bridge-utils virt-manager -y

The libvirt daemon is started after its installation, check its status to ensure its running.

$ systemctl is-active libvirtd


Only the members of the libvirt and kvm groups can create and manage virtual machines. Add your username to these groups using the usermod command. Replace the <USER> placeholder with your username.

$ usermod -aG libvirt <USER>
$ usermod -aG kvm <USER>

How to Create Virtual Machines on KVM

You can create kvm virtual machines using the GUI or the command line. Jump to your preferred way from here.

Creating VM using GUI

The virt-manager package provides a simple GUI interface to create and manage virtual machines on a local or remote kvm machine through libvirt. These are some of the handy feature of this tool:

  • Create and manage virtual machines.
  • View VM’s live performance and resource utilisation statistics.
  • Manage VMs on a remote kvm machine.
  • Configure and adjust a VM’s resource allocation and virtual hardware.

We have already installed virt-manager along with kvm, so let’s go ahead and create a virtual machine.

  1. Launch the Virutal Machine Manager application and go to File -> New Virtual Machine.
Virt Manager Create VM

2. Select the Local install media option in the dialog box that appears and click on Forward.

Virt Manager Local Install Media

3. Choose the ISO in the next dialog and click on Forward.

Virt Manager Select ISO

4. Enter the RAM and CPU details you want to allocate to your virtual machine and click on Forward.

Virt Manager Allocate RAM and CPUs

5. Allocate the hard disk space for the VM in the next dialog and click on Forward.

Virt Manager Allocate Disk

6. Finally provide a name to your VM and click on Finish.

Virt Manager Name VM

The VM will start automatically and boot from the ISO. Follow the installation instructions for the OS.

Creating VM using Command Line

The virt-install command line tool is provided by the virt-manager package to create a virtual machine using the command line and install operating system on it. The virt-install command provides a number of options to provision virtual machines but these are the bare minimum parameters that are needed.

--nameThe name of the virtual machine.
--memoryThe amount of RAM to allocate to the virtual machine in MBs.
--diskThe path for the virtual machine’s disk file. If the disk image does not exist, it gets created automatically.
--vcpusThe number of virtual cpus to allocate for the VM.
--cdromThe path of the OS ISO file.
--graphicsThe graphical console type like vnc, spice, sdl. Default is vnc if no option is specified.

This is how we can create a RHEL 8 vm using the virt-install command.

$ virt-install \ 
  --name rhel8 \ 
  --memory 2048 \ 
  --vcpus 2 \ 
  --disk size=8 \ 
  --cdrom /path/to/rhel8.iso \ 
  --os-variant rhel8 

How to Migrate a Virtual Machine to Another KVM

A major advantage of using virtual machines is that they are portable. KVM also supports live migration for the virtual machines i.e you can migrate them even when they are running. You can try migrating a VM using the virsh command. You will need to have access to the target kvm.

$ virsh migrate --live rhel8 qemu+ssh://<TARGET_KVM_HOSTNAME>/system

Qemu-img Commands

qemu-img is an important and very helpful tool provided by Qemu. This utility helps in viewing the info for your disk images and in manipulating them. Let’s look at some of the qemu-img commands that would come in handy for you.

1. Create disk image
$ qemu-img create -f <FORMAT> <DISK_FILE>
2. View disk image info
$ qemu-img info <DISK_FILE>
3. Change the backing disk file
$ qemu-img rebase -f qcow2 -b <NEW_BACKING_FILE> <DISK_FILE>
4. View the full backing file chain

If your disk image has a very long backing file chain. You can view the entire chain as follows:

$ qemu-img info --backing-chain <DISK_FILE>
5. Merge the disk image and its backing disk file
$ qemu-img commit <DISK_FILE>

The commit command merges the top most disk image on to its backing file and the top disk file can be removed after this.

6. Convert raw image to qcow2
$ qemu-img convert -f raw -O qcow2 <INPUT_DISK_FILE> <OUTPUT_DISK_FILE>
7. Resize image
$ qemu-img resize disk.qcow2 [+|-]<SIZE_IN_GB>

To shrink the disk image size, you will have to first use the file system and partitioning tools to reduce the partition sizes of the disk, otherwise you will end up losing your data. Use the partitioning tools after increasing the disk size to grow the partition sizes.

8. Disk image snapshot operations
List snapshot
$ qemu-img snapshot -l <DISK_FILE>

Create snapshot
$ qemu-img snapshot -c <SNAPSHOT_NAME> <DISK_FILE>

Restore snapshot
$ qemu-img snapshot -a <SNAPSHOT_NAME> <DISK_FILE>

Delete snapshot
$ qemu-img snapshot -d <SNAPSHOT_NAME> <DISK_FILE>


In this tutorial you learned:

  • How to install KVM on a CentOS or Ubuntu machine
  • How to create virtual machines on KVM using the GUI or the command line
  • How to migrate virtual machines from one host to another
  • How to view info about your disk files and how to manipulate them

The migration command and qemu-img are quite handy tools and often come in use. I would highly recommend you learn them. If you any doubts or if I missed something ,feel free to drop a comment below.

Leave a Reply

Your email address will not be published.

Previous Post
Streams in Flutter

Streams in Flutter, Simplified

Next Post
StreamBuilder in Flutter

Complete guide to StreamBuilder in Flutter