Managing Virtual Machines with Virsh
Learn to manage Linux VMs with virsh. A guide to essential commands to start, stop, clone, and monitor virtual machines from the command line.
Overview
Use Virsh to easily create, start, stop, and monitor your Linux virtual machines from. Virsh is lightweight, scriptable, and ideal for system administrators and developers who want fast, efficient VM management. In this guide, we’ll explore essential Virsh commands and the best practices to help you streamline virtualization on Linux.
Best Practices for Using Virsh Safely
- Prefer graceful shutdowns over forced stops.
- Avoid the destroy command unless the VM is completely unresponsive.
- Double-check VM names before running start/stop commands.
- Back up important VMs before performing risky operations like cloning.
- Run commands as root or with sudo to ensure you have proper privileges.
With Virsh, you can manage and optimize your AlmaLinux virtual machines directly from the command line, turning your server into a streamlined and powerful virtualization platform.
Parameters / Features
The following command provides a quick reference list with short descriptions of each available subcommand
virsh --helpListing Virtual Machines
- Show currently running virtual machines:
sudo virsh list
- Show all virtual machines, regardless if they are currently running or stopped:
sudo virsh list --allViewing Virtual Machine Details
To view information about a specific VM such as its ID, CPU allocation, and memory — run the following command:
sudo virsh dominfo <VM_name>Starting and Gracefully Stopping Virtual Machines
- Start a VM by running the command:
sudo virsh start <VM_name>- To gracefully shut down your VM and have the OS to close any open apps and services properly, run:
sudo virsh shutdown <VM_name>Forcefully Stopping and Destroying Virtual Machines
The following commands should only be run with caution as there is a potential for data loss. Make sure to back up or save any important data before running these commands.
- Forcefully stop a VM if it is unresponsive by running:
sudo virsh stop <VM_name>- Trigger an immediate ungraceful shutdown and stop the specified guest virtual machine with the command:
sudo virsh destroy <VM_name>Suspending and Resuming Virtual Machines
- Suspend
sudo virsh suspend <VM_name> - Resume
sudo virsh resume <VM_name>Saving and Restoring VM States
- Save a VM’s current state to a file:
sudo virsh save <VM_name> <file_name>- Restore from that saved state:
sudo virsh restore <file_name>Cloning Virtual Machines
- Quickly clone a VM using virt-clone. The –auto-clone option automatically generates a new name and file path:
sudo virt-clone --original <VM_name> --auto-cloneAccessing Console Logs
- To view a VM’s console output:
virsh console-log <VM_name>