How To Install KVM On Ubuntu 20.04 / Linux Mint 20

THIS DOCUMENT IS ALSO AVAILABLE FOR

KVM is a hypervisor module in the Linux Kernel that helps you run multiple virtual machines on a Linux machine with the help of virtualization extensions.

KVM supports the wide variety of guest operating systems such as Linux, Windows, Unix family OSes, and much more. You can manage virtual machines using the command line or available graphical tools.

Virt-Manager (Virtual Machine Manager) is the graphical application for managing KVM based virtual machines. It supports creating, starting, stopping, and managing virtual machines, as well as the migration of virtual machines between KVM hosts.

The steps mentioned here afterward will work on Linux Mint as well.

Prerequisites

VT Support

KVM will work only if the system CPU has the support of hardware virtualization, either Intel VT or AMD-V.

To find out whether your CPU supports VT features, run the following command.

egrep -c '(vmx|svm)' /proc/cpuinfo

Output:

4

If the above command returns nonzero, then your hardware supports VT else it does not.

Create Network Bridge For KVM

The bridged network is a dedicated network interface to a virtual machine that helps virtual machines to connect outside the host machine.

Let us list the available network connections.

nmcli connection show

Output:

NAME                UUID                                  TYPE      DEVICE
Wired connection 1  fbbdd6f9-0970-354e-8693-ff8050a85c77  ethernet  enp0s3

Now, we will create a virtual bridge network br0 with the help of physical interface enp0s3.

sudo nmcli con add ifname br0 type bridge con-name br0

sudo nmcli con add type bridge-slave ifname enp0s3 master br0

Next, we will assign the IP address of the physical interface to the bridge interface as the Bridge network interface will act as the primary network interface of your host system.

sudo nmcli con mod br0 ipv4.addresses 192.168.0.10/24

sudo nmcli con mod br0 ipv4.gateway 192.168.0.1

sudo nmcli con mod br0 ipv4.dns "8.8.8.8","192.168.0.1"

sudo nmcli con mod br0 ipv4.method manual

KVM requires a few additional network settings. So, set them.

sudo nmcli con modify br0 bridge.stp no

sudo nmcli con modify br0 bridge.forward-delay 0

Disable the physical interface and enable the network bridge.

sudo nmcli con down "Wired connection 1" && sudo nmcli con up br0
Run the above command in the system terminal as you may lose SSH sessions when running them remotely.

Finally, check the network connections.

sudo nmcli con show
Output:

NAME                 UUID                                  TYPE      DEVICE
br0                  ee117099-4935-4dde-a1f5-4981b0d9585e  bridge    br0
bridge-slave-enp0s3  492b5c81-e59d-4150-9b24-0348cd0dd87c  ethernet  enp0s3
Wired connection 1   fbbdd6f9-0970-354e-8693-ff8050a85c77  ethernet  --

Install KVM

Update the repository cache.

sudo apt update

Install the below packages for KVM set up.

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

Create Virtual Machine

User Permission

Ensure users are part of the libvirt group to create virtual machines. Use id command to check if the user is part of the libvirt group.

id raj

Output:

uid=1000(raj) gid=1000(raj) groups=1000(raj),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),120(lpadmin),131(lxd),132(sambashare),134(libvirt)

If the user is not part of libvirt group, follow anyone the steps to fix it.

To add an existing user to libvirt group.

sudo usermod -aG libvirt raj

OR

If you are creating a new user, use the below command.

sudo useradd -m -d /home/user_name -s /bin/bash -G libvirt user_name

Create VM

Once you have installed KVM and other tools, it is all set to start our first virtual machine.

Command Line

virt-install --name=centos_8_2 --ram=2048 --vcpus=1 --os-type=linux --os-variant=centos8 --cdrom=/tmp/CentOS-8.2.2004-x86_64-minimal.iso --network bridge=br0  --graphics vnc,listen=0.0.0.0 --noautoconsole --disk path=/var/lib/libvirt/images/centos_8_2.qcow2,size=10

Output:

Starting install...
Allocating 'centos_8_2.qcow2'                                                                                                                                         |  10 GB  00:00:00
Domain installation still in progress. You can reconnect to
the console to complete the installation process.

You can use the VNC viewer to connect to the virtual machine console and complete the OS installation.

192.168.0.10:5900
Access Virtual Machine Console with VNC Viewer
Access Virtual Machine Console with VNC Viewer

You can also use Virtual Machine Manager to take a virtual machine console.

Graphical Mode

Activities » Search for Virtual Machine Manager.

OR

Run virt-manager command in the terminal and bring up the Virtual Machine Manager.

Start Virtual Machine Manager
Start Virtual Machine Manager

Once it is opened, go to File » New Virtual Machine. The Virtual Machine Manager will start a new virtual machine wizard.

Create New Virtual Machine
Create New Virtual Machine

Choose how you would like to install the operating system. Here I chose to install it from ISO image or CD-ROM.

OS Installation Method
OS Installation Method

Browse to the location of the ISO image by clicking on Browse and then Browse Local and select the ISO image.

Browse for ISO Image
Browse for ISO Image
Select ISO Image
Select ISO Image

The new virtual machine wizard will try to detect the operating system based on your OS image. If not, search for an operating system version by typing the OS version.

OS Version
OS Version

Configure CPU and memory allocation for the virtual machine.

CPU & Memory Information
CPU & Memory Information

Here, mention the amount of storage you want to assign to a virtual machine. Additionally, you can change the location of the disk image as per your requirement.

Storage Information
Storage Information

On this page, you will get a summary of the virtual machine.

Click the Network selection and ensure the bridged adaptor we created earlier is chosen for the virtual machine. Finally, click on Finish.

Virtual Machine Summary & Network Selection
Virtual Machine Summary & Network Selection

Virtual Machine Manager will begin the creation of a virtual machine depends on our input. Once the VM is created, Virtual Machine Manager will start the console for OS installation.

Virtual Machine Running on KVM
Virtual Machine Running on KVM

Manage Virtual Machine

With the help of Virtual Machine Manager, you can perform a virtual machine’s life cycle actions such as start, power off, reset, clone, and migration by right-clicking on the selected virtual machine.

Manage Virtual Machine State
Manage Virtual Machine State

You can view and manage a virtual machine by clicking the info icon in the virtual machine console. Here you can add, remove, and modify devices connected to a virtual machine.

Manage Virtual Machine Configuration
Manage Virtual Machine Configuration

Conclusion

That’s All. I hope you have learned how to install KVM on Ubuntu 20.04 / Linux Mint 20 and create a virtual machine with Virtual Machine Manager and command-line mode.

Prev Post
Next Post
comments powered by Disqus