virsh Command Cheat Sheet: Manage KVM Virtual Machines from the Terminal

The virsh commands you actually need: list, start, shutdown, console, edit, autostart, resources, disks and networks, with examples and pitfalls.

Published November 27, 2026

virsh is the Swiss-army knife of libvirt. Anything virt-manager can do, virsh can do from a terminal or a shell script. This cheat sheet groups the commands you will use daily, explains what each one actually does under the hood, and points out the ones that look similar but behave very differently.

The concept

virsh commands operate on domains (libvirt's word for a virtual machine), networks, storage pools, volumes, and snapshots. Most commands take the domain name, its numeric ID (only while running), or its UUID. virsh can run one command at a time (virsh list) or as an interactive shell (just type virsh) with tab completion.

An important distinction is between the persistent configuration and the live state. A VM has an XML definition on disk and, while running, a live configuration in the QEMU process. Many virsh commands accept --config (change the saved definition, applies on next boot), --live (change the running VM only), or both. Getting this wrong is why changes "disappear" after a reboot.

Lifecycle commands

TaskCommandNotes
List all VMsvirsh list --allWithout --all only running VMs show
Start / autostartvirsh start web01 · virsh autostart web01Autostart boots it with the host
Graceful stopvirsh shutdown web01Sends ACPI power button; guest must respond
Hard stopvirsh destroy web01Pulls the plug. Does NOT delete anything
Reboot / resetvirsh reboot web01 · virsh reset web01reset is an immediate hardware reset
Pause / resumevirsh suspend web01 · virsh resume web01Freezes vCPUs, keeps RAM
Serial consolevirsh console web01Detach with Ctrl+]
Edit definitionvirsh edit web01Opens XML in $EDITOR, validates on save
Infovirsh dominfo web01 · virsh domstats web01State, memory, vCPUs, counters
Deletevirsh undefine web01 --remove-all-storageDestroys disks too. Be sure

VM states

startsuspendresume → stopshut offdefinedrunningstartpausedsuspendshut offshutdown/destroy
Domain states you will see in virsh list.

Changing resources safely

CPU and memory have two ceilings: the maximum (set at boot) and the current value (adjustable while running, up to the maximum). Raise the maximum with --config and reboot; then you can grow and shrink the current value live with ballooning or vCPU hotplug, provided the guest supports it.

# Persistently give web01 4 vCPUs and 8 GiB (applies next boot)
virsh setvcpus web01 4 --maximum --config
virsh setvcpus web01 4 --config
virsh setmaxmem web01 8G --config
virsh setmem web01 8G --config

# Live change within the current maximum
virsh setmem web01 6G --live

Disks and NICs

virsh domblklist web01
Target Source
------------------------------------------------
vda /var/lib/libvirt/images/web01.qcow2
virsh attach-disk web01 /var/lib/libvirt/images/data.qcow2 vdb --driver qemu --subdriver qcow2 --persistent
Disk attached successfully
virsh domiflist web01
Interface Type Source Model MAC
vnet0 network default virtio 52:54:00:3a:91:7c
Inspecting and hot-plugging devices.

Practical examples

Script: gracefully stop all running VMs

Useful before host maintenance. Waits until each guest is actually off.

for vm in $(virsh list --name --state-running); do
  virsh shutdown "$vm"
done
while [ -n "$(virsh list --name --state-running)" ]; do sleep 2; done
echo "all guests stopped"

Get the IP address of a guest

Three sources: DHCP leases, the guest agent, or ARP.

virsh domifaddr web01                 # DHCP lease on libvirt network
virsh domifaddr web01 --source agent  # needs qemu-guest-agent in the guest
virsh domifaddr web01 --source arp

Rename and change autostart

The VM must be shut off to rename.

virsh domrename web01 web-prod-01
virsh autostart web-prod-01
virsh autostart --disable web-prod-01

Common mistakes

  • Confusing destroy with delete. destroy only powers off; undefine removes the definition.
  • Expecting shutdown to work on a guest without ACPI support or still in the bootloader. Use destroy as a last resort.
  • Changing settings with --live only and losing them on reboot. Add --config, or both.
  • Editing XML files under /etc/libvirt/qemu directly. libvirt will not notice; always use virsh edit or virsh define.
  • Running undefine on a UEFI VM without --nvram and getting an error about the NVRAM file.

Key takeaways

  • virsh list --all, start, shutdown, destroy and console cover most daily work.
  • Know the difference between --config (persistent) and --live (running only).
  • Use virsh edit for XML changes; it validates before saving.
  • Install qemu-guest-agent in guests for IP reporting, clean shutdowns and filesystem freeze.

FAQ

How do I exit virsh console?

Press Ctrl+] (control and right square bracket). On some keyboard layouts use Ctrl+5 or configure an escape with --escape.

Can virsh manage remote hosts?

Yes: virsh -c qemu+ssh://user@host/system list. Keys-based SSH makes it seamless.

Where are VM definitions stored?

Persistent domain XML lives in /etc/libvirt/qemu/ for qemu:///system, but treat those files as read-only and change them through virsh.

More Linux articles