For the system administrators and DevOps engineers using KVM as their virtualized environment, controlling virtual machines (VMs) effectively can be challenging without the right tools. This is where virsh
, a command-line interface provided by Libvirt, shines as a powerful solution.
virsh
is a powerful CLI tool that allows you to manage VMs running on KVM (Kernel-based Virtual Machine). While GUI-based management tools exist, they often lack the flexibility, scripting capabilities, and remote management options that virsh
offers. With virsh
, you can automate tasks, troubleshoot issues faster, and gain fine-grained control over your virtualized environment.
Why virsh?
- Centralized Control:Â
virsh
 provides consolidation of virtual machines, network, and storage management into a single command-line interface, thus streamlining operations across hypervisors supported by Libvirt. - Scriptability and Automation: Being a command-line tool,Â
virsh
 is ideal for integration into automation and scripted workflows, enabling repetitive tasks to be executed reliably and efficiently. - Remote Management: UseÂ
virsh
 to connect to remote servers, making it a practical choice for managing headless environments or virtualized data centers. - Advanced Troubleshooting: Access detailed diagnostic information for VMs, networks, and storage pools to identify and resolve issues promptly.
By taking advantage of virsh
, you gain the ability to manage virtual machines on KVM more effectively without the need for a graphical interface.
In this section of the ebook, we guide you through practical examples that demonstrate how to harness the full potential of virsh
 when working with the KVM hypervisor. From foundational tasks to advanced management techniques, these examples are designed to help you get started quickly and effectively.
Mastering KVM Virtualization - The Ultimate eBook
From home labs to production clouds - master KVM Host management, automating KVM administration using Terraform, Vagrant, and cloud automation. This eBook will enable you to build scalable virtual infrastructure that works whether you're learning at home or deploying enterprise solutions. Get your full copy today
1. Host and Hypervisor Management
Check virsh version:
sudo virsh version
Connect to a local KVM hypervisor session with virsh
:
sudo virsh connect qemu:///system
If you want to initiate a read-only connection, append with -readonly
.
Connecting to remote KVM. This is covered in details in another chapter:
sudo virsh -c qemu+ssh://${username}@${remote_kvm_host_ip}/system
Get the hypervisor canonical URI:
$ sudo virsh uri qemu:///system
Get information about the hypervisor:
sudo virsh sysinfo
Print node information:
sudo virsh nodeinfo
Print hypervisor hostname:
sudo virsh hostname
Check the host capabilities:
sudo virsh domcapabilities
List active networks:
sudo virsh net-list
Get a list of dhcp leases for all network interfaces connected to a given virtual network:
sudo virsh net-dhcp-leases default
2. List and Display VM Information
In the context of KVM, a domain refers to virtual machine (VM) managed using Libvirt API. Each domain represent a guest operating system with its own allocated hardware resources such as memory, CPU, storage, and network interfaces. Each domain in KVM operate independently, ensuring there is security and proper resource allocation and management.
In all examples, <vm-name>
should be replaced with the domain name, ID, orUUID.
List all Virtual Machines (running and stopped):
sudo virsh list --all
List only running Virtual Machines:
sudo virsh list
Show detailed information about a VM:
sudo virsh dominfo <vm-name>
Shows the current state of a VM:
sudo virsh domstate <vm-name>
List block devices attached to a VM:
sudo virsh domblklist <vm-name>
Get block device size information for a specific domain/guest:
# List block devices sudo virsh domblklist --details <vm-name> # Get block device size sudo virsh domblkinfo <vm-name> <disktarget> --human
List network interface used for the Virtual Machine:
sudo virsh domiflist <vm-name>
Get domain ID for a VM:
sudo virsh domid <vm-name>
Get Universally Unique Identifier (UUID) for a VM:
virsh domuuid <vm-name>
View resource usage statistics for a domain:
sudo virsh domblkstat --human <vm-name>
Display detailed information about virtual CPU of a VM:
sudo virsh vcpuinfo <vm-name>
Display VM block device statistics:
sudo virsh domblkstat <vm-name>
Display VM information about network device:
sudo virsh domifstat <vm-name> <interface-device>
Get VM display connection URI:
sudo domdisplay <vm-name>
Print VNC display address:
sudo virsh vncdisplay <vm-name>
3. Managing VM State
Start a Virtual Machine:
sudo virsh start <vm-name>
Enable automatic start of VM (when KVM host is rebooted):
sudo virsh autostart <vm-name>
Disable VM autostart:
sudo virsh autostart --disable <vm-name>
Gracefully shut down a Virtual Machine:
sudo virsh shutdown <vm-name>
Forcefully power off a Virtual Machine:
sudo virsh destroy <vm-name>
Reboot a Virtual Machine:
sudo virsh reboot <vm-name>
Suspends a running VM, saving its state to disk:
sudo virsh suspend <vm-name>
Resume a suspended VM:
sudo virsh resume <vm-name>
Reset a Virtual Machine:
sudo virsh reset <vm-name>
4. Modify VM Configurations
Rename a virtual machine:
sudo virsh domrename <current-name> <new-name>
Connect to VM Console:
sudo virsh console <vm-name>
Edit VM XML configuration:
sudo virsh edit <vm-name>
Change VM CPU allocation
- Shutdown VM:
sudo virsh shutdown <vm-name>
- Confirm it’s powered off:
sudo virsh list --all
- Set the maximum vCPUs:
virsh setvcpus <vm-name> <max-vcpus> --maximum --config
- Change vCPUs to value less or equals to maximum value set:
sudo virsh dominfo <vm-name> sudo virsh setvcpus <vm-name> <vcpu-count> --config
- Start the VM after CPU changes:
sudo virsh start <vm-name>
Change VM Memory allocation
- Poweroff the VM
sudo virsh shutdown <vm-name>
- Check current allocation
sudo virsh dominfo <vm-name>
- Change maximum memory limit
sudo virsh setmaxmem <vm-name> <max-memory> --config
- Change memory allocation
sudo virsh setmem <vm-name> <memory> --config
**Output domain information in XML:
sudo virsh dumpxml <vm-name> # To save to file sudo virsh dumpxml vm-name > file.xml
Define a new domain from XML file:
sudo virsh define domain-config-file.xml
Delete VM without removing volumes:
# First stop the VM sudo virsh destroy <vm-name> # Then undefine from Libvirt sudo virsh undefine <vm-name>
Delete a Virtual Machine with the attached volumes:
sudo virsh undefine --remove-all-storage <vm-name>
Save VM current state to a file:
sudo virsh save <vm-name> <filename>
Configure VM to use custom bridge by editing XML file:
# Shutdown VM sudo virsh shutdown <vm-name> # Open VM for edit in XML sudo virsh edit <vm-name> # Find network section <interface type='network'> <source network=''/> <model type='virtio'/> </interface> # Change to: <interface type='bridge'> <source bridge='<bridge-name>'/> <model type='virtio'/> </interface> # Start VM sudo virsh start <vm-name>
Attach network interface to a VM (live):
sudo virsh attach-interface <vm-name> --type bridge \ --source <bridge-name> --model virtio --live
Insert ISO file into the VM:
sudo virsh attach-disk <vm-name> <iso-path> hdc --type cdrom --mode readonly
Remove CD ROM from the VM:
sudo virsh attach-disk <vm-name> "" hdc --type cdrom --mode readonly
Watch out form more guide.