In this guide, you’ll learn how to manage virtual machines (VMs) on Linux using QEMU-KVM and Libvirt’sDocumentation Index
Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
Use this file to discover all available pages before exploring further.
virsh CLI. By the end, you will be able to install dependencies, define VM domains via XML, control VM lifecycle, adjust resources, and clean up definitions—all from the command line. This workflow is fundamental for self-hosted virtualization and supports major cloud platforms like AWS, DigitalOcean, and Google Cloud.
Example: A single Linux server with 64 CPU cores and 1 TiB RAM can host dozens of isolated VMs, each with its own dedicated vCPU and memory allocation.
1. Installing Dependencies
Install QEMU, KVM, and Libvirt tools:- qemu-kvm: Hardware-accelerated virtualization
- libvirt: API and utilities for managing VMs
You may also need to start and enable the libvirtd service:
2. Defining a Virtual Machine Domain
Libvirt uses XML to describe VM configurations (called domains). Create a file namedtestmachine.xml:
- Name: TestMachine
- Memory: 1 GiB
- vCPUs: 1
- OS: Hardware VM on x86_64
3. Defining and Listing Domains
Register the domain with Libvirt:| Id | Name | State |
|---|---|---|
| - | TestMachine | shut off |
Omit
--all to list only running domains.4. VM Lifecycle Management
Use the following commands to control the VM. See the quick reference table below.| Action | Command |
|---|---|
| Start | virsh start TestMachine |
| Graceful reboot | virsh reboot TestMachine |
| Forced reset | virsh reset TestMachine |
| Graceful shutdown | virsh shutdown TestMachine |
| Forced power-off | virsh destroy TestMachine |
resetsimulates pressing the reset button.
destroycuts power immediately—like unplugging the VM.
5. Deleting a Domain
Remove the domain definition only:--remove-all-storage will also delete associated disk images:6. Autostart Configuration
Enable the VM to start on host boot:7. Inspecting VM Details
Retrieve detailed VM information:8. Modifying VM Resources
Adjusting vCPUs
Increase maximum vCPUs:Adjusting Memory
Set maximum memory:9. Starting and Verifying Changes
Start the VM with updated resources:Quick Reference: Common virsh Commands
| Command | Description |
|---|---|
| virsh define <file>.xml | Define a new VM domain |
| virsh start <domain> | Start a VM |
| virsh shutdown <domain> | Graceful shutdown |
| virsh destroy <domain> | Forced power off |
| virsh reboot <domain> | Reboot VM |
| virsh reset <domain> | Forced reset |
| virsh list [—all] | List VMs (active/inactive) |
| virsh undefine <domain> [—remove-all-storage] | Remove VM definition |
| virsh autostart <domain> [—disable] | Enable/disable autostart |
| virsh dominfo <domain> | Show domain information |
| virsh setvcpus <domain> <count> [—config] [—maximum] | Adjust vCPUs |
| virsh setmaxmem <domain> <size> [—config] | Adjust max memory |