CentOS Scripting Ubuntu

How to create a virtual machine in KVM on the command line

How to create a virtual machine in KVM on the command line

In the previous guide, we looked at how to install the KVM hypervisor on Rocky Linux / AlmaLinux. We went further and demonstrated how you can create and manage virtual machines using the virt-manager. This is a desktop application that provides a GUI interface that allows users to intuitively create and manage virtual machines.

In this guide, we shift focus slightly and walk you through how you can create a virtual machine on command line.

Prerequisites

For this to be successful, you need to have KVM installed on your Linux instance. We already have a guide on how to install KVM on Rocky Linux / AlmaLinux.

Additionally, 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

In this example, 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/james/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.

Here is some output generated from running the command:

Accessing the virtual machine

At this point, the virtual machine is 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.

This opens the TigerVNC graphical viewer as shown.

Managing 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

Conclusion

In this guide, we have shown you how to create and manage guest virtual machines on KVM from the command-line. We hope you have grasped the basic concepts of creating anf managing the state of virtual machines straight from the terminal console.

Similar Posts