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
| Task | Command | Notes |
|---|---|---|
| List all VMs | virsh list --all | Without --all only running VMs show |
| Start / autostart | virsh start web01 · virsh autostart web01 | Autostart boots it with the host |
| Graceful stop | virsh shutdown web01 | Sends ACPI power button; guest must respond |
| Hard stop | virsh destroy web01 | Pulls the plug. Does NOT delete anything |
| Reboot / reset | virsh reboot web01 · virsh reset web01 | reset is an immediate hardware reset |
| Pause / resume | virsh suspend web01 · virsh resume web01 | Freezes vCPUs, keeps RAM |
| Serial console | virsh console web01 | Detach with Ctrl+] |
| Edit definition | virsh edit web01 | Opens XML in $EDITOR, validates on save |
| Info | virsh dominfo web01 · virsh domstats web01 | State, memory, vCPUs, counters |
| Delete | virsh undefine web01 --remove-all-storage | Destroys disks too. Be sure |
VM states
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 --liveDisks and NICs
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 arpRename 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-01Common 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.