How To Manage KVM Virtual Machines From The Host Terminal?


Virtualization means creating virtual instances of resources such as storage devices, network resources, etc. Kernel-based virtual machine or KVM is a virtualization module in the Linux Kernel which turns the Linux kernel into a hypervisor.

Using KVM, one can run multiple virtual machines. Each virtual machine has private virtualized hardware: a network card, disk, graphics adapter, etc. We can run, stop, delete, and manage virtual machines from the host OS terminal by using a tool called virsh.

Host Operating System

Before we start using virsh commands, We should understand the host and guest OS in the context of virtualization. Host refers to the bare metal machine or server and the OS you install on that physical machine is known as Host OS. This OS can also be called as Hypervisor.

Guest Operating System

The virtual machines are created on the top of the host machine. The operating system installed on the virtual machine is known as the guest operating system.

How to manage guests with virsh?

Virsh is a command-line interface tool that can be used to manage guests and hypervisor. It is useful for advanced Linux administrators interested in automating some aspects of managing their virtual machines.

Connect to your hypervisor

A hypervisor could be local or remote. You can manage virtual machines running on a local hypervisor, use the following command to connect to hypervisor –

virsh connect qemu:///system

List all virtual machines

To display the list of all the virtual machines in your system use the following command –

virsh list --all

Create a virtual machine

We will use virt-install tool to create virtual machines from the terminal. This tool can be used in both interactive and non-interactive modes. For example –

# virt-install \
 -n CentOS8 \
 --description "Test VM with CentOS" \
 --os-type=Linux \
 --os-variant=centos8 \
 --ram=1500 \
 --vcpus=2 \
 --disk path=/var/lib/libvirt/images/centos8.img,bus=virtio,size=10 \
 --graphics none \
 --cdrom /home/lalit/Downloads/CentOS-8-x86_64-Minimal-1908 \
 --network bridge:br0

Where,

-n – Name of your virtual machine

--description – Some valid description about VM. For example – Web server, Application server, etc.

--os-type – Type of operating system for example – Linux, Unix, Solaris, window, etc.

--os-variant – Version of distribution for example – Ubuntu 20.04 LTS, Centos 7.0, etc.

--ram – Amount of RAM in MB for virtual machine

--vcpus – The total number of virtual CPUs for VM.

--disk path – It is the path where VM image file is stored, Size is mentioned in GB

--graphics – This option instructs virt-install to use a text console on VM serial port instead of a graphical VNC window. If you have the Xmanager set up, then you can ignore this parameter.

--cdrom – Indicates the location of the installation image.

--network bridge – This example uses a bridged adapter br0. If you want to use NAT, then use it as given below –

--network network = virbr0 where virbr0 is the name of the virtual network

Start a virtual machine

You can start fedora28 VM by using the following command –

virsh start fedora28

Shutdown a virtual machine

To shutdown fedora28 VM, use the following command –

virsh shutdown fedora28

Reboot a virtual machine

To reboot fedora28 VM, use the following command –

virsh reboot fedora28

List all running virtual machines

You can display the list of running virtual machines by using –

virsh list

Connect to VM console

To connect to the console of fedora28 virtual machine use the following command-

virsh console fedora28

Press ctrl+] to exit from the console.

Display virtual machine information

To display the details of a VM, use the following command –

virsh dominfo fedora28

Delete a virtual machine

If you want to delete a VM then you can use the following command –

First terminate it, if it is running –

virsh destroy fedora28

And then undefine it by using –

virsh undefine fedora28

For a complete description of virsh commands see its manual page by using –

man virsh

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.