Command-line tool for managing KVM/QEMU virtual machines through the libvirt API, covering VM lifecycle, snapshots, networking, and automation.
Table of Contents#
- Overview
- Installation
- Basic Virsh Commands
- Creating a Virtual Machine
- Advanced Usage
- Troubleshooting
- See Also
- 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-installEnable and start the libvirt daemon:
sudo systemctl enable --now libvirtd3. Basic Virsh Commands#
VM Lifecycle#
| Command | Description |
|---|---|
virsh list --all | List 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#
| Command | Description |
|---|---|
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.iso4.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 20GThis 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>.isoNote: Use
osinfo-query osto list available--os-variantvalues 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.xml5.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.txtExample 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)"
doneTroubleshooting#
| Issue | Cause | Solution |
|---|---|---|
virsh list shows no VMs | Connected to wrong URI or libvirtd not running | Use virsh -c qemu:///system list --all; check systemctl status libvirtd |
| Permission denied on virsh commands | User not in libvirt group | Run sudo usermod -aG libvirt $USER and re-login |
| VM fails to start with disk error | Disk image path wrong or permissions issue | Verify path exists; check ownership is qemu:qemu or libvirt-qemu |
virsh console shows no output | Serial console not configured in guest | Add console=ttyS0,115200n8 to guest kernel parameters |
Network default not starting | dnsmasq conflict or virbr0 missing | Check journalctl -u libvirtd; restart virsh net-start default |
| Cannot connect to remote hypervisor | SSH or TLS not configured | Use virsh -c qemu+ssh://<user>@<host>/system list |