RHEL BasedRocky Linux

How To Install KVM on Rocky Linux 9

Install KVM on Rocky Linux 9

Virtualization has become an essential technology in modern computing environments, offering flexibility, resource optimization, and cost savings. Kernel-based Virtual Machine (KVM) stands out as a powerful open-source virtualization solution for Linux systems. This guide will walk you through the process of installing KVM on Rocky Linux 9, a robust and stable enterprise-grade Linux distribution.

Rocky Linux, as a downstream partner of Red Hat Enterprise Linux (RHEL), provides an excellent platform for hosting virtual machines using KVM. Whether you’re setting up a home lab or deploying a production environment, this tutorial will equip you with the knowledge to harness the full potential of KVM virtualization on Rocky Linux 9.

Prerequisites

Before diving into the installation process, it’s crucial to ensure your system meets the necessary requirements for running KVM effectively on Rocky Linux 9.

Hardware Requirements

  • CPU: Your processor must support hardware virtualization. This feature is known as Intel VT-x for Intel processors and AMD-V for AMD processors.
  • RAM: While KVM itself doesn’t have strict RAM requirements, you’ll need sufficient memory to run both the host system and virtual machines. A minimum of 8GB is recommended, but 16GB or more is ideal for running multiple VMs simultaneously.
  • Storage: SSD storage is preferable for optimal performance, with enough capacity to accommodate the host OS and virtual machine images. Plan for at least 50GB for the host system and additional space based on your VM requirements.

Software Requirements

  • A fresh installation of Rocky Linux 9
  • Root access or sudo privileges on your system
  • An active internet connection for downloading packages

Checking System Compatibility

Before proceeding with the KVM installation, it’s essential to verify that your system supports hardware virtualization.

Verifying Hardware Virtualization Support

To check if your CPU supports virtualization, run the following command:

grep -E '(vmx|svm)' /proc/cpuinfo

If you see output containing “vmx” (for Intel) or “svm” (for AMD), your processor supports virtualization. If you don’t see any output, you may need to enable virtualization in your system’s BIOS/UEFI settings.

BIOS/UEFI Configuration

If virtualization is not enabled:

  1. Restart your system and enter the BIOS/UEFI setup (usually by pressing F2, Del, or Esc during boot).
  2. Look for options related to “Virtualization,” “Intel VT-x,” “AMD-V,” or “SVM Mode.”
  3. Enable these options, save changes, and reboot.

Kernel Module Verification

Ensure that the KVM kernel module is loaded:

lsmod | grep kvm

You should see output including “kvm” and either “kvm_intel” or “kvm_amd” depending on your processor.

Installation Process

Now that we’ve confirmed system compatibility, let’s proceed with installing KVM and its associated tools on Rocky Linux 9.

Installing Core Packages

First, update your system to ensure you have the latest packages:

sudo dnf update -y

Next, install the required KVM packages and management tools:

sudo dnf install qemu-kvm libvirt virt-install virt-viewer virt-manager -y

This command installs the following components:

  • qemu-kvm: The main KVM package
  • libvirt: The virtualization API library
  • virt-install: Command-line tool for creating new VMs
  • virt-viewer: Graphical console for connecting to VMs
  • virt-manager: User-friendly GUI for managing virtual machines

System Configuration

After installation, start and enable the libvirtd service:

sudo systemctl start libvirtd
sudo systemctl enable libvirtd

To allow your user account to manage virtual machines, add it to the “libvirt” group:

sudo usermod -aG libvirt $(whoami)
newgrp libvirt

Network Configuration

Proper network configuration is crucial for VM connectivity. We’ll set up bridge networking to allow VMs to appear as separate devices on your network.

Setting Up Bridge Networking

Install the bridge utilities package:

sudo dnf install bridge-utils -y

Create a bridge interface by editing the network configuration file. First, identify your main network interface:

ip a

Let’s assume your main interface is “eth0”. Create a new configuration file for the bridge:

sudo nano /etc/sysconfig/network-scripts/ifcfg-br0

Add the following content:

DEVICE=br0
TYPE=Bridge
BOOTPROTO=dhcp
ONBOOT=yes
DELAY=0

Now, edit the configuration of your main interface:

sudo nano /etc/sysconfig/network-scripts/ifcfg-eth0

Modify it to look like this:

DEVICE=eth0
TYPE=Ethernet
BOOTPROTO=none
ONBOOT=yes
BRIDGE=br0

Restart the network service to apply changes:

sudo systemctl restart NetworkManager

Virtual Machine Management Tools

KVM on Rocky Linux 9 offers both command-line and graphical tools for managing virtual machines.

Command Line Tools

The primary command-line tool for managing KVM virtual machines is virsh. Here are some essential commands:

  • List all VMs: virsh list --all
  • Start a VM: virsh start vm_name
  • Stop a VM: virsh shutdown vm_name
  • Force stop a VM: virsh destroy vm_name
  • Delete a VM: virsh undefine vm_name

The virt-install command is used for creating new virtual machines. Here’s a basic example:

virt-install --name=testvm --vcpus=2 --memory=2048 --cdrom=/path/to/iso --disk size=20

Graphical Tools

For those who prefer a graphical interface, virt-manager (Virtual Machine Manager) provides a user-friendly way to create and manage VMs. To launch it, simply run:

virt-manager

Additionally, Rocky Linux 9 includes Cockpit, a web-based interface for system administration. To enable and access Cockpit:

sudo systemctl enable --now cockpit.socket
sudo firewall-cmd --add-service=cockpit --permanent
sudo firewall-cmd --reload

You can then access Cockpit by navigating to https://your_server_ip:9090 in a web browser.

Creating Your First Virtual Machine

Let’s walk through the process of creating a virtual machine using virt-manager:

  1. Launch virt-manager: virt-manager
  2. Click on the “Create a new virtual machine” button.
  3. Choose your installation method (e.g., Local ISO image).
  4. Browse and select your ISO file.
  5. Allocate RAM and CPU cores for the VM.
  6. Create or select a storage volume for the VM’s disk.
  7. Configure network settings, preferably using the bridge we created earlier.
  8. Review the settings and click “Finish” to create the VM.

The virtual machine will start, and you can proceed with the guest OS installation.

Post-Installation Configuration

After setting up KVM and creating your first VM, it’s important to consider security and performance optimizations.

Security Considerations

SELinux Configuration

Rocky Linux 9 comes with SELinux enabled by default, which provides an additional layer of security. Ensure it’s in enforcing mode:

sudo sestatus

If it’s not in enforcing mode, edit /etc/selinux/config and set SELINUX=enforcing, then reboot.

Firewall Settings

Configure the firewall to allow necessary services:

sudo firewall-cmd --permanent --add-service=libvirt
sudo firewall-cmd --reload

Access Control

Regularly audit user access to the libvirt group and remove any unnecessary accounts:

getent group libvirt

Performance Optimization

To get the best performance out of your KVM setup on Rocky Linux 9, consider the following optimizations:

Memory Management

Enable KSM (Kernel Samepage Merging) to reduce memory usage:

sudo systemctl enable --now ksmtuned

CPU Allocation

When creating VMs, align virtual CPUs with physical CPU cores for optimal performance. Use the lstopo command to view your system’s topology:

sudo dnf install hwloc -y
lstopo

Storage Optimization

Use virtio drivers for disk and network devices in your VMs for better performance. When creating a new VM, select “virtio” as the disk bus type.

Troubleshooting Tips

Even with careful setup, you may encounter issues. Here are some common problems and their solutions:

VM Won’t Start

Check the VM’s XML configuration:

virsh dumpxml vm_name

Look for any misconfigurations in device paths or resource allocations.

Network Connectivity Issues

Verify bridge interface status:

ip a show br0

Ensure the bridge is up and has an IP address.

Performance Problems

Monitor VM performance using tools like top or htop inside the VM. For host-level monitoring, use:

virt-top

Congratulations! You have successfully installed KVM. Thanks for using this tutorial for installing the Kernel-based Virtual Machine (KVM) on your Rocky Linux 9 system. For additional or useful information, we recommend you check the official KVM website.

VPS Manage Service Offer
If you don’t have time to do all of this stuff, or if this is not your area of expertise, we offer a service to do “VPS Manage Service Offer”, starting from $10 (Paypal payment). Please contact us to get the best deal!

r00t

r00t is an experienced Linux enthusiast and technical writer with a passion for open-source software. With years of hands-on experience in various Linux distributions, r00t has developed a deep understanding of the Linux ecosystem and its powerful tools. He holds certifications in SCE and has contributed to several open-source projects. r00t is dedicated to sharing her knowledge and expertise through well-researched and informative articles, helping others navigate the world of Linux with confidence.
Back to top button