How To Install KVM On Ubuntu 20.04 / Linux Mint 20
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.
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.
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
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
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 "22.214.171.124","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
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 --
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
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.
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
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
Once you have installed KVM and other tools, it is all set to start our first virtual machine.
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
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.
You can also use Virtual Machine Manager to take a virtual machine console.
Activities » Search for Virtual Machine Manager.
Run virt-manager command in the terminal and bring up the Virtual Machine Manager.
Once it is opened, go to File » New Virtual Machine. The Virtual Machine Manager will start a new virtual machine wizard.
Choose how you would like to install the operating system. Here I chose to install it from ISO image or CD-ROM.
Browse to the location of the ISO image by clicking on Browse and then Browse Local and select the 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.
Configure CPU and memory allocation for the virtual machine.
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.
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 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.
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.
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.