Introduction
Virsh (Virtual Shell) is the command-line interface for managing virtual machines and hypervisors through libvirt. This cheatsheet provides essential commands for daily VM management tasks, from basic operations like listing and controlling VMs to advanced display configuration and remote viewing setup. Whether you’re a system administrator managing multiple virtual environments or a developer working with local VMs, these commands will streamline your virtualization workflow.
VM Listing and Information
List Virtual Machines
# List all running VMs virsh list # List all VMs (running and stopped) virsh list --all # List only inactive/stopped VMs virsh list --inactive # List VMs with additional details virsh list --all --title
VM Information
# Show VM configuration virsh dominfo <vm-name> # Show VM XML configuration virsh dumpxml <vm-name> # Show VM state virsh domstate <vm-name> # Show VM ID virsh domid <vm-name> # Show VM UUID virsh domuuid <vm-name>
VM Power Management
Starting VMs
# Start a VM virsh start <vm-name> # Start VM and connect to console virsh start <vm-name> --console # Auto-start VM on host boot virsh autostart <vm-name> # Disable auto-start virsh autostart --disable <vm-name>
Stopping VMs
# Graceful shutdown (ACPI signal) virsh shutdown <vm-name> # Force shutdown (equivalent to pulling power) virsh destroy <vm-name> # Reboot VM virsh reboot <vm-name> # Reset VM (hard reset) virsh reset <vm-name>
Pause and Resume
# Pause/suspend VM (freeze state) virsh suspend <vm-name> # Resume paused VM virsh resume <vm-name> # Save VM state to file (hibernation) virsh save <vm-name> <filename> # Restore VM from saved state virsh restore <filename>
Display and Console Management
Check Display Protocol
# Show display information (VNC/SPICE details) virsh domdisplay <vm-name> # Show VNC display details virsh domdisplay <vm-name> --type vnc # Show SPICE display details virsh domdisplay <vm-name> --type spice # List all graphics devices virsh domxml <vm-name> | grep -A5 '<graphics'
Console Access
# Connect to VM console (text-based) virsh console <vm-name> # Connect to VM console and escape with Ctrl+] virsh console <vm-name> --safe
Remote Viewing with virt-viewer and remote-viewer
Basic Connections
# Connect using virt-viewer (auto-detects protocol) virt-viewer <vm-name> # Connect to remote hypervisor virt-viewer -c qemu+ssh://user@host/system <vm-name> # Connect using remote-viewer with URI remote-viewer $(virsh domdisplay <vm-name>)
VNC Connections
# Direct VNC connection via remote-viewer remote-viewer vnc://localhost:5900 # VNC with authentication remote-viewer vnc://localhost:5900 # Secure VNC over SSH tunnel ssh -L 5900:localhost:5900 user@remote-host remote-viewer vnc://localhost:5900
SPICE Connections
# SPICE connection via remote-viewer remote-viewer spice://localhost:5900 # SPICE with TLS encryption remote-viewer spice://hostname:5901 # Secure SPICE over SSH tunnel ssh -L 5900:localhost:5900 user@remote-host remote-viewer spice://localhost:5900 # SPICE with password file remote-viewer --spice-password-file=/path/to/password spice://host:5901 # Full SPICE URI from virsh remote-viewer $(virsh domdisplay <vm-name> --type spice)
Advanced Display Configuration
Modify Display Settings
# Edit VM configuration virsh edit <vm-name> # Change graphics type (in XML): # For VNC: # <graphics type='vnc' port='5900' autoport='yes'/> # For SPICE: # <graphics type='spice' port='5901' autoport='yes'/>
Display Port Management
# Find used VNC ports virsh list --all | xargs -I {} virsh domdisplay {} 2>/dev/null | grep vnc # Check listening ports netstat -tlnp | grep :590[0-9] # Show SPICE ports virsh domdisplay <vm-name> | grep spice
Networking Commands
# List virtual networks virsh net-list --all # Show network configuration virsh net-dumpxml <network-name> # Start virtual network virsh net-start <network-name> # Stop virtual network virsh net-destroy <network-name>
Storage Management
# List storage pools virsh pool-list --all # List volumes in a pool virsh vol-list <pool-name> # Show volume information virsh vol-info <volume-name> <pool-name> # Create volume virsh vol-create-as <pool-name> <volume-name> <size>
Snapshots
# Create snapshot virsh snapshot-create-as <vm-name> <snapshot-name> "Description" # List snapshots virsh snapshot-list <vm-name> # Revert to snapshot virsh snapshot-revert <vm-name> <snapshot-name> # Delete snapshot virsh snapshot-delete <vm-name> <snapshot-name>
Useful Troubleshooting Commands
# Check hypervisor connection virsh uri # Show hypervisor version virsh version # Show node information virsh nodeinfo # Monitor VM resources virsh domstats <vm-name> # Show VM block devices virsh domblklist <vm-name> # Show VM network interfaces virsh domiflist <vm-name>
Final Thoughts
Mastering virsh commands significantly improves your efficiency when working with virtual machines. The combination of virsh for management and virt-viewer/remote-viewer for graphical access provides a powerful toolkit for both local and remote virtualization scenarios. Remember that display protocols (VNC vs SPICE) each have their strengths: VNC offers broader compatibility and simplicity, while SPICE provides better performance, audio support, and advanced features like clipboard sharing and USB redirection.
For production environments, always prefer graceful shutdowns over forced destruction, implement regular snapshots for critical VMs, and use secure connection methods when accessing remote systems. Keep your libvirt and QEMU packages updated to benefit from the latest features and security improvements.
The virsh domdisplay
command is particularly valuable as it provides the exact connection string needed for remote viewers, eliminating guesswork about ports and protocols. Combine this with SSH tunneling for secure remote access to your virtual infrastructure.