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.
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:
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.
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
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
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:
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