Categories: TutorialsUbuntu

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.

Sponsored

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.

Sponsored

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.

Ubuntu Server Admin

Recent Posts

Canonical Releases Ubuntu 25.04 Plucky Puffin

The latest interim release of Ubuntu introduces “devpacks” for popular frameworks like Spring, along with…

2 days ago

Ubuntu 25.04 (Plucky Puffin) Released

Ubuntu 25.04, codenamed “Plucky Puffin”, is here. This release continues Ubuntu’s proud tradition of integrating…

3 days ago

Extended Security Maintenance for Ubuntu 20.04 (Focal Fossa) begins May 29, 2025

Ubuntu released its 20.04 (Focal Fossa) release 5 years ago, on March 23, 2020. As…

3 days ago

Ubuntu 20.04 LTS End Of Life – activate ESM to keep your fleet of devices secure and operational

Focal Fossa will reach the End of Standard Support in May 2025, also known as…

4 days ago

Ubuntu MATE 25.04 Release Notes

Ubuntu MATE 25.04 is ready to soar! 🪽 Celebrating our 10th anniversary as an official…

4 days ago

Ubuntu Weekly Newsletter Issue 887

Welcome to the Ubuntu Weekly Newsletter, Issue 887 for the week of April 6 –…

5 days ago