How to Install and Use KVM in Ubuntu

KVM Virtualization

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 we need processors with hardware virtualization extensions, such as AMD-V or Intel-VT and as it was originally designed for x86 processors, so having x86 processors is quite preferable.

This article will guide you to install and configure KVM on Ubuntu 20.04 and I’ll show you how to use Virtmanager to create a virtual machine.

Inspect Virtualization Compatibility

Before jumping into the process, we need to perform some compatibility tests to run KVM. Execute the command given below to test the CPU compatibility.

$ egrep -c '(vmx|svm)' /proc/cpuinfo

The above command will check if the hardware virtualization extensions such as AMD-V or Intel-VT are available or not. Once the command is executed you will get either 0 or more. If you get more than 0 your system supports virtualization.

Check if CPU supports KVM virtualization

Now, execute the following command to check KVM acceleration support.

$ sudo kvm-ok

If the kvm-ok command is not present, execute the following apt command to install it.

$ sudo apt install cpu-checker

Then, again execute the kvm-ok command to KVM support.

kvm-ok command

Installing KVM on Ubuntu 20.04

Once the compatibility test is completed and the response is positive you can install the KVM and its related utility package. To run installation execute the following command.

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

In the above apt command, the qemu-kvm package is the actual package for KVM, the qemu package helps in hardware virtualization, the libvirt-clients is used to manage and access the virtual machine where the libvirtd-daemon is the virtualization daemon, the virt-manager manages virtual machines through a graphical user interface, and lastly, the permit other hosts to access a virtual machine through bridge connection.

Now, once the installation is completed check if the libvirtd-daemon is running so, check the status using the systemctl command.

$ sudo systemctl status libvirtd

libvirtd service

You can verify if KVM modules are loaded using the following command.

$ lsmod | grep -i kvm

KVM Kernel module

Network Bridge

When installing a KVM network bridge called virbr0 is created automatically. This network setup is maybe suitable inside the local network but guests cannot be accessed from outside its local network. You need to configure manually to be able to access from outside the local network boundaries. Execute the following command to check the network bridge of your local system

$ brctl show

KVM Network Bridge

Understanding and Using KVM

You can open the KVM using the virt-manager utility or GUI-based Application manager. Now, execute the following command to kick on the virtual machine.

$ sudo virt-manager

You can see the following user interface of the KVM once the command is executed.

Virtual Machine Manager

Now, it is possible to add a new virtual machine by clicking on the top left corner monitor button. Then, you will see the pop dialog to choose the method to install the operating system. Choose the method you prefer and follow up the procedure to set up a virtual machine.

New VM

Once you finish up creating the virtual machine you can see the following type of virtual machine listed from where you can power up the virtual machine.

New Debian VM

In kvm you can manage and control virtual machine hardware configuration such as memory, disk space, etc to the max your actual system has integrated. This is the key feature of the KVM which lets you configure virtual machine hardware according to your need. To configure simply double click on the virtual machine or open virtual machine then, click on the blue icon button. As you can see there are several hardware component configurations from where you can reconfigure your VM hardware.

Memory Allocation

Conclusion

The virtual machine helps in setting up the multiple systems with separate OS from which can use a different distro of the Operating system virtually. Now, after reading this article you might have a clear concept of how we can install and set up the KVM.

Published
Categorized as Ubuntu