How to Install KVM on CentOS or Rocky Linux

Introduction

KVM (short for Kernel-based Virtual Machine) is a Linux virtualization application that allows users to turn their machine into a hypervisor. It enables multiple virtual machines to run in isolated environments.

In this tutorial, you will learn how to install KVM on Rocky Linux or CentOS.

Install KVM on CentOS or Rocky Linux

Prerequisites

Steps to Install KVM on CentOS or Rocky Linux

The sections below provide step-by-step instructions on how to install KVM. The steps listed in this tutorial work with CentOS and Rocky Linux.

Step 1: Check if Your System Supports Hardware Virtualization

To see if the system is capable of hardware virtualization (HV), run the following:

cat /proc/cpuinfo | egrep "vmx|svm"
cat/proc/cpuinfo | egrep "vmx|svm" terminal output

The egrep command highlights the flags if present. If you find either vmx (Intel processors) or svm (AMD), the system supports hardware virtualization.

Note: If you encounter issues with installing KVM on a system which supports HV, check your BIOS to see if HV is enabled. Refer to the documentation for your host's BIOS to enable HV.

Step 2: Install KVM

Use the following commands to install KVM on CentOS or Rocky Linux:

1. Update the system repositories:

sudo yum update

2. Install virtualization tools:

  • CentOS:
sudo yum install @virt
  • Rocky:
sudo dnf groupinstall "Virtualization Host"

3. Enable the libvirtd service:

sudo systemctl enable --now libvirtd
sudo systemctl enable --now libvirtd terminal output

The command creates several symlinks.

4. Install tools required for KVM management:

sudo yum -y install virt-top libguestfs-tools
sudo yum install virt-top libguestfs-tools terminal output

The command also installs additional dependencies for the two libraries.

Step 3: Verify KVM Installation

To confirm that the KVM module is loaded, run:

lsmod | grep kvm
lsmod | grep kvm terminal output

The output shows a list of loaded KVM modules.

Step 4: Configure Bridge Interface

A bridge interface is necessary for accessing VMs from outside of the hypervisor network. To create a bridge interface:

1. Identify the network interfaces attached to your machine:

sudo nmcli connection show
sudo nmcli connection show terminal output

The output shows all available network interfaces. Note the name of the ethernet interface (in this case, it is enp0s3).

2. Delete the connection by name or UUID:

sudo nmcli connection delete [name_or_UUID]
sudo nmcli connection delete enp0s3 terminal output

The system confirms the deletion.

3. Add a new bridged connection called br0:

sudo nmcli connection add type bridge con-name br0 ifname br0
sudo nmcli connection add br0 terminal output

The command confirms the connection creation.

4. Add a physical interface to the bridge:

sudo nmcli connection add type bridge-slave ifname enp0s3 master br0
sudo nmcli connection add bridge slave terminal output

5. Modify the connection details. To use DHCP for IP address assignment, run:

sudo nmcli connection modify br0 ipv4.method auto

Alternatively, provide the data for a static IP address:

sudo nmcli connection modify br0 ipv4.addresses [IP_address/subnet]
sudo nmcli connection modify br0 ipv4.gateway [gateway]
sudo nmcli connection modify br0 ipv4.dns [DNS]
sudo nmcli connection modify br0 ipv4.method manual

The following information is required:

  • IP_address/subnet. IP address and subnet for the connection (e.g. 192.168.122.1/24).
  • gateway. Default gateway address (e.g. 192.168.122.1).
  • DNS. DNS addresses (e.g. 8.8.8.8 and/or 8.8.4.4).

6. Activate the bridge and interface with:

sudo nmcli connection up br0
sudo nmcli connection up br0

The command brings up the bridged connection and interface. The network takes a few moments to connect.

7. Verify the bridge configuration is functional:

ip addr show br0
ip addr show br0 terminal output

The ip command fetches the IP address for the bridged connection, which is either a static or dynamic IP address.

8. Ping a public server to test bridge connectivity. For example:

ping -c 4 google.com
ping -c 4 google.com

If the packets successfully perform the round trip, the bridged connection is functional.

Create Virtual Machine via Command Line

To create a VM via the command line, download a .iso image for the operating system you want to install and use the virt-install command:

1. Install the utility with:

sudo yum -y install virt-install

2. Adjust the user permissions to allow the hypervisor access to the .iso file:

sudo setfacl -m u:qemu:x [file_location]

Adjust the path in the command to the location of the .iso file.

3. Create a virtual machine. The following example command shows how to install Ubuntu 24.04 on a VM:

sudo virt-install --name=ubuntu --ram=3072 --vcpus=2 --file=/var/lib/libvirt/images/ubuntu.img,size=20 --cdrom=Downloads/ubuntu-24.04.1-desktop-amd64.iso --network bridge=br0
sudo virt-install create vm terminal output

The example uses the following command line arguments to set up a virtual machine:

ArgumentDescription
--name=Custom name of the VM
--ram=Allocated RAM
--vcpu=Number of virtual CPUs
--file=Disk file/image location
--size=Allocated file size of the VM
--cdrom=Installation media
--network bridge=Network used for the VM

Create Virtual Machine via GUI

Use the virt-manager GUI to create a VM with KVM:

1. Install virt-manager with:

sudo yum -y install virt-manager

The program provides a graphical interface for creating and managing VMs.

2. Start virt-manager from the console:

sudo virt-manager

The command opens a GUI interface for the Virtual Machine Manager.

3. Open the File menu and select New Virtual Machine. Alternatively, click the new VM button below the menu.

VM manager New button

The action opens a new window to create a VM.

4. Choose to install the OS from local media and click Forward to proceed.

VM manager new connection KVM ISO

5. Specify the path to the .iso file with the OS.

VM manager new VM choose ISO

The program detects the OS based on the file. Click Forward to proceed.

6. Allocate RAM and CPU resources. The numbers depend on the OS requirements.

VM manager memory and CPU

Click Forward to continue.

7. Specify the virtual hard disk size (in GiB) and proceed to the final step.

VM manager storage

8. Adjust the VM name and review the configuration.

New VM ready to install

Press Finish to set up the virtual machine. The installation process starts immediately after the VM is set up.

Conclusion

This article showed the process of setting up KVM on Rocky Linux and CentOS. It provided instructions for installing KVM, configuring a bridge interface, and setting up virtual machines using the GUI or terminal.

A popular alternative to KVM is VirtualBox. If you are interested, learn how to install VirtualBox on CentOS.

Was this article helpful?
YesNo
Marko Aleksic
Marko Aleksić is a Technical Writer at phoenixNAP. His innate curiosity regarding all things IT, combined with over a decade long background in writing, teaching and working in IT-related fields, led him to technical writing, where he has an opportunity to employ his skills and make technology less daunting to everyone.
Next you should read
How to Install Minikube on CentOS
May 28, 2020

Install Minikube on CentOS and set up a single-node Kubernetes cluster on your local machine...
Read more
CentOS 8 Released: New Features, Changes, & How to Download
October 2, 2019

CentOS is widely popular among developers and system administrators as it offers full control over its highly...
Read more
Containers vs Virtual Machines (VMs): What's the Difference?
January 25, 2024

Both virtual machines and containers are used to created isolated virtual environments for developing and...
Read more
What is a Hypervisor? Types of Hypervisors 1 & 2
September 29, 2022

Server virtualization is one of the hottest topics in the IT world today. It has been around for many years...
Read more