How to Install KVM on Rocky Linux 9 / AlmaLinux 9

This tutorial will walk you through how to install KVM on Rocky Linux 9 or AlmaLinux 9.

Kernel Virtual Machine (KVM) is an open-source Type1/bare-metal hypervisor that enables users to host and run multiple isolated virtual environments on their Linux machine.

Like most virtualization solutions, KVM abstracts hardware resources, including CPU, memory, storage, network, and graphics, and assigns them to guest machines that run in an isolated environment.

KVM provides numerous functionalities such as resource control, scheduling, scalability, high performance, low latency, live migration, memory management, and more. In addition, you can use Ansible and other automation tools to automate KVM deployments.

Prerequisites

  • An active and running Rocky Linux 9 / AlmaLinux 9 system.
  • Sudo or root privilege
  • Internet Connection
  • Basic understanding of Linux networking and commands.

Without any delay, let’s deep dive into KVM installations steps.

1. Confirm Hardware Virtualization

Ensure that your system has hardware virtualization extensions enabled. For Intel-based hosts, Use the following command to confirm if the CPU virtualization extension (vmx) is available:

$ sudo grep -e 'vmx' /proc/cpuinfo

For AMD-based hosts, confirm if the CPU virtualization extension (svm) is available by running the following command:

$ sudo grep -e 'svm' /proc/cpuinfo

If virtualization is not enabled, you can enable it in the BIOS settings of your machine.

2. Install KVM on Rocky Linux 9 / AlmaLinux 9

Run the following command to install the KVM packages, which are located in the default repository of Rocky Linux 9:

$  sudo dnf install qemu-kvm libvirt virt-manager virt-install

Install-KVM-on-rockylinux9-almalinux9

Install other KVM management tools as shown:

$ sudo dnf install epel-release -y

Enable-EPEL-Repository-RockyLinux9-AlmaLinux9

$ sudo dnf -y install bridge-utils virt-top libguestfs-tools bridge-utils virt-viewer

Install-bridge-utils-virt-viewer-rockylinux9

The following is a brief explanation of the above packages:

  • virt-manager provides graphical user interface for managing virtual machines.
  • libvirt-client offers CL utility to administer the virtual environment.
  • virt-install is the command line tool used to create virtual machines.
  • libvirt provides the host-side libraries for interacting with host systems and hypervisors.

After the installation is complete, run the following command check whether KVM module is loaded into the kernel or not.

$ sudo lsmod | grep kvm

3. Start and Enable libvirtd daemon

libvirtd is a daemon component that operates on the server side and controls tasks on virtualized guests. It is employed in managing virtualization technologies, including ESXi, KVM, and Xen.

To start and enable the libvirtd daemon, run:

$ sudo systemctl start libvirtd
$ sudo systemctl enable libvirtd

To check if the libvirtd daemon is running, run the following command:

$ sudo systemctl status libvirtd

Libvirtd-Service-Status-RockyLinux9-AlmaLinux9

You must also add your system user to the KVM group to execute virt-install commands without sudo.

$ sudo usermod -aG libvirt $USER
$ newgrp libvirt

4. Create Network Bridge for KVM Instances

A network bridge with the name virbr0 is automatically created to offer Network Address Translation (NAT). Virtual machines using this bridge lack external connectivity.

Existing bridge networks can be listed using the brctl command:

$ sudo brctl show

Default-virbr0-KVM-RockyLinux9-AlmaLinux9

In this section, we’ll create a network bridge for external connections using NMCLI. To get started, run the following command to list the network interfaces available on your machine:

$ sudo nmcli connection show

Nmcli-Connection-Show-Command-Output-RockyLinux9-AlmaLinux9

To start creating the bridge, first, delete the existing connection using the following command:

$ sudo nmcli connection delete bf2bae34-5130-309a-b84d-a3c56dec9ed8

Before moving on, it would be important to have the following information at hand:

  • Bridge name: This is the new bridge’s preferred name (e.g., br1).
  • Device name: This is the network interface’s name. This is going to be the bridge slave (e.g., ens160).
  • IP address/subnet: This is the bridge network’s IP address and subnet, for example, 192.168.122.1/24. This should match your network subnet IP address.
  • gateway:  This is the network’s default gateway address, for example, 192.168.16.2
  • DNS1 and DNS2: These are the preferred DNS addresses (e.g., 8.8.8.8 and 8.8.4.4).

To create a new bridge, run the following command:

$ sudo nmcli connection add type bridge autoconnect yes con-name br1 ifname br1

Create-Network-Bridge-Br1-KVM-RockyLinux9

Next, add the IP, gateway, and DNS to the bridge, as shown below:

$ sudo nmcli connection modify br1 ipv4.addresses 192.168.16.122.1/24 ipv4.method manual
$ sudo nmcli connection modify br1 ipv4.gateway 192.168.16.2
$ sudo nmcli connection modify br1 ipv4.dns 8.8.8.8 +ipv4.dns 8.8.4.4

Assign-IP-Address-KVM-Bridge-Br1-RockyLinux9-AlmaLinux9

Now, run the following command to add the bridge slave:

$ sudo nmcli connection add type bridge-slave autoconnect yes con-name ens160 ifname ens160 master br1

Add-Interface-to-KVM-Bridge-RockyLinux9-AlmaLinux9

To verify the bridge creation, run the following command:

$ sudo nmcli connection show

Nmcli-connection-after-creating-bridge-rockylinux9-almalinux9

Next, start the network bridge:

$ sudo nmcli connection up br1

Start-KVM-Network-Bridge-Br1-RockyLinux9-AlmaLinux9

To verify if the bridge is active, run:

$ sudo nmcli connection show br1

KVM-Network-Bridge-Details-RockyLinux9-AlmaLinux9

To enable KVM to use this bridge, edit the below file,

$ sudo vim /etc/qemu-kvm/bridge.conf

Add the line:

Qemu-KVM-Bridge-Conf-RockyLinux9-AlmaLinux9

Then restart libvirtd service

$ sudo systemctl restart libvirtd

5. Creating Virtual Machine using KVM

Now that KVM is set up and the bridge connection is established, let’s create a virtual machine. You need an ISO file to continue with the VM creation.

It’s easy to spin a virtual machine via the CLI, especially if you understand the fundamentals of KVM. To get started, set the right ownership of the libvirt directory:

$ sudo chown -R $USER:libvirt /var/lib/libvirt/

Using the following syntax, we will create a virtual machine on the command line using the Rocky9 Linux image.

$ virt-install \
--name Rocky9 \
--ram 2048 \
--vcpus 1 \
--disk path=/var/lib/libvirt/images/rocky-9.img,size=20 \
--os-variant centos-stream9 \
--network bridge=br1,model=virtio \
--graphics vnc,listen=0.0.0.0 \
--console pty,target_type=serial \
--location /home/rocky9/Downloads/Rocky-9.0-x86_64-minimal.iso

Note:

  • -disk path=/var/lib/libvirt/images/rocky-9.img,size=20  – It is  the path to create the disk and disk size in GBs.
  • -vcpus 1 is the number of CPUs to be allocated.
  • -ram 2048 is the memory allocated.
  • network bridge=br1 specifies the network bridge to use.
  • graphics vnc,listen=0.0.0.0 shows the VNC listen address.
  • location /home/rocky9/Downloads/Rocky-9.0-x86_64-minimal.iso is the path of your ISO file.

When the above command is executed, VNC will launch and the guest operating system installation will begin, as shown below.

Virt-Install-VM-Installation-screen-RockyLinux9-AlmaLinux9

You can also create virtual machines using Virt-Manager GUI. To get started, head over to the application menu and click the icon as shown:

Search-Virtual-Machine-Manager-RockyLinux9-AlmaLinux9

Click on the highlighted icon to create a virtual machine.

Click-on-Virtual-Machine-Icon-VirtManager-RockyLinux9-AlmaLinux9

Here, select how you would like to install the ISO, choose the default option, and proceed.

Choose-Local-Install-Media-VirtManager-RockyLinux9

In this window, select the ISO image.

Select-ISO-Image-KVM-Virt-Manager-RockyLinux9

Set up the virtual machine’s memory and CPU.

Memory-CPU-For-VM-in-KVM-VirtManager-RockyLinux9-AlmaLinux9

Here, configure the hard disk size.

Disk-Size-For-KVM-VM-VirtManager-RockyLinux9

Now, click the finish button for the installation to begin.

Click-Finish-KVM-VirtManager-RockyLinux9-AlmaLinux9

KVM-VM-Installation-Screen

Conclusion

That’s it! We have shown you how to install KVM on Rocky Linux 9 or AlmaLinux 9. We hope you find this tutorial useful and informative. Feel free to post your queries and feedback in below comments section.

Leave a Comment

two × 1 =