KVM stands for Kernel based Virtual Machine, KVM is a free and open source type 2 hypervisor which is installed on top of Linux like distributions. Once the KVM is installed on your system then it becomes the virtualization server which allows us to run multiple virtual machines at same time. KVM requires either Intel processor with VT-x (virtualization technology extension) or AMD processor with AMD-V (AMD64 Virtualization Extension).
In this article we will learn how to install and configure KVM on Debian 10 system, I am assuming Debian 10 is already installed on system / server. Let’s jump into installation steps.
Step:1) Check Whether Virtualization Extension is enabled or not
Login to your Debian 10 system, open the terminal and run the below command,
pkumar@kvm-debian10:~$ egrep -c '(vmx|svm)' /proc/cpuinfo 2 pkumar@kvm-debian10:~$
If output of above command is more than zero then we can say Virtualization technology enabled on your system. If the output is zero then we must restart the system, go to bios settings and then enable VT-x (Virtualization Technology Extension) for Intel processor and AMD-V for AMD processor.
Run the below command to verify whether your processor is Intel / AMD and support hardware virtualization,
pkumar@kvm-debian10:~$ grep -E --color '(vmx|svm)' /proc/cpuinfo
if the output contains vmx then you have a Intel based processor and svm confirms that it is AMD processor.
Step:2) Install QEMU-KVM & Libvirt packages along with virt-manager
kvm, qemu, libvirt and virt-manager packages are available in the default repositories of Debian 10, run the beneath apt command to install these packages,
pkumar@kvm-debian10:~$ sudo apt install qemu-kvm libvirt-clients libvirt-daemon-system bridge-utils virtinst libvirt-daemon virt-manager -y
Once above packages are installed successfully then libvirtd service will be started automatically, run the below systemctl command to verify the status
pkumar@kvm-debian10:~$ sudo systemctl status libvirtd.service
Step:3) Start default network and add vhost_net module
Run the below virsh command to list available networks for kvm VMs
pkumar@kvm-debian10:~$ sudo virsh net-list --all Name State Autostart Persistent ---------------------------------------------- default inactive no yes pkumar@kvm-debian10:~$
As we can see in above output, default network is inactive so to make it active and auto-restart across the reboot by running the following commands,
pkumar@kvm-debian10:~$ sudo virsh net-start default Network default started pkumar@kvm-debian10:~$ sudo virsh net-autostart default Network default marked as autostarted pkumar@kvm-debian10:~$
If you want to offload the mechanism of “virtio-net” and want to improve the performance of KVM VMs then add “vhost_net” kernel module on your system using the beneath command,
pkumar@kvm-debian10:~$ sudo modprobe vhost_net kumar@kvm-debian10:~$ echo "vhost_net" | sudo tee -a /etc/modules vhost_net pkumar@kvm-debian10:~$ pkumar@kvm-debian10:~$ lsmod | grep vhost vhost_net 24576 0 vhost 49152 1 vhost_net tap 28672 1 vhost_net tun 49152 2 vhost_net pkumar@kvm-debian10:~$
Note: If you want a normal user to use virsh commands then add that user to libvirt and libvirt-qemu group using the following commands
pkumar@kvm-debian10:~$ sudo adduser pkumar libvirt pkumar@kvm-debian10:~$ sudo adduser pkumar libvirt-qemu
To refresh or reload group membership run the followings,
pkumar@kvm-debian10:~$ newgrp libvirt pkumar@kvm-debian10:~$ newgrp libvirt-qemu
Step:4) Create Linux Bridge(br0) for KVM VMs
When we install KVM then it automatically creates a bridge with name “virbr0“, this is generally used for all test environments but if you wish to access your KVM VMs over the network then create Linux bridge which will be attached to physical nic ( or lan card) of your system.
To create a bridge in Debian 10 , edit the network configuration file “/etc/network/interfaces” and add the following contents,
In my case ens33 is the physical nic and br0 is the Linux bridge and have assigned same ip address of ens33 to bridge br0. ( Also make sure to remove IP address from ens33). Replace the interface name, bridge name and IP details as per your setup.
pkumar@kvm-debian10:~$ sudo vi /etc/network/interfaces .............. #Primary network interface(ens33) auto ens33 iface ens33 inet manual #Configure bridge and give it a static ip auto br0 iface br0 inet static address 192.168.29.150 netmask 255.255.255.0 network 192.168.29.1 broadcast 192.168.29.255 gateway 192.168.29.1 bridge_ports ens33 bridge_stp off bridge_fd 0 bridge_maxwait 0 dns-nameservers 8.8.8.8 ...............
save and exit the file
To make the above network changes into the effect we have to reboot the system , so run the below reboot command,
pkumar@kvm-debian10:~$ sudo reboot
Once the system is back online after reboot, we will see that bridge br0 will come up, run the following command to confirm,
pkumar@kvm-debian10:~$ ip a s br0
Step:5) Create Virtual Machines either via Virt-Manager (GUI) or virt-install (command line)
There are two ways to create virtual machines in KVM , first one is via virt-manager GUI tool and 2nd way is via command line using virt-install utility.
Creating Virtual Machines using Virt-Manager (GUI)
Access the virt-manager GUI tool from Desktop environment
Click on Virtual Machine Manager icon
Click on monitor icon to create a new VM
Browse and choose the ISO file and then click on forward, in my case i have already uploaded RHEL 8 iso on Debian 10 system, so i will be using the same iso for installing operating system,
In the next window, specify the RAM and CPU for the VM
click on Forward to proceed further,
Click on Forward and in next screen specify the VM name and its network
Click on Finish to proceed with operating system installation,
Follow the screen instructions and complete the installation.
Creating VM via command line using virt-install
We can also create vms from command line using virt-install utility. Example is shown below,
pkumar@kvm-debian10:~$ sudo virt-install --name ubuntu-vm --os-type linux --os-variant ubuntu18.04 --ram 1024 --vcpu 1 --disk path=/var/lib/libvirt/images/ubuntu-vm.qcow2,size=10 --graphics vnc,listen=0.0.0.0 --noautoconsole --hvm --cdrom /home/pkumar/ubuntu-18.04-live-server-amd64.iso --boot cdrom,hd Starting install... Domain installation still in progress. You can reconnect to the console to complete the installation process. pkumar@kvm-debian10:~$
As we can see in above output, we can connect to console of ubuntu vm either via VNC console or virt-manager to finish the OS installation.
Run the following commands to find VNC display port for ubuntu-vm,
pkumar@kvm-debian10:~$ sudo virsh list --all Id Name State --------------------------- 3 ubuntu-vm running 4 rhel8-vm running pkumar@kvm-debian10:~$ pkumar@kvm-debian10:~$ sudo virsh vncdisplay ubuntu-vm :0 pkumar@kvm-debian10:~$
Now access remote desktop viewer on your Debian 10 system and connect to VM console using IP address of your system and vnc port as “0”, example is demonstrated below,
Click on Connect,
Follow the screen instructions and complete Ubuntu 18.04 server Installations. That’s all from this article, please do share your feedback and comments in case these steps help you to setup KVM on your Debian 10 system.
This is very technical information on Debian 10 buster
Thank for so detailed steps for KVM set up. 🙂
after
sudo virsh net-start default
i got this error:
error: Cannot check dnsmasq binary /usr/sbin/dnsmasq: file or directory not found
I canno instal dnsmasq. systemd-resolved ist blocking port 53. so what can i do?
Thanks Pradeep! This is just what I was looking for.
I followed your step i only see LXC in virt-manager. I am using debian buster
‘https://ibb.co/FzLYXYM’
Hmm, I have wlan0 (no eth0) and under KDE worked NetworkManager.
When I use network config like write above – wlan0 is not worked and no accessible in NetworkManager.
How to rewrite configs to get worked br0, wlan0, and NetworkManager?
Wireless interfaces do NOT support bridging and will not work with KVM/QEMU. They DO seem to work with VirtualBox in Windows tho. 🙁
Huge Thanks for this article. Very helpful to setup my first KVM VM. Main thing I was looking for is how to access the VNC console of a remote server to complete the installation procedure of VM.
Thank you 🙂