libvirt (virsh, virt-install, virt-manager) starts and manages one QEMU process per VM.
QEMU runs the guest’s CPU through the KVM kernel module, via ioctl() calls on /dev/kvm. KVM uses the CPU’s VT-x or AMD-V extensions.
Emulated NIC (e1000, VM 1): every packet goes through the NIC emulated by QEMU in user space, then to the VM’s tap device in the host (vnet0, … in virsh domiflist).
virtio-net (VM 2): with vhost-net, which libvirt uses by default when /dev/vhost-net exists, packets go from the guest’s virtqueues straight to a vhost-net kernel thread and the tap device. QEMU only sets it up, which is why virtio is much faster (see Recommended Settings).
Bridge: the tap devices are ports of a Linux bridge. With your own br0, the physical NIC is a port of the same bridge. libvirt’s NAT network virbr0 (virsh net-info default) has no physical port: the host routes and NATs its traffic (nftables or iptables) before it reaches the NIC.
Host Check
# Check KVM, IOMMU and cgroup support in one go (PASS / WARN / FAIL per line)
virt-host-validate qemu
# CPU virtualization extensions (vmx = Intel VT-x, svm = AMD-V); 0 means none or disabled in firmwaregrep-cE'vmx|svm' /proc/cpuinfo
# KVM modules loaded and the device present
lsmod | grep kvm
ls-l /dev/kvm
VM States
State
Description
running
The domain is currently running on a CPU
idle
The domain is idle — waiting on I/O or has nothing to do
paused
Paused via virsh suspend — still consumes memory but not scheduled
in shutdown
Shutting down — guest OS notified and stopping gracefully
shut off
Not running — fully shut down or not yet started
crashed
Ended violently — only if configured not to restart on crash
pmsuspended
Suspended by guest power management (e.g., S3 sleep state)
VM Lifecycle
Command
Details
virsh list
List running VMs
virsh list --all
List all VMs (including stopped)
virsh list --autostart
List VMs set to autostart
virsh start <vm-name>
Start a VM
virsh shutdown <vm-name>
Shutdown (graceful — sends ACPI signal)
virsh destroy <vm-name>
Force stop (like pulling the power)
virsh destroy <vm-name> --graceful
Force stop without resorting to SIGKILL (returns error if guest doesn’t stop)
virsh reboot <vm-name>
Reboot
virsh suspend <vm-name>
Suspend (pause in memory)
virsh resume <vm-name>
Resume from suspend
virsh save <vm-name> /path/to/save-file
Save VM state to file (hibernate)
virsh restore /path/to/save-file
Restore VM from saved state
virsh reset <vm-name>
Reset (hard reset, no graceful shutdown)
virsh shutdown <vm-name> --mode acpi
Send ACPI shutdown signal
virsh domrename <old-name> <new-name>
Rename a VM (must be off)
VM Creation
# Create VM from ISO
virt-install \--name myvm \--ram 2048 \--vcpus 2 \--diskpath=/var/lib/libvirt/images/myvm.qcow2,size=20 \--os-variant ubuntu22.04 \--networkbridge=br0 \--graphics vnc,listen=0.0.0.0 \--cdrom /path/to/installer.iso
# Create VM with default NAT network
virt-install \--name myvm \--ram 2048 \--vcpus 2 \--diskpath=/var/lib/libvirt/images/myvm.qcow2,size=20 \--os-variant rocky9.0 \--networknetwork=default \--graphics spice \--cdrom /path/to/installer.iso
# Headless VM (serial console, no graphics)
virt-install \--name headless \--ram 1024 \--vcpus 1 \--diskpath=/var/lib/libvirt/images/headless.qcow2,size=10 \--os-variant generic \--networknetwork=default \--graphics none \--extra-args='console=ttyS0,115200n8 serial'\--location /path/to/installer.iso
# Import existing disk image (no install)
virt-install \--name imported \--ram 2048 \--vcpus 2 \--diskpath=/var/lib/libvirt/images/existing.qcow2 \--os-variant generic \--networknetwork=default \--import\--graphics vnc
# Create VM with multiple disks
virt-install \--name multi-disk \--ram 4096 \--vcpus 4 \--diskpath=/var/lib/libvirt/images/root.qcow2,size=30 \--diskpath=/var/lib/libvirt/images/data.qcow2,size=100 \--os-variant rocky9.0 \--networknetwork=default \--cdrom /path/to/installer.iso
# Create VM with cloud-init (NoCloud datasource)
virt-install \--name cloud-vm \--ram 2048 \--vcpus 2 \--diskpath=/var/lib/libvirt/images/cloud.qcow2 \--os-variant ubuntu22.04 \--networknetwork=default \--cloud-init user-data=/path/to/user-data.yaml \--import# List available OS variants
osinfo-query os
osinfo-query os | grep-i ubuntu
osinfo-query os | grep-i rhel
osinfo-query os | grep-i rocky
Quick Start from a Cloud Image
The fastest way to a working VM: no installer, cloud-init configures it on first boot.
cd /var/lib/libvirt/images
# Download a cloud image once, as the base for all VMs# (Rocky: https://dl.rockylinux.org/pub/rocky/9/images/x86_64/Rocky-9-GenericCloud-Base.latest.x86_64.qcow2)sudo curl -LO https://cloud-images.ubuntu.com/noble/current/noble-server-cloudimg-amd64.img
# Per VM: a 20 GB overlay on top of the base (the base itself is never written to)sudo qemu-img create -f qcow2 -b noble-server-cloudimg-amd64.img -F qcow2 vm01.qcow2 20G
# Create and boot the VM
virt-install \--name vm01 \--memory 4096 \--vcpus 2 \--diskpath=/var/lib/libvirt/images/vm01.qcow2 \--os-variant ubuntu24.04 \--networknetwork=default \--cloud-init user-data=user-data.yaml \--import\--noautoconsole
Then virsh domifaddr vm01 shows its IP and ssh admin@<ip> logs in.
VM Deletion
Command
Details
virsh undefine <vm-name>
Undefine (remove VM definition, keep disk)
virsh undefine <vm-name> --remove-all-storage
Undefine and remove all storage
virsh undefine <vm-name> --nvram
Undefine with NVRAM (UEFI VMs)
virsh undefine <vm-name> --snapshots-metadata
Undefine with snapshots
VM Information
Command
Details
virsh dominfo <vm-name>
Detailed VM info
virsh dumpxml <vm-name>
VM XML configuration
virsh domiflist <vm-name>
VM network interfaces
virsh domifaddr <vm-name>
VM IP address (DHCP leases of the libvirt network; --source agent asks the guest agent)
virsh dommemstat <vm-name>
VM memory stats
virsh cpu-stats <vm-name>
VM CPU stats
virsh domstate <vm-name>
VM state
virsh domid <vm-name>
VM ID
virsh domuuid <vm-name>
VM UUID
virsh domdisplay <vm-name>
Display URI (VNC or SPICE)
VM Configuration
Command
Details
virsh edit <vm-name>
Edit VM XML (opens in $EDITOR)
EDITOR=nano virsh edit <vm-name>
Edit with a specific editor
virsh define /etc/libvirt/qemu/vm01.xml
Define a VM from XML file
virsh autostart <vm-name>
Set autostart (start on host boot)
virsh autostart --disable <vm-name>
Disable autostart
virsh desc <vm-name> --title "My Web Server" virsh desc <vm-name> "Production web server running nginx"
Change VM title/description
Memory and vCPUs: see CPU and Memory below
virt-xml (Edit a VM Without Touching XML)
Installed with virt-install. Changes are persistent and apply on the next boot.
# Memory (MiB) and vCPUs
virt-xml <vm-name> --edit--memorymemory=4096,currentMemory=4096
virt-xml <vm-name> --edit--vcpus 4
# Add a new 20 GB disk, or a NIC on the default network
virt-xml <vm-name> --add-device--disksize=20
virt-xml <vm-name> --add-device--networknetwork=default,model=virtio
# Preview a change as a diff, without applying it
virt-xml <vm-name> --edit--vcpus 4 --print-diff# Also apply it to the running VM, where hot-plug is supported
virt-xml <vm-name> --add-device--disksize=20 --update
Console and Display
Command
Details
virsh console <vm-name>
Serial console (text-based access). Exit with: Ctrl+] or Ctrl+5
virsh vncdisplay <vm-name>
VNC display port (:0 means port 5900, :1 means 5901, etc.)
UEFI VMs: internal snapshots (the default, without --disk-only) need the NVRAM file in qcow2 format. With a raw NVRAM file libvirt refuses: internal snapshots of a VM with pflash based firmware require QCOW2 nvram format. Check with virsh dumpxml <vm-name> | grep nvram; otherwise use external snapshots (--disk-only).
Cloning
# Clone a VM (must be shut down)
virt-clone --original <source-vm> --name <new-vm> --auto-clone# Clone with specific disk path
virt-clone --original <source-vm> --name <new-vm> \--file /var/lib/libvirt/images/new-vm.qcow2
# Clone with multiple disks
virt-clone --original <source-vm> --name <new-vm> \--file /var/lib/libvirt/images/new-root.qcow2 \--file /var/lib/libvirt/images/new-data.qcow2
# Before the clone's first boot: reset machine-id, SSH host keys, MAC config, DHCP leases, ...
virt-sysprep -d <new-vm>
# Only selected operations, and set a new hostname
virt-sysprep -d <new-vm> --operations machine-id,ssh-hostkeys,net-hwaddr,dhcp-client-state --hostname <new-vm>
A clone made with virt-clone keeps the source’s machine-id, SSH host keys and hostname, which leads to duplicate DHCP leases and SSH host key warnings. Run virt-sysprep on it while it is shut off. virt-sysprep --list-operations shows everything it can reset.
# List all VM IPs (requires guest agent)for vm in$(virsh list --name);do
ip=$(virsh domifaddr "$vm"--source agent 2>/dev/null | awk'/ipv4/{print $4}' | cut-d/ -f1)echo"$vm: ${ip:-N/A}"done# Find which VM uses a disk image
virsh list --all--name | while read vm;do[-n"$vm"]&& virsh domblklist "$vm" 2>/dev/null | grep-q"/path/to/disk"&&echo"$vm"done# Get total resources used by all VMsecho"Total vCPUs: $(virsh list --name | xargs -I{} virsh vcpucount {}--current 2>/dev/null | paste-sd+ | bc)"echo"VMs running: $(virsh list --name | wc-l)"# Export a VM (disks + libvirt XML) with virt-v2v; there is no OVA output (-of is raw or qcow2)
virsh shutdown <vm-name> # wait until it is shut off
virt-v2v -i libvirt <vm-name> -olocal-os /tmp/export -of qcow2
# Check QEMU version
qemu-system-x86_64 --version# Check libvirt version
virsh version
# View host capabilities
virsh capabilities | head-50
libguestfs Tools (VM Filesystem Access)
Access VM disk contents without booting the VM:
Command
Details
virt-ls -l -d <vm-name> /etc
List files in a VM
virt-cat -d <vm-name> /etc/fstab
Display a file from a VM
virt-edit -d <vm-name> /etc/fstab
Edit a file in a VM (VM must be shut down)
virt-df -h -d <vm-name>
Display disk usage
virt-filesystems -l -h -d <vm-name>
List filesystems
virt-filesystems -l -h --partitions -d <vm-name>
List partitions
virt-log -d <vm-name> \| less
Display log messages
Important Files and Directories
Path
Description
/var/lib/libvirt/images/
Default VM disk images location
/etc/libvirt/qemu/
VM XML configuration files
/var/log/libvirt/qemu/
Per-VM log files (<domain>.log)
$HOME/.cache/virt-manager/virt-install.log
virt-install tool log
$HOME/.cache/virt-manager/virt-manager.log
virt-manager GUI log
/etc/libvirt/libvirtd.conf
libvirt daemon configuration
/etc/libvirt/qemu.conf
QEMU driver configuration
/var/run/libvirt/
Runtime files (sockets, PID files)
Recipes
Delete a VM Completely (Definition + Disk)
virsh shutdown vm01 # returns at once: wait until 'virsh domstate vm01' says shut off
virsh undefine vm01
virsh vol-delete --pool default vm01.qcow2
Increase Memory (e.g., 1 GB → 2 GB)
virsh dominfo vm01
virsh shutdown vm01 # wait until shut off# Give a unit: a plain number is KiB, so "2048" would mean 2 MiB
virsh setmaxmem vm01 2G --config
virsh setmem vm01 2G --config
virsh dominfo vm01
virsh start vm01
virsh destroy does an ungraceful immediate power-off — can corrupt guest filesystems. Use virsh shutdown for graceful stops. The --graceful flag avoids SIGKILL if the guest doesn’t stop in a reasonable timeout (returns an error instead of forcing).
virsh undefine removes the XML configuration. If the VM is running, it becomes a transient domain and the config is removed when it stops. Disk images are NOT deleted unless --remove-all-storage is used.
VM XML files live in /etc/libvirt/qemu/ — never edit them directly. Use virsh edit instead.
The guest agent (qemu-guest-agent) must be installed inside the VM for domifaddr --source agent, domfsfreeze, and other guest-aware commands to work.
<cpumode="host-passthrough"><!-- topoext exists only on AMD CPUs --><featurepolicy="require"name="topoext"/></cpu><cputune><vcpupinvcpu="0"cpuset="2-3"/><emulatorpincpuset="0-1"/><iothreadpiniothread="1"cpuset="4"/></cputune>
# Full backup that also creates checkpoint cp1 (the start point for incremental backups)
virsh backup-begin <vm-name> --checkpointxml checkpoint.xml
# Later: incremental backup of everything changed since cp1
virsh backup-begin <vm-name> backup.xml
# Progress, or cancel (a push-mode backup ends on its own when done)
virsh domjobinfo <vm-name>
virsh domjobabort <vm-name>
# List checkpoints
virsh checkpoint-list <vm-name>
# Load the nbd module, then connect the disk image as a block devicesudo modprobe nbd max_part=8
sudo qemu-nbd --connect=/dev/nbd0 disk.qcow2
# Access with guestfish
guestfish --ro-a /dev/nbd0 -i# Disconnectsudo qemu-nbd --disconnect /dev/nbd0
Then regenerate the GRUB config and reboot: grub2-mkconfig -o /boot/grub2/grub.cfg on RHEL/Fedora, sudo update-grub on Debian/Ubuntu.
Bind Device to vfio-pci
# Find the device and its [vendor:device] ID, e.g. [10de:1eb8]
lspci -nn | grep-i nvidia # or your device# Load vfio-pci and unbind the device from its current driversudo modprobe vfio-pci
echo"0000:3b:00.0" | sudo tee /sys/bus/pci/devices/0000:3b:00.0/driver/unbind
# Bind to vfio-pci, using the vendor and device ID from lspciecho"10de 1eb8" | sudo tee /sys/bus/pci/drivers/vfio-pci/new_id
Attach PCI Device to VM
# List PCI devices
virsh nodedev-list | grep pci
# Detach from host
virsh nodedev-detach pci_0000_3b_00_0
# Attach to VM (via XML)
virsh attach-device <vm-name> pci-device.xml --live
# Create Virtual Functionsecho 4 | sudo tee /sys/class/net/enp3s0f0/device/sriov_numvfs
# Verify VFs created
lspci | grep"Virtual Function"
ip link show enp3s0f0
Advanced Networking
macvtap (Direct Physical Interface Access)
# Attach macvtap interface (guest gets own MAC on physical network)
virsh attach-interface <vm-name> --type direct --source eth0 --model virtio --live
Note: With macvtap, the guest can communicate with the external network but NOT with the host.
# Create seed ISO for cloud images
cloud-localds seed.iso user-data meta-data
# Attach to VM before it boots (CD-ROM hot-plug isn't supported on SATA/IDE)
virsh attach-disk <vm-name> /var/lib/libvirt/images/seed.iso sdb --type cdrom --config
virtiofs (Shared Filesystem)
XML Configuration
virtiofs needs shared guest memory, so the domain also needs a <memoryBacking> block: