Command-line tool for managing KVM/QEMU virtual machines through the libvirt API, covering VM lifecycle, snapshots, networking, and automation.

Table of Contents#

  1. Overview
  2. Installation
  3. Basic Virsh Commands
  4. Creating a Virtual Machine
  5. Advanced Usage
  6. Troubleshooting
  7. See Also
  8. Sources

1. Overview#

Virsh is the primary command-line interface for managing virtual machines through the libvirt library. It provides full control over VM lifecycle, configuration, snapshots, networking, and storage without requiring a graphical interface.

Virsh supports a wide variety of hypervisors, including QEMU, KVM, Xen, and more. It operates by connecting to the libvirt daemon (libvirtd) and can manage both local and remote hypervisors.


2. Installation#

To use virsh, install libvirt and its dependencies:

# Debian/Ubuntu
sudo apt install libvirt-daemon-system libvirt-clients qemu-kvm

# CentOS/RHEL/Fedora
sudo dnf install libvirt libvirt-client libguestfs-tools virt-install qemu-kvm

# Arch Linux
sudo pacman -S libvirt qemu-full virt-install

Enable and start the libvirt daemon:

sudo systemctl enable --now libvirtd

3. Basic Virsh Commands#

VM Lifecycle#

CommandDescription
virsh list --allList all VMs (running and stopped)
virsh start <vm-name>Start a VM
virsh shutdown <vm-name>Graceful shutdown (sends ACPI signal)
virsh destroy <vm-name>Force stop (equivalent to pulling the power)
virsh reboot <vm-name>Reboot a VM
virsh suspend <vm-name>Pause a VM (freeze in memory)
virsh resume <vm-name>Resume a paused VM
virsh autostart <vm-name>Enable autostart on host boot

Snapshots#

CommandDescription
virsh snapshot-create-as <vm-name> <snapshot-name>Create a named snapshot
virsh snapshot-list <vm-name>List all snapshots for a VM
virsh snapshot-revert <vm-name> <snapshot-name>Revert to a snapshot
virsh snapshot-delete <vm-name> <snapshot-name>Delete a snapshot

4. Creating a Virtual Machine#

4.1 Preparing the Virtual Machine Image#

Download an OS image. For example, Arch Linux:

wget https://geo.mirror.pkgbuild.com/iso/latest/archlinux-x86_64.iso

4.2 Creating a Qcow2 Disk for the VM#

Create a qcow2 disk image for the virtual machine:

qemu-img create -f qcow2 /var/lib/libvirt/images/<vm-name>.qcow2 20G

This creates a 20 GB thin-provisioned disk (actual size grows as data is written).

4.3 Defining the Virtual Machine#

Use virt-install to create and define a VM:

virt-install \
    --name=<vm-name> \
    --memory=2048 \
    --disk path=/var/lib/libvirt/images/<vm-name>.qcow2,size=20,bus=virtio,format=qcow2 \
    --vcpus=2 \
    --os-variant=archlinux \
    --network bridge=virbr0 \
    --graphics none \
    --console pty,target_type=serial \
    --cdrom=/path/to/<iso-file>.iso

Note: Use osinfo-query os to list available --os-variant values for your system.

4.4 Starting the Virtual Machine#

After defining the VM, start it and connect to its console:

virsh start <vm-name>
virsh console <vm-name>

5. Advanced Usage#

5.1 Managing Network Interfaces#

# List all virtual networks
virsh net-list --all

# Start a network
virsh net-start <network-name>

# Set a network to autostart
virsh net-autostart <network-name>

# Show network details
virsh net-info <network-name>

5.2 Configuring Virtual Machine Settings#

Modify VM settings by editing its XML configuration:

# Dump current configuration to a file
virsh dumpxml <vm-name> > vm-config.xml

# Edit inline (opens in $EDITOR)
virsh edit <vm-name>

# Redefine from a modified XML file
virsh define vm-config.xml

5.3 Automating Tasks with Virsh#

Virsh integrates well into shell scripts. Example to start a list of VMs from a file:

while read -r vm; do
    virsh start "$vm"
done < vm-list.txt

Example to snapshot all running VMs:

virsh list --name | while read -r vm; do
    [ -n "$vm" ] && virsh snapshot-create-as "$vm" "auto-$(date +%Y%m%d)"
done

Troubleshooting#

IssueCauseSolution
virsh list shows no VMsConnected to wrong URI or libvirtd not runningUse virsh -c qemu:///system list --all; check systemctl status libvirtd
Permission denied on virsh commandsUser not in libvirt groupRun sudo usermod -aG libvirt $USER and re-login
VM fails to start with disk errorDisk image path wrong or permissions issueVerify path exists; check ownership is qemu:qemu or libvirt-qemu
virsh console shows no outputSerial console not configured in guestAdd console=ttyS0,115200n8 to guest kernel parameters
Network default not startingdnsmasq conflict or virbr0 missingCheck journalctl -u libvirtd; restart virsh net-start default
Cannot connect to remote hypervisorSSH or TLS not configuredUse virsh -c qemu+ssh://<user>@<host>/system list

See Also#

Sources#