virsh is the libvirt CLI — the supervisor every KVM operator lives in. virt-manager is fine on a workstation; on a remote host with no display, virsh is the contract. This article is the subset of commands that come up daily in production: enumerating domains, manipulating power state, editing the XML safely, taking snapshots, and exporting a guest to move it elsewhere. The intent is muscle memory, not exhaustive coverage.
How to verify
Open a session and confirm the daemon is answering. virsh defaults to qemu:///session for unprivileged users — almost always wrong on a server. Always pass -c qemu:///system or export LIBVIRT_DEFAULT_URI in your shell profile.
export LIBVIRT_DEFAULT_URI=qemu:///system
virsh version
virsh nodeinfo
virsh list --all
virsh net-list --all
virsh pool-list --all
virsh version reports both libvirt and the running hypervisor. nodeinfo confirms the host has detected CPU sockets, cores, and memory correctly — a hypervisor that sees half its RAM is a BIOS or kernel problem masquerading as a libvirt one.
What’s happening
A libvirt “domain” is the guest’s persistent definition plus its current runtime state. The definition lives as XML in /etc/libvirt/qemu/<name>.xml; the runtime is the QEMU process the daemon spawned when you started it. virsh commands either mutate the persistent XML, the runtime state, or both — and the distinction matters. virsh setmem with --config changes the XML; without it, the change is live-only and lost on reboot. virsh edit always changes the XML and never the running guest.
Most subcommands accept a domain by name, UUID, or numeric ID. ID is only stable for the lifetime of the running guest — scripts should use name or UUID. The XML schema is documented in libvirt domain XML schema; virsh is the operational surface on top of it.
The procedure
-
Enumerate everything. Three lists matter — domains, networks, storage pools.
--allincludes inactive items; the default shows only running ones, which has surprised many a new operator:virsh list --all virsh net-list --all virsh pool-list --all --details -
Power state operations.
startboots a defined-but-stopped domain.shutdownsends ACPI;destroyis the equivalent of pulling the plug — use only when ACPI is ignored.rebootis also ACPI.suspendandresumepause/unpause the QEMU process; the guest does not notice it slept:virsh start web01 virsh shutdown web01 # graceful virsh destroy web01 # force-off virsh reboot web01 virsh suspend web01 && virsh resume web01 -
Edit the XML safely.
virsh editopens the persistent definition in$EDITOR, validates the XML on save, and refuses to apply changes that the schema rejects. The running guest is not touched — the new XML takes effect on the next start:virsh edit web01 virsh dumpxml web01 > /tmp/web01.xml # before editing manually virsh define /tmp/web01.xml # re-apply edited XML -
Snapshots — internal vs external. Internal snapshots store the diff inside the qcow2; external snapshots create a new file and pivot writes to it. External is what production wants: smaller live files, cleaner consolidation. Internal snapshots break live backup tools and require the guest’s disk format to be qcow2:
virsh snapshot-create-as web01 pre-upgrade --description 'before kernel bump' \ --disk-only --atomic --quiesce virsh snapshot-list web01 virsh snapshot-revert web01 pre-upgrade virsh snapshot-delete web01 pre-upgrade -
Live resource changes. Memory and vCPU counts can be adjusted live within the
<currentMemory>/<vcpu current>headroom defined in the XML. Without--config, the change is runtime-only:virsh setmem web01 4096M --config --live virsh setvcpus web01 4 --config --live virsh memtune web01 --hard-limit 8388608 --config -
Export, clone, import.
dumpxmlemits the definition;vol-downloadexports the disk image.virt-cloneis the libvirt-aware path to a copy with regenerated UUIDs and MACs:virsh dumpxml web01 > web01.xml virsh vol-download --pool default web01.qcow2 /backup/web01.qcow2 virt-clone --original web01 --name web01-clone --auto-clone -
Console access.
consoleattaches to a serial console — only works if the guest exposes one (the cloud images do; some Windows VMs do not).vncdisplayreturns the VNC port for graphical guests:virsh console web01 # Ctrl+] to exit virsh vncdisplay web01
Common pitfalls
- Forgetting
-c qemu:///system(or the env var) connects you to the per-user session URI, where your “missing” VMs are simply on a different libvirt instance. virsh destroydoes not delete a domain — it force-stops it.undefineis what removes the persistent XML; pair it with--remove-all-storageto also delete the disks.- Snapshot reverts on running guests can be silently incomplete if
--quiesceis omitted and the guest agent isn’t installed — writes in flight don’t get fsync’d. virsh editvalidates schema but not semantics — you can happily set a vCPU count higher than the host has cores; the guest will start but the OOM killer eventually intervenes.virsh listshowing an empty result on a host you know has VMs almost always means you are talking to the wrong URI; checkvirsh uri.
Stack Harbor runs libvirt fleets where virsh is the lingua franca — every operator can read a dumpxml, every change is captured by a config-management run, every snapshot has a retention rule, and the daemon is exposed via TLS only to the bastion. Clients who let virtualization grow as “one VM at a time” eventually need that consolidated; the operational pattern is covered in Managed Operations.