virsh — Virtualization Shell for Managing VMs
virsh
is a command-line interface for managing virtual machines via libvirt, typically used with KVM, QEMU, and other virtualization technologies. It supports operations like VM lifecycle management, storage, network, snapshots, and resource monitoring.
Key Features
Lifecycle Management: Start, stop, suspend, and destroy VMs.
Persistent and Transient VMs: Manage both runtime and saved configurations.
Storage and Networking: Control virtual disks, volumes, and virtual networks.
Snapshots: Create and revert to VM snapshots.
Remote Management: Manage VMs on remote hosts over SSH or TLS.
Interactive and Non-interactive Use: Run commands or enter an interactive shell.
Basic Syntax
virsh [options] <command> [args]
Example:
virsh start vm-name
Common and Essential Subcommands
1. VM Lifecycle Management
List VMs (running or all)
virsh list # Only running virsh list --all # All domains (running + shut off)
Start / Shutdown / Destroy / Reboot
virsh start vm-name virsh shutdown vm-name virsh destroy vm-name # Force off (like pulling power plug) virsh reboot vm-name
Suspend / Resume
virsh suspend vm-name virsh resume vm-name
Autostart on Boot
virsh autostart vm-name virsh autostart --disable vm-name
2. Domain and Resource Info
Get VM Information
virsh dominfo vm-name virsh domuuid vm-name virsh domstate vm-name
CPU and Memory Statistics
virsh dommemstat vm-name virsh domstats vm-name
3. VM Configuration and XML
Dump or Edit XML Configuration
virsh dumpxml vm-name # Show XML virsh edit vm-name # Edit XML with $EDITOR
Define or Undefine Domains
virsh define vm.xml # Create from XML virsh undefine vm-name # Remove config only
4. Snapshot Management
Create and Manage Snapshots
virsh snapshot-create-as vm-name snap1 "Snapshot description" virsh snapshot-list vm-name virsh snapshot-revert vm-name --snapshotname snap1 virsh snapshot-delete vm-name --snapshotname snap1
5. Console and Access
Connect to Serial Console
virsh console vm-name
To exit:
Ctrl+]
6. Storage Management
List, Create, and Delete Volumes
virsh vol-list default virsh vol-create-as default vol1.img 10G virsh vol-delete vol1.img --pool default
Pool Management
virsh pool-list virsh pool-start default virsh pool-refresh default
7. Network Management
List, Start, and Define Networks
virsh net-list --all virsh net-start default virsh net-destroy default virsh net-define net.xml virsh net-autostart default
8. Remote and Interactive Mode
Connect to Remote Host
virsh -c qemu+ssh://user@remote/system
Enter Interactive Shell
virsh
Example Use Cases
Start a Virtual Machine
virsh start ubuntu-vm
Check Running VMs
virsh list
Create a Snapshot Before Upgrade
virsh snapshot-create-as ubuntu-vm pre-upgrade "Before system upgrade"
Attach a CD-ROM ISO Image
virsh attach-disk ubuntu-vm /isos/ubuntu.iso hdc --type cdrom --mode readonly
Conclusion
virsh
is an indispensable tool for Linux-based virtualization environments using libvirt. It provides complete lifecycle control, resource introspection, and management capabilities for virtual machines, networks, and storage — both locally and remotely. It’s highly scriptable and favored for automation and infrastructure orchestration workflows.
Last updated