Installing KVM on Debian 12 for Creating Virtual machines

If you want to utilize the spare power of your Debian 12 system using virtualization then KVM will be the best option to have a Type-1 hypervisor for running various virtual machines. KVM is easy to install, however, the management of virtual machines would not be smooth like virtual box for beginners, hence some learning curves will be there, especially when it comes to managing networking. Nevertheless, like any other hypervisor KVM on Debian can also be used to create isolated virtual machines, each running its operating system, independent of the host.

Here in this tutorial, we will learn the commands that can be used to install KVM on Debian 12 Bookworm.

Step 1: Ensure Virtualization support

Ofcourse, we need the virtualization enabled in the system BIOS to run virtual machines using the KVM. But if you don’t know whether it is active or not, you can run the given command in the Debian 12 terminal.

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

The output of the above command must be greater than ‘0’, it can be any number such as 2, 6, etc, if you have that then it means the virtualization is enabled in your system BIOS and we can start the installation of KVM. Whereas, if it is zero then restart the system, open BIOS, and enable the virtualization that will be available in the CPU section.

Step 2: Perform Debian 12 Update

Once you have confirmed that the system supports the virtualization and is already enabled, run the system update and upgrade command. This will ensure our system is up to date and also the package index cache of added repositories is at its latest.

sudo apt update && sudo apt upgrade

Step 3: Installing QEMU-KVM on Debian 12

Qemu-Kvm is the package that will install and enable the Kernel Virtual Machine feature on our Debian Linux system. Apart from that, in this step, we will also install a few other required packages for the management of virtual machines. So, execute the given command in your terminal:

sudo apt install qemu-kvm libvirt-clients libvirt-daemon-system bridge-utils virtinst libvirt-daemon virt-top libguestfs-tools libosinfo-bin qemu-system 

Step 4: Add Your User to the Libvirt and KVM Groups

To manage VMs without root privileges, add your current Ubuntu 24.04 user to the libvirt and kvm groups:

sudo usermod -aG libvirt $(whoami)
sudo usermod -aG kvm $(whoami)

After that enable and start the Libvirt Deamon service

sudo systemctl enable --now libvirtd

Verify the KVM modules are loaded: You will see ‘KVM‘ in the output showing it is loaded…

lsmod | grep kvm

Now, reboot your system:

sudo reboot

Step 5: Create Bridge Network (optional)

Although the KVM comes with a bridge network interface called “virbr0” out of the box, however, that won’t let us access the KVM virtual machine out of the host PC using some other computer in the network because of the IP range assigned by it. Therefore, we will create our full-bridge interface over the physical nic of the host system that will have the IP range of our network.

To list the default NAT network, use:

sudo virsh net-list --all

Now, to find the network adapter or interface used by the host to connect outside world or network:

sudo nmcli device status

In our case, it is ens33 and would be different in our case, so note it down.

DEVICE             TYPE         STATE                                    CONNECTION         
ens33 ethernet connected netplan-ens33
lo loopback connected (externally) lo
virbr0 bridge connected (externally) virbr0
Vnet1 tun unmanaged --
list network connections

First of all, let’s create a bridge interface that we will be able to use with virtual machines. “Br0” is the name we are assigning to our bridge interface.

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

We have created the Biidge interface using the previous command, now, assign which physical ethernet or interface you want to use as a slave from where the Bridge connection will fetch the IP address. As here our physical interface name is ens33, so we will use that, similarly in the given command replaceens33” with your network interface name. Also, we have to give a name to this connection we are creating, here we will call it “KVM Bridge connection 1“, you can assign whatever name you want.

sudo nmcli connection add type ethernet slave-type bridge \
con-name 'KVM Bridge connection 1' ifname ens33 master br0

Enable the “connection.autoconnect-slaves” parameter which determines whether slave connections (e.g., individual network interfaces like Ethernet ports or Wi-Fi adapters) are automatically added to a master connection (e.g., a bridge or bond connection) when they are available.

sudo nmcli connection modify b0 connection.autoconnect-slaves 1

Activate the Bridge Connection:

sudo nmcli connection up br0

Reload Network:

sudo nmcli connection reload

Check the network interface status to confirm the bridge connection up

sudo nmcli device status
Check Bridge connection is up or not

Now, to check the Ip-address of the Bridge connection, use:

ip -brief addr show dev br0
Check bridge connection Ip address range

 

Set Image Directory permission

By default, the images are stored in the “/var/lib/libvirt/images” directory, and the access of it is limited to the “root” user only. To make it accessible for your current user, use the given commands.

To check whether it is accessible by your current use;

ls /var/lib/libvirt/images/

You will have a “permission denied” message.

Now, first, remove the existing permissions for the directory…

sudo setfacl -R -b /var/lib/libvirt/images

Set permission for your current user to access the Images directory

sudo setfacl -R -m u:$USER:rwX /var/lib/libvirt/images

The above will only set permission for the existing files however to apply it for any folders and files we will create in the future, run the given command.

sudo setfacl -m d:u:$USER:rwx /var/lib/libvirt/images

Now, copy whatever ISO images you have and want to create the virtual machine using them to “/var/lib/libvirt/images“.

Step 6. Create Virtual Machines using Virt-Manager GUI

Those who want to use the Virt command line to create virtual machines and run using KVM just follow the link.

Whereas if you are using GUI Debian 12 Desktop can use the Virt-Manager. To give an idea here is a glimpse… Also, if you don’t want to use the VIrt-Manager or run the KVM on some CLI Debian server then use Cockpit to manage KVM virtual machines.

Run Virt-Manager

Go to the Application launcher, search for “Virtual machine manager”  (VVM) and run it. Before it opens, the system will ask to provide the root password. This will allow it to create and manage virtual machines.

Create Virtual Machines:

Click on the PC icon to create a new virtual machine on Virt-Manager using KVM.

Locate ISO Media Volume

Click on the Browse button and then locate the ISO file you want to use for installing an operating system on your virtual machine as a guest OS.

Create new virtual machine on KVM Virt Manager

After that uncheck the box is given for “Automatically detect from the installation media/source” and then click on the Forward button.

Select ISO file KVM Virt Manager

Choose Memory and CPU Settings

Set the amount of virtual memory you want to assign to VM and then the number of CPU cores.

Assign RAM virtual memory

Create Qemu Virtual Disk Image

To install VM guest OS, we need some storage space and for that, we have to create a disk image file. Just enter the amount of hard disk space you want to assign and move forward.

Create Virtual Disk Qemu image

Network

Name your Virtual machine, here. Also, by default, the Virt-manager will use the NAT, however, you can select the Bridge interface, the one you have created by clicking on the Network Selection arrow. If you don’t know about these settings let the default NAT be selected.

image 3

Qemu KVM Virtual Machine on Debian 12

Finally, you will have a booted virtual machine that can be controlled just like any physical computer using a keyboard and mouse.

Install KVM virtual machine Debian 11 Bullseye

Other Articles:

1 thought on “Installing KVM on Debian 12 for Creating Virtual machines”

  1. I am running Debian Bullseye on my Linux platform. Your How To was clear and easy to follow. I installed Windows 1022H2 without any problems.

    I elected to not include Step 4: Create Bridge Network (optional). but installed the default NAT. Unfortunately, although Network IP settings indicated the network was connected. The IPv4 was set at 192.168.144.74. but pinging yshoo.com resulted in ‘Network unreachable’

    I should add that I have successfully installed the same version of Windows 10 in Oracle VM VirtualBox, but am trying to covert my LAN to virt-manager for Linux and Hyper-V for my one windows platform.

    I would appreciate any comments or suggestions.

    Thanks in advance.

    Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.