Create a virtual machine in KVM on the command line

Virt-Manager helps you to create and manage virtual machines. It is basically a desktop application that provides a GUI interface that allows users to intuitively create and manage virtual machines.

Here at LinuxAPT, we regularly help our Customers to perform related KVM Linux system queries.

In this context, we shall look into how you can create a virtual machine on command line.

Before proceeding with this Configuration procedure, you need to have KVM installed on your Linux instance. We already have a guide on how to install KVM on Rocky Linux / AlmaLinux.

Also, ensure that you have a sudo user configured on your system for performing elevated tasks.


How to create a virtual machine in KVM on Command line ?

Here, we are going to create a virtual machine from a Debian 10 ISO image located in the 'Downloads' folder in the home directory. To achieve this, launch the terminal and execute the following command:

$ sudo virt-install --name=debian-10 \
--os-type=Linux \
--os-variant=debian10 \
--vcpu=2 \
--ram=2048 \
--disk path=/var/lib/libvirt/images/debian.img,size=15 \
--graphics vnc,listen=0.0.0.0 \
--location=/home/linuxapt/Downloads/debian-10.1.0-amd64-netinst.iso \
--network bridge:virbr0

Let's briefly expound on the options used:

  • –name : This is the name of the virtual machine, in our case – debian-10.
  • –os-type: The option indicates the operating system.
  • –os-variant: This indicates the flavor or version of your OS. You can get a comprehensive list of all he supported OS variants by running the osinfo-query os command.
  • –vcpu: The number of virtual CPUs allocated to the virtual machine.
  • –ram: The amount of RAM in Megabytes allocated to the virtual machine.
  • –disk path: The option specifies the path of the virtual machine image. The size option specifies the size of the image in GB.
  • –graphics: This specifies the graphical mode used to access the virtual machine. In this case, we have specified vnc as our preferred option.
  • –location: This points to the location of the ISO image used for creating the virtual machine.


How to access the virtual machine ?

After installation, the virtual machine will be up and running. 

But how do you access its graphical interface? 

There are two main ways of going about this. You can use a VNC client or simply use the Virtual machine manager.

To use VNC, you need to install a VNC client such as TigerVNC. To install it, simply run the command:

$ sudo dnf install tigervnc

Next, run the following command to find out which vnc port the virtual machine is listening to:

$ sudo virsh vncdisplay debian-10

Next, use the application manager to launch TigerVNC. Type in your IP address followed by the port.

For example:

VNS Server: 192.168.2.106:70

This will open the TigerVNC graphical viewer.


How to manage the virtual machines on command-line ?

The virsh utility is a command-line tool that is used to manage virtual machines. You can perform various operations as we shall see shortly.

To list currently running virtual machines, run the command:

$ sudo virsh list

To list all the virtual machines, including those that have been powered off use the –all option at the end. Since we have only deployed a since VM, the output will remain the same:

$ sudo virsh list --all

To poweroff a vm, use the syntax:

$ sudo virsh shutdown vm

For example, to poweroff the virtual machine, run:

$ sudo virsh shutdown debian-10

To start the virtual machine execute:

$ sudo virsh start debian-10

To reboot the virtual machine, run:

$ sudo virsh reboot debian-10

To suspend the VM, run the command:

$ sudo virsh suspend debian-10

To resume the vm, execute:

$ sudo virsh resume debian-10

And finally, you can delete to destroy the virtual machine:

$ sudo virsh destroy debian-10


How to Autostart a virtual machine on boot ?

You can take advantage of the virsh command also if you want certain guests,  lets say linuxapt-vm to be started automatically when the host system boots: the syntax it’s, again, very intuitive:

# virsh autostart linuxapt-vm

To disable this option, we run:

# virsh autostart --disable linuxapt-vm


[Need assistance in configuring a Virtual Machine in KVM ? We can help you. ]

This article covers how to create and manage guest virtual machines on KVM from the command-line. In fact, KVM (Kernel based Virtual Machine) is an opensource virtualization technology built for Linux machines. It comprises a kernel module – kvm.ko which provides the core virtualization platform and a processor-specific module ( kvm-intel.ko for Intel processors or kvm-amd.ko for AMD processors ).


How to Install KVM, Qemu, virt-manager & libvirtd daemon ?

To install KVM and associated packages, run the below command:

$ sudo apt install -y qemu qemu-kvm libvirt-daemon libvirt-clients bridge-utils virt-manager

The command installs the following packages:

  • qemu-kvm –  This is the main KVM package that provides virtualization support.
  • libvirt – Includes the libvirtd daemon which supports creation and management of virtual machines.
  • libvirt-client – This package provides the virsh utility used for interacting with virtual machines.
  • virt-install – A utility that helps you to create virtual machines and install OS on those virtual machines from command line.
  • virt-viewer – A utility that displays the graphical view for the virtual machine.

Once installed, we need to confirm if the required kvm modules are loaded. Therefore, run the command:

$ lsmod | grep kvm

Additionally, confirm that the libvirtd daemon is running as follows:

$ sudo systemctl status libvirtd.service

Related Posts