How to Manage KVM Virtual Machines on Ubuntu Server Using virsh

Kernel-based Virtual Machine (KVM) is the most widely used virtualization technology on Linux servers. On Ubuntu Server, KVM is managed using libvirt, and the primary command-line tool provided by libvirt is virsh.

This guide explains how to manage KVM virtual machines on Ubuntu Server using virsh. It covers real-world examples such as starting and stopping virtual machines, enabling auto-start after reboot, accessing the VM console, monitoring resources, managing disks and networks, and troubleshooting common issues. This article is suitable for production environments.


What Is KVM Virtualization on Ubuntu Server

Overview of KVM and libvirt

KVM (Kernel-based Virtual Machine) is a Linux kernel module that allows the Linux kernel to act as a hypervisor. libvirt is a management layer that provides a unified API and tools to manage virtual machines, storage, and networking.

Why virsh Is Used for KVM Management

virsh is the official CLI tool for libvirt. It is lightweight, scriptable, and ideal for headless Ubuntu servers where a graphical interface is not required.


Prerequisites for Managing KVM Using virsh

Required Packages on Ubuntu Server

sudo apt update
sudo apt install qemu-kvm libvirt-daemon-system libvirt-clients bridge-utils -y

Verify libvirtd Service Status

sudo systemctl status libvirtd

How to List KVM Virtual Machines Using virsh

List Running Virtual Machines

virsh list

List All Virtual Machines

virsh list --all

How to Start, Stop, and Reboot KVM Virtual Machines

Start a Virtual Machine

virsh start vicidial

Gracefully Shutdown a Virtual Machine

virsh shutdown vicidial

Force Stop a Virtual Machine

virsh destroy vicidial

Reboot a Virtual Machine

virsh reboot vicidial

How to Enable Auto Start for KVM Virtual Machines on Boot

Enable Auto Start After Ubuntu Reboot

virsh autostart vicidial

Verify Auto Start Status

virsh dominfo vicidial | grep Autostart

Disable Auto Start

virsh autostart --disable vicidial

How to Access KVM Virtual Machine Console Using virsh

Connect to the VM Console

virsh console vicidial

To exit the console, press:

Ctrl + ]

How to Monitor CPU and Memory Usage of KVM Virtual Machines

Check Resource Usage

virsh domstats vicidial

Live Monitoring

watch virsh domstats vicidial

How to Edit KVM Virtual Machine Configuration

Edit VM XML Configuration

virsh edit vicidial

This allows you to modify CPU, memory, disks, network interfaces, and boot options. For major changes, always shut down the VM first.


How to Manage Disks and Network Interfaces in KVM

Attach a Disk to a Virtual Machine

virsh attach-disk vicidial /data/disk2.qcow2 vdb --persistent

Detach a Disk

virsh detach-disk vicidial vdb --persistent

Attach a Network Interface

virsh attach-interface vicidial bridge br0 --model virtio --persistent

How to Take and Restore Snapshots in KVM

Create a Snapshot

virsh snapshot-create-as vicidial snap_before_update

List Snapshots

virsh snapshot-list vicidial

Restore a Snapshot

virsh snapshot-revert vicidial snap_before_update

Troubleshooting KVM Virtual Machines on Ubuntu Server

Check libvirtd Logs

journalctl -u libvirtd

Check VM-Specific Logs

/var/log/libvirt/qemu/vicidial.log

Best Practices for Managing KVM Virtual Machines in Production

  • Enable auto-start for critical virtual machines
  • Use graceful shutdown instead of force stop
  • Take snapshots before upgrades or major changes
  • Monitor disk usage under /var/lib/libvirt
  • Use fast storage (SSD or NVMe) for VM disks

Conclusion

Managing KVM virtual machines on Ubuntu Server using virsh provides full control, automation capabilities, and production-level reliability. Whether you are running Linux servers, Windows servers, or call-center platforms, mastering virsh is essential for efficient KVM administration.

This entry was posted in KVM and tagged , , , , , , , , , , , , , , , . Bookmark the permalink.