Cyber Services / All lessons / CentOS / Linux Administration / KVM Virtualization

KVM Virtualization

Module 30 of 33 · 2 min read · Free to read

MODULES ↓ Command blocks have a COPY button

Step-by-Step Guide on Rocky Linux / CentOS 8.5

Virtualization allows multiple isolated OS environments—Virtual Machines (VMs)—to run on a single physical host. KVM (Kernel-based Virtual Machine) is built into the Linux kernel, turning your Rocky Linux or CentOS server into a full-featured Type 1 hypervisor.

Type 1 vs. Type 2 Hypervisors

Type 1 (Bare Metal) Type 2 (Hosted)
Installed directly on hardware. Maximum performance and security. Runs on top of an existing OS like Windows or macOS.
Analogy: Architect designing a skyscraper from scratch. Analogy: Renting an office in an existing building.
Examples: VMware ESXi, Hyper-V, Rocky Linux KVM. Examples: VirtualBox, VMware Workstation.

KVM turns Rocky Linux/CentOS 8.5 into a powerful Type 1 hypervisor.

KVM Software Stack

Management Tools (virsh, virt-manager)

User-friendly command-line and GUI tools.

Libvirt API

Standard API to manage hypervisors.

QEMU

Emulates CPU, memory, disks, and network devices.

KVM Kernel Module

Provides hardware acceleration (Intel VT-x / AMD-V).

  • KVM: Core virtualization engine.
  • QEMU: Hardware emulator.
  • Libvirt: Management API for VMs.

Step 1: Prepare the Host

  1. Check CPU Virtualization:
    $ lscpu | grep Virtualization
    Virtualization:      VT-x
  2. Install KVM and Management Packages:
    sudo dnf groupinstall -y "Virtualization Host"
    sudo dnf install -y virt-manager libvirt libvirt-python libguestfs-tools qemu-kvm
  3. Enable libvirtd Service:
    sudo systemctl enable --now libvirtd
    sudo systemctl start libvirtd
  4. Verify Installation:
    $ virsh list --all
     Id    Name                           State
    --------------------------------------

Step 2: Create a VM (CLI)

sudo virt-install \
--name rocky8-vm1 \
--ram 2048 \
--vcpus 2 \
--disk path=/var/lib/libvirt/images/rocky8-vm1.qcow2,size=20 \
--os-variant rhel8.5 \
--network bridge=virbr0 \
--graphics none \
--console pty,target_type=serial \
--location /home/user/isos/Rocky-8.6-x86_64-dvd1.iso \
--extra-args 'console=ttyS0,115200n8 serial'

This launches the text-based installer automatically.

Step 3: Manage VMs with virsh

  • List all VMs: sudo virsh list --all
  • Start VM: sudo virsh start rocky8-vm1
  • Shutdown VM: sudo virsh shutdown rocky8-vm1
  • Force off VM: sudo virsh destroy rocky8-vm1
  • Connect console: sudo virsh console rocky8-vm1
  • Delete VM definition: sudo virsh undefine rocky8-vm1

Step 4: Advanced Management

  1. Create a Snapshot:
    sudo virsh snapshot-create-as rocky8-vm1 snap1 "Snapshot before update"
  2. List Snapshots:
    sudo virsh snapshot-list rocky8-vm1
  3. Restore Snapshot:
    sudo virsh snapshot-revert rocky8-vm1 snap1
  4. Attach a Disk:
    sudo virsh attach-disk rocky8-vm1 /var/lib/libvirt/images/data.qcow2 vdb --persistent
  5. Networking (Bridge):
    sudo nmcli connection add type bridge con-name br0 ifname br0
    sudo nmcli connection add type bridge-slave con-name br0-port1 ifname enp1s0 master br0

Step 5: GUI Management with virt-manager

  1. Run virt-manager or search "Virtual Machine Manager".
  2. Create a new VM, select ISO, assign CPU/RAM/disk.
  3. Finish, boot VM, install OS graphically.
  4. Use virsh to manage lifecycle even for GUI-created VMs.
Lab: From Beginner to Expert

Create multiple VMs, experiment with snapshots, networking, and automation scripts. Confirm everything works via virsh list --all.

Modules (33) ← CentOS / Linux Administration