Linux VPS

virsh commands cheat sheet for managing KVM virtual machines

The virsh commands you need day to day on a KVM host: list, start, stop and autostart VMs, edit XML, attach disks and NICs, change CPU and RAM, consoles, networks and storage pools.

IntermediateKVM hosts2 min read

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 libvirt group connected to qemu:///system.
  • Replace web01 with your VM (domain) name.

VM lifecycle

TaskCommand
List all VMsvirsh list --all
Startvirsh start web01
Graceful shutdownvirsh shutdown web01
Rebootvirsh reboot web01
Force power offvirsh destroy web01 (does not delete)
Pause / resumevirsh suspend web01 / virsh resume web01
Start on host bootvirsh autostart web01
Remove definitionvirsh undefine web01 (add --nvram for UEFI VMs)

Information and consoles

TaskCommand
Basic infovirsh dominfo web01
Disksvirsh domblklist web01
NICs and MACsvirsh domiflist web01
Guest IPsvirsh domifaddr web01 (add --source agent with guest agent)
Serial consolevirsh console web01 (exit with Ctrl+])
VNC displayvirsh vncdisplay web01

Edit configuration

Open the VM's XML in your editor (validated on save):

virsh edit web01

Export or re-import a definition:

virsh dumpxml web01 > web01.xml
virsh define web01.xml

Change CPU and memory

virsh setvcpus web01 4 --config --maximum
virsh setvcpus web01 4 --config
virsh setmaxmem web01 8G --config
virsh setmem web01 8G --config

Changes 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 100G
virsh attach-disk web01 /var/lib/libvirt/images/web01-data.qcow2 vdb --driver qemu --subdriver qcow2 --persistent
virsh attach-interface web01 bridge br0 --model virtio --persistent

Networks and storage pools

TaskCommand
List networksvirsh net-list --all
Start default NAT network at bootvirsh net-autostart default
DHCP leasesvirsh net-dhcp-leases default
List storage poolsvirsh pool-list --all
List volumesvirsh 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.

Next steps

Was this guide helpful?

Still stuck?

Ask MdmHost AI for a quick answer, or open a ticket and our team will take a look. We are here 24/7.