Skip to main content
In this guide, you’ll learn how to manage virtual machines (VMs) on Linux using QEMU-KVM and Libvirt’s 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 named testmachine.xml:
Paste this minimal configuration:
This sets:
  • Name: TestMachine
  • Memory: 1 GiB
  • vCPUs: 1
  • OS: Hardware VM on x86_64
Save and exit.

3. Defining and Listing Domains

Register the domain with Libvirt:
Expected output:
List all domains, including inactive:
Sample:
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.
reset simulates pressing the reset button.
destroy cuts 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:
Disable autostart:

7. Inspecting VM Details

Retrieve detailed VM information:
Sample output:

8. Modifying VM Resources

Adjusting vCPUs

Increase maximum vCPUs:
Set vCPUs for next boot:

Adjusting Memory

Set maximum memory:
Verify changes:

9. Starting and Verifying Changes

Start the VM with updated resources:
Confirm the running state:
Expected fields:

Quick Reference: Common virsh Commands


References

Watch Video