Skip to content
systemcoding.dev
Virtualization

Set Up a Linux Workstation for KVM Development Without Running as Root

Prepare a Linux machine for KVM and QEMU work as a normal user: packages, the kvm group, headers, tracing access and a smoke test, with no sudo for daily use.

7 min read
Summarize in AI
Flat illustration of guest machines running on a hypervisor over hardware, with the Linux, QEMU and RISC-V logos.

Writing a hypervisor or testing a kernel in QEMU should not require a root shell. Everything a developer needs from KVM is reachable by an ordinary user once a handful of one-time steps are done with sudo, and running day-to-day work as root is a habit that ends with a typo erasing a disk or a guest-controlled path writing into your home directory as root. This post sets up a Linux workstation (Debian 12 or Ubuntu 24.04 as the example, Linux 6.12 as the kernel, QEMU 9.2) so you can build KVM programs, run QEMU, record traces and debug guests as yourself. It covers the packages, the kvm group and /dev/kvm, the kernel headers, access to tracing and performance counters without root, and a smoke test that proves each piece. It supports the other posts in this series, starting from the complete guide to the Linux KVM API.

What you need and why

NeedWhat provides itNeeds root once?
Hardware virtualization onFirmware setting, CPU supportYes (firmware)
KVM kernel modules loadedkvm_intel or kvm_amdUsually automatic
Permission to open /dev/kvmkvm group membershipYes, to add the group
A C compiler and KVM headersbuild-essential, linux-libc-devYes, to install
QEMU and qemu-imgqemu-system-x86, qemu-utilsYes, to install
A debuggergdbYes, to install
Tracing and perf as a userKernel settings and group accessYes, once

After the one-time work, none of the daily commands need elevated privileges.

Step 1: confirm the hardware

Run the checks from the earlier posts first, so a missing feature does not look like a permissions problem later:

Shell
grep -E -c '(vmx|svm)' /proc/cpuinfo
ls -l /dev/kvm
Text
8
crw-rw---- 1 root kvm 10, 232 Nov 23 09:14 /dev/kvm

A non-zero count and a /dev/kvm node mean the CPU feature and the kernel modules are fine. If either is missing, fix it with the posts on checking CPU support and on enabling hardware virtualization before continuing.

Step 2: install the packages

On Debian or Ubuntu:

Shell
sudo apt update
sudo apt install build-essential gdb git \
  qemu-system-x86 qemu-system-arm qemu-utils \
  linux-libc-dev linux-perf trace-cmd \
  cpu-checker

The pieces: build-essential gives gcc and make, linux-libc-dev installs the userspace kernel headers including <linux/kvm.h> that every KVM program includes, qemu-system-* provides the emulators, qemu-utils provides qemu-img, linux-perf and trace-cmd are the tracing front ends, and cpu-checker provides kvm-ok. On Fedora the equivalents are gcc, make, kernel-headers, qemu-system-x86-core, qemu-img, perf and trace-cmd.

Check the header is present and which version of the API it describes:

Shell
grep -n 'define KVM_API_VERSION' /usr/include/linux/kvm.h
Text
18:#define KVM_API_VERSION 12

The headers come from the installed linux-libc-dev package and may be older than your running kernel, which is fine: the API is backward compatible. It does mean a new ioctl introduced in Linux 6.12 may be missing from the header on a system whose headers are older, and the compile error KVM_SOMETHING undeclared is the symptom. Either install newer headers or define the constant yourself for a quick test.

Step 3: give your user access to /dev/kvm

The device is owned by root:kvm with mode 0660, so membership in kvm is the whole story:

Shell
sudo usermod -aG kvm "$USER"

The change applies to new login sessions. In the current shell, start a subshell with the new group to test without logging out:

Shell
newgrp kvm
id -nG | tr ' ' '\n' | grep -x kvm
Text
kvm

A full logout and login makes it permanent for every program. If you work over SSH, reconnect. On a desktop, systemd-logind also grants access to the user at the active seat through an ACL, which is why KVM may work on the physical console and fail in a remote shell until the group is added.

If your distribution does not create the kvm group or the device permissions are different, a udev rule sets them. Create /etc/udev/rules.d/65-kvm.rules:

Text
KERNEL=="kvm", GROUP="kvm", MODE="0660", OPTIONS+="static_node=kvm"

and apply it with sudo udevadm control --reload && sudo udevadm trigger --name-match=kvm. Most distributions ship an equivalent rule already, so only add one if ls -l /dev/kvm shows the wrong group.

Step 4: prove it works with a program

A smoke test beats guessing. This program opens /dev/kvm as the current user, creates a VM, and prints the API version:

smoke.cc
#include <fcntl.h>
#include <linux/kvm.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <unistd.h>

int main(void)
{
    int kvm = open("/dev/kvm", O_RDWR | O_CLOEXEC);
    if (kvm < 0) {
        perror("open /dev/kvm");
        return 1;
    }
    printf("api version: %d\n", ioctl(kvm, KVM_GET_API_VERSION, 0));

    int vm = ioctl(kvm, KVM_CREATE_VM, 0);
    if (vm < 0) {
        perror("KVM_CREATE_VM");
        return 1;
    }
    puts("created a VM");
    close(vm);
    close(kvm);
    return 0;
}
Shell
gcc -O2 -Wall smoke.c -o smoke
./smoke
Text
api version: 12
created a VM

If you see open /dev/kvm: Permission denied, the group has not taken effect in this session. If you see No such file or directory, the module is not loaded. Both are covered in the troubleshooting post, and both are fixed once.

Step 5: a QEMU run as a normal user

Confirm that QEMU accelerates with KVM without root:

Shell
qemu-system-x86_64 -accel kvm -m 128M -display none -monitor stdio -S
Text
QEMU 9.2.0 monitor - type 'help' for more information
(qemu) info kvm
kvm support: enabled
(qemu) quit

With -S QEMU starts paused with no kernel, so this tests KVM alone. For a networked guest without root, use the user-mode backend, which needs no privileges, as in the hostfwd post. For TAP or bridge networking you need either a one-time root setup of a persistent interface or the qemu-bridge-helper approach described in the networking post.

Step 6: tracing and performance counters without root

KVM debugging relies on the kernel's tracepoints and perf. Both are restricted by default for good reasons, and you can relax them in a controlled way.

For perf, the setting kernel.perf_event_paranoid controls what unprivileged users can measure. Check it:

Shell
cat /proc/sys/kernel/perf_event_paranoid
Text
2

Level 2 lets users profile their own user-space code, and a level of 1 or lower is needed to see kernel events. Setting it lower reduces a protection that limits information leaks, so do it only on a development machine you control, and make it persistent through sysctl.d:

Shell
echo 'kernel.perf_event_paranoid = 1' | sudo tee /etc/sysctl.d/90-perf.conf
sudo sysctl --system

For perf kvm stat, which reads host-wide kernel events, you may still need elevated rights, in which case sudo perf kvm stat record ... for that one command is reasonable, or grant the CAP_PERFMON capability to the perf binary with sudo setcap cap_perfmon,cap_sys_ptrace,cap_syslog=ep $(which perf). Capabilities on a binary apply to every user who can run it, so on a shared machine prefer sudo.

For raw tracepoints, tracefs is at /sys/kernel/tracing, owned by root. To use it without sudo, either run trace-cmd through sudo for the recording step, which is the simplest, or create a tracing group and change the permissions with a udev or systemd tmpfiles rule. A tmpfiles entry in /etc/tmpfiles.d/tracing.conf:

Text
z /sys/kernel/tracing 0750 root tracing - -

This sets the group on that directory at boot. Tracing exposes kernel internals, so a group with read access is a deliberate choice.

Step 7: a project skeleton

Keep your KVM experiments in one directory with a small Makefile, so builds are one command:

Makefilemakefile
CFLAGS ?= -O2 -Wall -Wextra -g
PROGS  := smoke hello-kvm

all: $(PROGS)

%: %.c
	$(CC) $(CFLAGS) $< -o $@

clean:
	rm -f $(PROGS)

.PHONY: all clean

Run make and then each program as yourself. For the debugging cycle, gdb ./hello-kvm works unchanged on a KVM program: the guest is not visible to gdb, but your VMM is an ordinary process, and a breakpoint on the line before KVM_RUN shows the state you set up. To debug the guest, you use a gdb stub in your VMM or QEMU's -s -S.

Safety habits

Doing this as a normal user is not only about convenience. A few habits keep experiments contained.

  • Never run an untested VMM as root. A bug that writes through a guest-controlled pointer is the same bug with root's authority.

  • Limit memory. A VMM that allocates guest RAM with mmap can request more than the machine has. Use ulimit -v in the shell you test in, or run under a cgroup limit with systemd-run --user --scope -p MemoryMax=2G ./hello-kvm.

  • Use a scratch directory for images. Keep qcow2 files and kernels in a project directory, not in /var/lib.

  • Keep the host kernel updated. KVM is host kernel code, and security fixes to it matter more than for most subsystems.

Troubleshooting

SymptomCauseFix
Permission denied on /dev/kvmNot in kvm group, or old sessionusermod -aG kvm, then log in again
No such file or directoryModules not loadedsudo modprobe kvm_intel (or kvm_amd)
KVM_SOMETHING undeclaredOld linux-libc-devUpdate headers or define the constant
perf shows no kernel eventsperf_event_paranoid too highLower it, or use sudo for that command
QEMU warns about KVM and runs slowlyKVM unavailable, fell back to TCGFix the cause, check info kvm
Also inKernels
Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.