🎁 New User? Get 20% off your first purchase with code NEWUSER20 Register Now β†’
Menu

Categories

virsh Command

Advanced Virtualization man(1)

KVM/libvirt virtual machine management CLI

πŸ‘ 1 views πŸ“… Updated: Mar 16, 2026
SYNTAX
virsh [OPTIONS] COMMAND [ARGS]

What Does virsh Do?

The virsh command is the primary command-line tool for managing virtual machines through the libvirt virtualization API. It controls KVM/QEMU virtual machines β€” creating, starting, stopping, migrating, and monitoring VMs on Linux servers.

libvirt provides a unified management layer for multiple hypervisors (KVM, QEMU, Xen, LXC), and virsh is its CLI frontend. While GUI tools like virt-manager exist, virsh is essential for headless server management, scripting, and automation.

virsh manages the complete VM lifecycle: defining VMs from XML configurations, managing virtual networks and storage pools, creating snapshots, performing live migrations between hosts, and monitoring resource usage. It supports both local and remote connections, allowing management of VMs across multiple hypervisor hosts from a single workstation.

For enterprise Linux environments, KVM with libvirt (managed via virsh) is the standard virtualization stack, powering everything from development environments to production infrastructure. Red Hat Virtualization, oVirt, and OpenStack all use libvirt as their VM management layer.

Options & Flags

OptionDescriptionExample
list --all List all VMs (running and stopped) virsh list --all
start VM Start a virtual machine virsh start webserver
shutdown VM Gracefully shut down a VM virsh shutdown webserver
destroy VM Force stop a VM (like pulling power) virsh destroy webserver
console VM Connect to VM serial console virsh console webserver
define XML Define a VM from XML configuration virsh define /etc/libvirt/qemu/webserver.xml
snapshot-create-as Create a named snapshot virsh snapshot-create-as webserver snap1 "Before upgrade"
snapshot-revert Revert VM to a snapshot virsh snapshot-revert webserver snap1
dominfo VM Show VM information (CPU, RAM, state) virsh dominfo webserver
migrate Live migrate VM to another host virsh migrate --live webserver qemu+ssh://host2/system

Practical Examples

#1 List all virtual machines

Show all defined VMs with state (running, shut off, paused).
$ virsh list --all
Output: Id Name State\n--------------------------\n 1 webserver running\n - database shut off\n - testvm shut off

#2 Start and connect to VM

Start a VM and attach to its serial console. Exit console with Ctrl+] (right bracket).
$ virsh start webserver && virsh console webserver

#3 Create VM from command line

Create and install a new VM using virt-install (companion to virsh). Starts installation automatically.
$ virt-install --name testvm --ram 2048 --vcpus 2 --disk size=20 --os-variant ubuntu22.04 --cdrom /iso/ubuntu-22.04.iso --network bridge=br0

#4 Create a snapshot

Create a named snapshot before making changes. Revert if something goes wrong.
$ virsh snapshot-create-as webserver "pre-upgrade" "Before kernel upgrade" && virsh snapshot-list webserver

#5 Revert to snapshot

Roll back VM to the pre-upgrade snapshot. VM state is restored to the exact point of the snapshot.
$ virsh snapshot-revert webserver pre-upgrade && virsh start webserver

#6 Resize VM resources

Set VM to 4GB RAM and 4 vCPUs. --config applies on next boot. Use --live for hotplug (if supported).
$ virsh setmem webserver 4G --config && virsh setvcpus webserver 4 --config

#7 Live migrate VM

Live migrate a running VM to another host with zero downtime. Requires shared storage or block migration.
$ virsh migrate --live --persistent webserver qemu+ssh://host2/system

#8 Clone a VM

Create a full clone of an existing VM including disk. The clone gets a new MAC address and UUID.
$ virt-clone --original webserver --name webserver-clone --auto-clone

Tips & Best Practices

destroy is NOT delete: virsh destroy only force-stops the VM (like pulling the power cord). It does NOT delete the VM or its disk. Use virsh undefine --remove-all-storage to actually delete a VM.
Use autostart for critical VMs: Enable auto-start: virsh autostart webserver. The VM will start automatically after host reboot.
XML configuration: virsh dumpxml VM shows the complete XML config. Edit with virsh edit VM (opens in $EDITOR). XML defines CPU, memory, disks, NICs, and all VM hardware.
Monitor VM performance: Use virsh domstats VM for detailed performance metrics, or virt-top for a top-like view of all VMs resource usage.

Frequently Asked Questions

How do I create a VM on Linux?
Install KVM: apt install qemu-kvm libvirt-daemon-system. Create VM: virt-install --name myvm --ram 2048 --vcpus 2 --disk size=20 --cdrom /path/to.iso --os-variant generic
How do I take a snapshot of a VM?
Create: virsh snapshot-create-as VM_NAME snap_name "description". List: virsh snapshot-list VM_NAME. Revert: virsh snapshot-revert VM_NAME snap_name.
What is the difference between virsh shutdown and virsh destroy?
shutdown sends ACPI power-off signal (graceful, like pressing power button). destroy immediately kills the VM process (like pulling the power cord). Always try shutdown first.
How do I access a VM console?
virsh console VM_NAME for serial console. For graphical console: virt-viewer VM_NAME or connect via VNC/SPICE. Exit serial console with Ctrl+].
Can I resize a running VM?
CPU hotplug: virsh setvcpus VM N --live (if VM supports it). Memory hotplug: virsh setmem VM SIZE --live. Disk expand: virsh blockresize VM /path SIZE. Not all guest OS support hotplug.

Master Linux with Professional eBooks

Curated IT eBooks covering Linux, DevOps, Cloud, and more

Browse Books β†’