Where to start First steps track · 15 stages
Linux Labs

KVM and libvirt

4 min Ubuntu 24.04 · libvirt · KVM

Virtual machines on Linux — managed with virsh, networks, storage, console.

Cheat sheet

8 commands Printable version
  • virsh list --all all VMs
  • virsh start vm start it
  • virsh shutdown vm graceful shutdown (ACPI)
  • virsh console vm serial console (Ctrl+] to exit)
  • virsh net-list --all virtual networks
  • virsh domiflist vm a VM's interfaces
  • virsh snapshot-create-as vm snap a snapshot
  • virsh -c qemu+ssh://user@host/system list a remote host

Overview

KVM is the hypervisor built into the Linux kernel; libvirt is the management layer on top of it, and virsh is its CLI. This stack also runs linux-labs itself (the ephemeral lab VMs). The libvirtd daemon runs under systemd.

bash
virsh list --all                  # all domains (VMs): running and stopped
virsh nodeinfo                     # host resources
egrep -c 'vmx|svm' /proc/cpuinfo   # hardware virtualization support (>0 = present)

Connecting to a remote host: virsh -c qemu+ssh://user@host/system ....

Recipes

2

Managing VMs

bash
virsh start vm1                    # start it
virsh shutdown vm1                 # graceful shutdown (ACPI)
virsh destroy vm1                  # hard power-off (= pulling the plug)
virsh reboot vm1
virsh console vm1                  # serial console (exit: Ctrl+])
virsh dominfo vm1                  # state, memory, vCPUs
virsh autostart vm1                # start automatically with the host
virsh undefine vm1                 # remove the definition (the disk stays!)

Networks and storage

bash
virsh net-list --all               # virtual networks
virsh net-dumpxml default          # the network's XML
virsh net-start default
virsh net-autostart default

virsh pool-list                    # storage pools
virsh vol-list default             # volumes (VM disks)
virsh domiflist vm1                # a VM's network interfaces (MAC, bridge)
virsh dumpxml vm1                  # the VM's full definition (XML)

Snapshots:

bash
virsh snapshot-create-as vm1 snap1
virsh snapshot-list vm1
virsh snapshot-revert vm1 snap1

Troubleshooting

4

virsh: failed to connect to the hypervisor

libvirtd isn't running (systemctl status libvirtd), or you lack permissions — the user must be in the libvirt group. The system instance needs the URI qemu:///system.

The VM won't start

virsh start vm1 prints an error; check journalctl -u libvirtd and the log at /var/log/libvirt/qemu/vm1.log for details. Common causes: missing disk file, a port already in use, not enough memory.

The VM has no network

The virtual network isn't running: virsh net-list --all → net-start. Check the bridge and the firewall on the host.

Can't get into the VM

virsh console vm1 (needs serial enabled in the guest OS) — exit with Ctrl+]. Or use SSH if the VM has networking.

See also

  • qemu-img — Creating and maintaining qcow2 disks — overlays, conversion, resizing.
  • systemctl — Managing systemd services — start/stop/restart/status/enable/reload.
  • ssh-client — Connecting over SSH, copying with scp/rsync, ~/.ssh/config, port forwarding.
Loading…