In this article we will cover the setup process of KVM on various Linux distributions like Debian, Ubuntu, CentOS, AlmaLinux, Rocky Linux, Arch Linux, openSUSE/SUSE, and RHEL.
This content is an excerpt from Mastering KVM Virtualization – The Ultimate eBook. To dive deeper into KVM and elevate your virtualization skills, get the full eBook here.
Before doing any package installation, it is important to check if your CPU supports hardware virtualization. This is done by executing the following commands in your terminal.
grep -E --color 'vmx|svm' /proc/cpuinfo
Analyze the output if it contains “vmx” or “svm,” which will mean your CPU supports hardware virtualization.
You can as well run the commands separately:
### For Intel-based processors ###
grep -E --color 'vmx' /proc/cpuinfo
### For AMD-based processors ###
grep -E --color 'svm' /proc/cpuinfo
You can also run the following commands to check if your system supports hardware virtualization.
grep -Ec '(vmx|svm)' /proc/cpuinfo
If the output provided has a number greater than zero, it means CPU supports hardware virtualization.
Installation of KVM on Debian / Ubuntu
Begin by ensuring the system packages are up-to-date:
sudo apt update && sudo apt upgrade -y
If a reboot is required after the packages are upgraded, consider doing it.
[ -f /var/run/reboot-required ] && sudo reboot -f
Install KVM and all necessary packages required to convert your Debian or Ubuntu system into a complete hypervisor.
sudo apt install libvirt-daemon qemu-kvm libvirt-daemon-system virtinst libosinfo-bin \
libguestfs-tools bridge-utils genisoimage
Explanation of the packages installed:
libvirt-daemon
– A package that provides the libvirtd daemon, which provides an API for interacting with VMs.qemu-kvm
– A metapackage that provides QEMU with KVM support. Qemu is used in the creation and the management of virtual machines, and it provides necessary emulation and virtualization capabilities.libvirt-daemon-system
– It includes the system service files and default configurations for the libvirtd daemon service.virtinst
– This package provides the command-line utilities used to provision and manage virtual machines. It includevirt-install
andvirt-clone
command-line tools which facilitates VM creation and management automations.libosinfo-bin
– It provides the command-line tools required for the managment of operating system information. Command line tools likeosinfo-query
,osinfo-install-script
, andvirt-customize
are provided by this package.libguestfs-tools
– This provides a set CLI tools that allows you to access and modify virtual machine disk images. Some of these tools arevirt-builder
,virt-cat
,virt-ls
,virt-edit
,virt-df
, andguestmount
.bridge-utils
– Provide the utilities for managing and configuring network bridges. The main command-line tool in this package isbrctl
,
The installed packages collectively empower system administrators to create, configure, and manage virtual machines on KVM.
After installations, run the following command to check of KVM kernel modules are loaded.
$ sudo lsmod | grep kvm
kvm_intel 344064 8
kvm 958464 1 kvm_intel
irqbypass 16384 19 kvm
If your Linux OS has a graphical user interface (Desktop Environment), you can install
sudo apt install qemu-system virt-manager
Where:
qemu-system
is a package with full system emulation binaries for QEMUvirt-manager
(Virtual Machine Manager) is a graphical user interface tool that enables you to manage virtual machines through libvirt from an intuitive interface.
If you encounter an error message like below on Ubuntu:
Error: error creating libvirt domain: internal error: process exited while connecting to monitor: qemu-system-x86_64: -blockdev {"driver":"file","filename":"/var/lib/libvirt/images/flatcar-base","node-name":"libvirt-2-storage","auto-read-only":true,"discard":"unmap"}: Could not open '/var/lib/libvirt/images/flatcar-base': Permission denied
The solution is disable QEMU default security driver.
$ sudo nano /etc/libvirt/qemu.conf
security_driver = "none"
The libvirtd
service needs tp be restarted after the change:
sudo systemctl restart libvirtd
Install KVM on Rocky / AlmaLinux / CentOS Stream / Fedora
KVM can be installed on Rocky Linux, AlmaLinux, CentOS Stream, or Fedora operating systems using the steps provided in this section. All these distributions mentioned are similar and the same process apply when setting up KVM.
Start with the updating of system packages
sudo dnf update -y
Thereafter install KVM and the related packages for turning your OS into a hypervisor.
sudo dnf install -y libvirt qemu-kvm virt-install \
virt-top libguestfs-tools guestfs-tools genisoimage
In these distros, bridge-utils
package is contained in the epel
repository.
sudo dnf -y install epel-release
sudo dnf -y install bridge-utils
If you have a Desktop Environment, you can install virt-manager
sudo dnf -y install virt-viewer virt-manager
Start and enable the libvirtd
daemon to start at system boot.
sudo systemctl enable --now libvirtd
Confirm service status by running the command:
systemctl status libvirtd
Installation of KVM on openSUSE / SUSE
In this section you will learn how to install KVM hypervisor on openSUSE and SUSE Linux distributions.
We always recommend you work on a system that is up to date:
sudo zypper refresh
sudo zypper -n update
To install all KVM tools and utilities run the commands below in your terminal.
sudo zypper -n install libvirt qemu-kvm libvirt-daemon \
libvirt-daemon-qemu virt-install guestfs-tools virt-top \
bridge-utils libguestfs
If you have Desktop Environment in your system install virt-manager
package as well.
sudo zypper -n install virt-manager
After the installation start and enable libvirtd
service.
sudo systemctl start libvirtd
sudo systemctl enable libvirtd
Installation of KVM on RHEL
Login to your RHEL server and make sure it’s registered
sudo subscription-manager register --auto-attach
Provide your redhat account details:
Registering to: subscription.rhsm.redhat.com:443/subscription
Username: <INPUT-USERNAME>
Password: <ENTER-PASSWORD>
Install packages that together work to make your RHEL server a functional virtualization platform.
sudo dnf install -y libvirt qemu-kvm virt-install virt-top libguestfs-tools
Users running the Desktop edition of RHEL can install virt-manager
and virt-viewer
GUI packages.
sudo dnf -y install virt-manager virt-viewer
The bridge-utils
package is installed from EPEL repository. Add using the commands below.
# RHEL 9
sudo subscription-manager repos --enable codeready-builder-for-rhel-9-$(arch)-rpms
sudo dnf install https://dl.fedoraproject.org/pub/epel/epel-release-latest-9.noarch.rpm
sudo dnf -y install bridge-utils
# RHEL 8
sudo subscription-manager repos --enable codeready-builder-for-rhel-8-$(arch)-rpms
sudo dnf install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm
sudo dnf -y install bridge-utils
Start the service and set it to start automatically with system boot.
sudo systemctl enable --now libvirtd
To chek if it’ running we run:
systemctl status libvirtd
Installation of KVM on Arch-based systems
Arch-based Linux distributions are known for their simplicity, flexibility, and user-centric approach. The following are major distributions based on Arch Linux:
- Manjaro
- EndeavourOS
- ArcoLinux
- Garuda Linux
- RebornOS
- ArchBang
- Artix Linux
Follow the steps provided to have KVM installed on any Arch based Linux distribution.
Step 1: Ensure the default GPG keys are populated
sudo pacman-key --init
sudo pacman-key --populate archlinux
Step 2: Update system
sudo pacman -Sy archlinux-keyring
sudo pacman -Syyu
Next we install KVM and all other tools required.
sudo pacman -S qemu libvirt virt-manager virt-viewer dnsmasq ebtables bridge-utils libguestfs guestfs-tools
Ensure the kernel modules are automatically loaded
lsmod | grep kvm
Enable and start libvirtd service
sudo systemctl enable --now libvirtd
To check service status use the command
systemctl status libvirtd
Verify Virtualization Host Readiness
To verify that your system is prepared to be a virtualization host, run the command below:
$ sudo virt-host-validate
QEMU: Checking for hardware virtualization : PASS
QEMU: Checking if device /dev/kvm exists : PASS
QEMU: Checking if device /dev/kvm is accessible : PASS
QEMU: Checking if device /dev/vhost-net exists : PASS
QEMU: Checking if device /dev/net/tun exists : PASS
QEMU: Checking for cgroup 'cpu' controller support : PASS
QEMU: Checking for cgroup 'cpuacct' controller support : PASS
QEMU: Checking for cgroup 'cpuset' controller support : PASS
QEMU: Checking for cgroup 'memory' controller support : PASS
QEMU: Checking for cgroup 'devices' controller support : PASS
QEMU: Checking for cgroup 'blkio' controller support : PASS
QEMU: Checking for device assignment IOMMU support : PASS
QEMU: Checking if IOMMU is enabled by kernel : PASS
QEMU: Checking for secure guest support : WARN
For aFAIL
value from any of the checks, refer to the displayed instructions for how to fix the problem.
If virt-host-validate
generates generates the following output then your host CPU doesn’t support KVM virtualization:
QEMU: Checking for hardware virtualization: FAIL (Only emulated CPUs are available, performance will be significantly limited)