virt-manager and virsh: Running Virtual Machines on Linux

virt-manager and virsh: Running Virtual Machines on Linux

libvirt stores what your virtual machines are. virt-manager and virsh are how you tell it what to do. They are two interfaces to the same daemon, and most people end up using both.

Getting connected

sudo systemctl enable --now libvirtd
sudo usermod -aG libvirt "$USER"
# log out and back in
virsh list --all

Group membership is read at login, so the usermod does nothing until you start a new session. This is the most common first stumble, and our useradd guide covers why.

system versus session

virsh -c qemu:///system list --all      # the one you want
virsh -c qemu:///session list --all     # unprivileged, limited networking

qemu:///system runs VMs under the privileged daemon with access to bridged networking and shared storage pools. qemu:///session runs them as your user and cannot do bridged networking without extra work.

Set the default so you stop typing it:

echo 'export LIBVIRT_DEFAULT_URI=qemu:///system' >> ~/.bashrc

Creating a VM

The graphical wizard in virt-manager is genuinely the easiest path for a first VM. From the command line:

sudo virt-install \
  --name debian13 \
  --memory 4096 \
  --vcpus 4 \
  --disk path=/var/lib/libvirt/images/debian13.qcow2,size=40,format=qcow2,bus=virtio \
  --network network=default,model=virtio \
  --cdrom /var/lib/libvirt/boot/debian-13.iso \
  --os-variant debian13 \
  --graphics spice

Two details matter more than the rest. bus=virtio and model=virtio are what make disk and network fast, as covered in the KVM, QEMU and libvirt explainer. And --os-variant lets libvirt apply sensible defaults for that guest:

osinfo-query os | grep -i debian

Everyday virsh

virsh list --all
virsh start myvm
virsh shutdown myvm            # ACPI, graceful
virsh destroy myvm             # power off, ungraceful
virsh reboot myvm
virsh autostart myvm           # start at boot
virsh autostart --disable myvm

virsh dominfo myvm
virsh domifaddr myvm           # guest IP addresses
virsh console myvm             # serial console

destroy is badly named and does not delete anything; it is the equivalent of pulling the power cable. Deleting is undefine:

virsh undefine myvm --remove-all-storage

Editing a VM

virsh edit myvm                # opens the XML in $EDITOR, validates on save
virsh dumpxml myvm > myvm.xml  # export
virsh define myvm.xml          # import or update

virsh edit is the honest interface to libvirt. The graphical tool exposes a subset; the XML is everything. Anything virt-manager cannot configure is configurable here.

Snapshots

virsh snapshot-create-as myvm before-upgrade "before the 26.04 upgrade"
virsh snapshot-list myvm
virsh snapshot-revert myvm before-upgrade
virsh snapshot-delete myvm before-upgrade

Snapshots are excellent for “I am about to do something risky inside this VM.” They are not backups: they sit on the same storage, so a failed disk takes the VM and every snapshot with it. Our backup builder covers the real thing.

Note that snapshots on a running VM with memory state are larger and slower to take than disk-only snapshots of a stopped VM.

Consoles

Graphical: virt-manager opens a SPICE or VNC console directly.

Serial: better for servers, and works over SSH. Configure the guest to output to ttyS0, then:

virsh console myvm     # Ctrl-] to exit

On a Linux guest, enabling it is a kernel parameter:

# In the guest, /etc/default/grub
GRUB_CMDLINE_LINUX="console=tty0 console=ttyS0,115200n8"
sudo update-grub

Our GRUB guide covers the file.

Managing remote hosts

virsh -c qemu+ssh://user@server/system list --all

virt-manager takes the same URI when adding a connection, so one window can manage VMs across several machines. This is the point at which libvirt starts being genuinely convenient rather than an abstraction over QEMU.

Frequently Asked Questions

What is the difference between virt-manager and virsh?

Both are clients of libvirt. virt-manager is a graphical application with a VM list, console viewer, and creation wizard. virsh is the command line equivalent, scriptable and usable over SSH. They manage the same virtual machines and changes in one appear in the other.

Why do I get a permission error connecting to libvirt?

Your user is not in the libvirt group. Add yourself with usermod -aG libvirt, then log out and back in, because group membership is only read at login. Until then you can use the system connection with sudo.

What is the difference between the system and session connections?

qemu:///system runs VMs as a privileged daemon with access to real bridged networking and shared storage. qemu:///session runs them as your user with limited networking. System is what you almost always want; session exists for unprivileged desktop use.

How do I connect to a VM console without a graphical desktop?

Use virsh console after configuring a serial console in the guest, which gives you a text session over the terminal. This works over SSH and is how you manage headless VMs on a remote host.

Are libvirt snapshots a backup?

No. A snapshot lives on the same storage as the VM, so it protects against a bad change inside the guest and not against disk failure, corruption, or the host being lost. Take real backups separately.

How do I manage VMs on a remote server?

Both tools connect over SSH using a URI such as qemu+ssh://user@host/system. virt-manager can add remote connections in its interface, so one window manages several hosts.