Kernel-based Virtual Machine (KVM) is the leading open source virtualization technology for linux. It is available natively on all linux distributions and turns underlying physical servers into hypervisors. For those of you who are familiar with VMWare virtualization and Microsoft Hyper-V. Then the concept of KVM should not be new to you. Due to the wide availability of linux hosting available at affordable price today. Learning KVM and using nested KVM is a good and cheap way to start building your lab. The great thing about KVM is that you can run Windows Server on it as well and most of the affordable VPS hosting provider out there allows you to do nested virtualization as well. However if you are planning to do this production environment you need to be mindful of its performance and stability.
You will need to install vnc or xrdp if you want to install the GUI of the virtual machine or run virt-manager. You can find the step by step guide on how to install vnc in the below link.
https://dracocybersecurity.com/install-tightvncserver-on-ubuntu-20-04/
Now let’s get started. Issue the following command on your Ubuntu machine. This will install the necessary packages to run your kvm. (Install virt-manager if you are planning to manage the Virtual Machine using GUI)
sudo apt -y install bridge-utils cpu-checker libvirt-clients libvirt-daemon qemu qemu-kvm virt-manager
Next issue the following command to check if you can run KVM on your linux machine. It also shows whether you can run nested KVM as some hosted VPS may not allow you to run nested KVM on their underlying KVM.
sudo kvm-ok
You want to add your admin user (if it is not using root) to the libvirt and kvm group
sudo adduser <username> kvm
sudo adduser <username> libvirt
Check the status of the libvirtd to check if the installation is done successfully and running.
sudo systemctl status libvirtd
If the virtualized daemon is not active, you can activate if and make persistent using the following commdn.
sudo systemctl enable –now libvirtd
Next make a folder to hold your image. (Make sure you have set the necessary permission if you are using a different admin user.
sudo mkdir -p /<Enter the Diretory to store the image>/images
Below is an example of how to install another Ubuntu Client in KVM on Ubuntu. We will be using vnc to connect to the client after provisioning to do the installation and configuration. Do note if you are copying the below syntax to a text editor you want to make sure that — is correct.
sudo virt-install \
–name ubuntu3-guest \
–os-variant ubuntu20.04 \
–disk path=/mnt/vol2/kvm/images/ubuntu3.img,size=16 \
–vcpus 2 \
–ram 2048 \
–location http://ftp.ubuntu.com/ubuntu/dists/focal/main/installer-amd64/ \
–network bridge=virbr0,model=virtio \
–graphics vnc,listen=localhost,port=5901
Once you see the waiting for installation you can launch your vnc viewer to connect to the client.
You can follow my youtube video for the installation steps as well.
If you get stuck and cleaning the file when you reboot you can issue ctl alt (option for Mac) F1 to F6 to switch to another tty console and try startx.
Try startx at the command prompt. If that doesn’t work you can reinstall the Desktop
sudo apt install ubuntu-desktop
Once that is done you can enter startx to launch the GUI or just reboot the Client VM.