virsh is libvirt's command-line tool and the fastest way to manage KVM virtual machines on a headless server. This virsh commands cheat sheet groups the commands by task so you can find them quickly.
Before you start
- A KVM host with libvirt; see install KVM on Ubuntu.
- Run as root, or as a user in the
libvirtgroup connected toqemu:///system. - Replace
web01with your VM (domain) name.
VM lifecycle
| Task | Command |
|---|---|
| List all VMs | virsh list --all |
| Start | virsh start web01 |
| Graceful shutdown | virsh shutdown web01 |
| Reboot | virsh reboot web01 |
| Force power off | virsh destroy web01 (does not delete) |
| Pause / resume | virsh suspend web01 / virsh resume web01 |
| Start on host boot | virsh autostart web01 |
| Remove definition | virsh undefine web01 (add --nvram for UEFI VMs) |
Information and consoles
| Task | Command |
|---|---|
| Basic info | virsh dominfo web01 |
| Disks | virsh domblklist web01 |
| NICs and MACs | virsh domiflist web01 |
| Guest IPs | virsh domifaddr web01 (add --source agent with guest agent) |
| Serial console | virsh console web01 (exit with Ctrl+]) |
| VNC display | virsh vncdisplay web01 |
Edit configuration
Open the VM's XML in your editor (validated on save):
virsh edit web01Export or re-import a definition:
virsh dumpxml web01 > web01.xmlvirsh define web01.xmlChange CPU and memory
virsh setvcpus web01 4 --config --maximum
virsh setvcpus web01 4 --configvirsh setmaxmem web01 8G --config
virsh setmem web01 8G --configChanges with --config apply at next full boot. For pinning see CPU pinning and overcommit.
Disks and network cards
qemu-img create -f qcow2 /var/lib/libvirt/images/web01-data.qcow2 100Gvirsh attach-disk web01 /var/lib/libvirt/images/web01-data.qcow2 vdb --driver qemu --subdriver qcow2 --persistentvirsh attach-interface web01 bridge br0 --model virtio --persistentNetworks and storage pools
| Task | Command |
|---|---|
| List networks | virsh net-list --all |
| Start default NAT network at boot | virsh net-autostart default |
| DHCP leases | virsh net-dhcp-leases default |
| List storage pools | virsh pool-list --all |
| List volumes | virsh vol-list default |
Snapshots
See KVM snapshots with virsh for snapshot-create-as, snapshot-revert and snapshot-delete.
Frequently asked questions
Why does virsh list show nothing?
You are probably connected to qemu:///session. Use sudo virsh or virsh -c qemu:///system.
shutdown does nothing?
The guest ignores ACPI. Install acpid or the QEMU guest agent, or use virsh destroy as a last resort.
Can I manage a remote host?
Yes: virsh -c qemu+ssh://root@kvm1.example.com/system list.
