What is KVM

KVM, or Kernel-Based Virtual Machine, provides a virtual environment in which one or more additional operating systems can run alongside the primary system without affecting its functionality. In KVM, the Linux kernel makes use of virtualization technology by leveraging your existing system and hardware resources. The new virtual environment created by KVM can be used to provide additional workspace for software testers, developers, architects and even home users.

This tutorial describes how to install and use a virtual machine with the KVM open source virtualization platform. The commands and procedures described in this tutorial were run on Ubuntu 20.04 and Ubuntu 18.04 systems on an x86_64 (64bit x86 Intel or AMD processor).

The KVM Installation Procedure

The installation of KVM is done in 4 simple steps.

Step 1: Verifying Hardware Support

Before installing KVM, it is important to verify if your CPU supports virtualization and if the virtualization technology has been activated on your CPU.

Please run the following command on your Ubuntu Terminal. You can open the terminal by pressing Ctrl Alt T or through the system Dash.

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

How to Install KVM and Manage Virtual Machines in Ubuntu 20.04 linux ubuntu

The output of this command is very important. An output of 1 or anything greater than that indicates that your CPU can be set-up for using the virtualization technology. An output of 0 indicates the inability of your system to run KVM. In my case, the output is 4 which verifies that I can install and run virtual machine(s).

The next thing to do is to check if the virtualization technology is enabled on your system or not as it is very important to run KVM. Use the following command in order to do so:

$ sudo kvm-ok

OutPut 1:

How to Install KVM and Manage Virtual Machines in Ubuntu 20.04 linux ubuntu

This output indicates that your CPU has not been configured to run virtualization. The procedure to enable virtualization is to first enter the BIOS setup at system boot, edit system configuration to activate virtual technology and then hard switch off and on your system.

Now when you switch on your system and run the kvm-ok command, the following output will be displayed:

OutPut 2:

The following output indicates that your system has virtualization enabled and this can be used for KVM acceleration.:

How to Install KVM and Manage Virtual Machines in Ubuntu 20.04 linux ubuntu

Step 2: Installing the KVM Package

Now that you have verified the prerequisites for installing KVM on your system, use the following command to install KVM:

$ sudo apt-get install qemu-kvm libvirt-bin bridge-utils virt-manager

How to Install KVM and Manage Virtual Machines in Ubuntu 20.04 linux ubuntu

You will be prompted to provide a password as only a superuser can install a package on Ubuntu. The installation process will begin; during which you will be prompted with a yes or no option. Please enter y to continue installation. We are also installing the virtual manager package with this installation; it is a graphical tool for managing your virtual machines.

Step 3: Adding Your User Account to KVM

The next step after the installation is to add your user account as a KVM user. It is important to note that you can use virtual machines on KVM only if you are a root user or if you are part of the libvirt/libvirtd group. Please use the following command to add yourself to the libvirtd group:

$ sudo adduser [username] libvirtd

Output 1:

The [username] has been added to the libvirtd group.

This is the output for slightly older versions of KVM that had a default group called libvirtd.

Output 2:

The same command might give the following output on your system:

How to Install KVM and Manage Virtual Machines in Ubuntu 20.04 linux ubuntu

This indicates that you are using a new version of KVM that has a default group libvirt serving the same purpose as that of the libvirtd group. The following output of the groups command will indicate that the current root user is already a member of the libvirt group. Therefore there is no need to add yourself to this group.

How to Install KVM and Manage Virtual Machines in Ubuntu 20.04 linux ubuntu

For adding other users to this group use the following command:

$ sudo adduser [username] libvirt

This new user will have access and management privileges on KVM.

Step 4: Verifying Successful Installation

Logout and login back to your system and run the following command:

$ virsh -c qemu:///system list

The following output indicates that everything is running properly on your system.

How to Install KVM and Manage Virtual Machines in Ubuntu 20.04 linux ubuntu

Create a New VM in KVM

After the successful installation of KVM, you can start working with it right away. Let us see how to access the graphical Virtual Machine Manager on your Ubuntu.

You can access the VM Manager through the Ubuntu Dash as follows:

How to Install KVM and Manage Virtual Machines in Ubuntu 20.04 linux ubuntu

OR by accessing it through the Ubuntu Application list.

Now lets us create a sample virtual machine through the manager. In this tutorial, I will be creating a new VM for an additional Ubuntu OS that I want to use along with the current primary install of the same already existing on my system. I have downloaded the ISO file to my system through the internet.

Please follow these steps:

The following image shows the environment of the VM Manager:

How to Install KVM and Manage Virtual Machines in Ubuntu 20.04 linux ubuntu

You can create a new virtual machine through the File Menu, Create a new virtual Machine icon on the toolbar, or through the right-click menu at QEMU/KVM entry under the ‘Name’ column.

The following New VM dialog will open:

How to Install KVM and Manage Virtual Machines in Ubuntu 20.04 linux ubuntu

Please note that you may also get the following dialog displayed if you have not activated virtualization for your CPU. If that’s the case, refer to Step 1 of the Installation Procedure to get rid of this problem.

How to Install KVM and Manage Virtual Machines in Ubuntu 20.04 linux ubuntu Now select your installation option from the four available options and click the Forward button. I am selecting the Local install Media option as the ISO file is already downloaded on my system.

The following dialog will appear:

How to Install KVM and Manage Virtual Machines in Ubuntu 20.04 linux ubuntu

The above dialog lets you choose the location for installing media. You can wish to install it through a CD ROM or DVD or, like me, use an ISO image of the installation package.

Click Forward; the following dialog will appear:

How to Install KVM and Manage Virtual Machines in Ubuntu 20.04 linux ubuntu

In this step, you can select the RAM for the new VM. For 32 bit systems, you can reserve a RAM only up to 2GM. For 64 bit systems, you can even choose more than 2 GB from the memory available on the host computer. Through this dialog, you can also select the CPU number from the available processors of your system.

Specify your choice and then click the Forward button; the following dialog will appear:

How to Install KVM and Manage Virtual Machines in Ubuntu 20.04 linux ubuntu

Through this dialog, you can enable storage for the VM you are creating.

Click the Forward button after enabling storage; the following dialog will appear:

How to Install KVM and Manage Virtual Machines in Ubuntu 20.04 linux ubuntu

This is the last step before the installation of your new VM begins. Here you can specify the name of the new VM and make a few other configurations.

Click the Finish button. The installation of your new VM will begin depending on your choices and the installation options you provided.

When the installation is complete, your VM will open in a new console. You can install your specified OS through this window like you would on a physical system.

How to Install KVM and Manage Virtual Machines in Ubuntu 20.04 linux ubuntu

I will install the Ubuntu 18.04 package on my new VM and start using it as I would on a normal machine without affecting my original OS.

Manage KVM Virtual Machines

Through the respective VM console

When you create a new VM, it opens in a running state. You can perform the following management operations on your VM through the new VM console:

How to Install KVM and Manage Virtual Machines in Ubuntu 20.04 linux ubuntu

Power on a VM

You can power on your VM by clicking the Start Button located on the toolbar.

Pause a VM

You can pause a VM by clicking the Pause Button located on the toolbar. This will only pause the VM, not power it off.

Shutdown a VM

In order to power off a VM, like you would power off an operating system on your physical machine, click the Shutdown button from the toolbar.

Snapshots

A snapshot is the current state of a system at a given time. This includes information about the memory, disk and device state. You can take a snapshot of your virtual machine in a clean state before running a potentially fatal procedure. It will help you restore the system to its clean state in case a problem occurs in the future. You can take and manage snapshots of your virtual machine through the Manage Snapshot icon located on the toolbar. When you take a snapshot, it is assigned a unique ID so that it can be accessed later through that ID.

Clone a VM

You can clone an existing virtual machine with the same OS and configuration details by selecting the Clone option from the Virtual Machine menu. You can edit some of the configurations of the new clone, for example, the Network Configuration. The use of this new clone is that since it is based on a previous VM, its snapshots can be especially helpful for the original VM. Clones are also helpful in creating and managing a clustered network of VMs of the same type.

Migrate a VM

When you migrate a VM, it can successfully be run on the destination system. In order to migrate your VM, select the Migrate option from the Virtual Machine menu.

Take Screenshot

You can also take the screenshot of your VM environment simply by selecting Take Screenshot option from the Virtual Machine menu.

Redirect USB

You can let the virtual operating system access and use a USB device by selecting Redirect USB device option from the Virtual Machine menu.

  • Send Key

By sending hot-key combinations to your destination computer, you can use those keys on the VM without them having an impact on your host machine for a limited time. Virtual Manager comes with a set of such hotkeys that you can use exclusively on your destination computer for some time through the Send Key menu.

View and Configure Hardware Details

You can view and configure the system and hardware details of your virtual machine by clicking the information (i shaped)icon from the toolbar or by selecting the Details option from the View menu.

Through the Virtual Machine Manager

The Virtual Machine Manager allows you to manage the following options for your KVM connection and the new VM:

Monitor CPU Usage

You can monitor the CPU usage of your guest VM and also the host machine through the Graph option in the View Menu.

How to Install KVM and Manage Virtual Machines in Ubuntu 20.04 linux ubuntu

Edit Connection Details

Through the Connection Details option in the Edit menu, you can manage and configure the Virtual Network, Storage and Network Interfaces of the KVM connection.

Delete a VM

You can delete a VM through the Delete option in the Edit menu of the graphical VM Manager.

Manage Preferences

Other Preferences for the connection can be managed through the Preferences option in the Edit Menu.

This article should help you with a thorough understanding of the virtual machine environment provided by the KVM. You have learned the concept of KVM, how to install a virtual machine along with a new operating system on it. You also got a getting started experience of running and managing Virtual Machines on your destination computer. The article introduced you to the Virtual Manager environment where you can configure connection and hardware settings for your new VM. Now you do not need new hardware to run an additional operating system that you may want to use temporarily for software testing, deployment, and designing purposes or simply because you need one as a home user.