KVM Virtualization Installation: Linux
- Omkar Kanase
- May 24, 2024
- 2 min read
Updated: Aug 26, 2024
KVM (Kernel-based Virtual Machine) is an open-source virtualization technology integrated into the Linux kernel. It allows a host machine to run multiple, isolated virtual environments called virtual machines (VMs). Here are the key aspects of KVM.
Setting Up KVM
Step 1: Check CPU virtualization:
To check if your CPU supports hardware virtualization, you can use the egrep command to search for the appropriate CPU flags in the /proc/cpuinfo file.
egrep -c '(vmx|svm)' /proc/cpuinfoIf your CPU supports hardware virtualization, you will see vmx or svm listed in the output. For example:
vmx is the flag for Intel processors. svm is the flag for AMD processors.
vmx
VMX
VMX or
SVM
SVM
SVMVerify KVM support:
sudo kvm-okStep 2: Install KVM Packages
Install KVM, QEMU, libvirt, and virt-manager. These tools will help you create and manage virtual machines:
sudo apt-get update
sudo apt-get install qemu-kvm libvirt-bin bridge-utils virt-managerStep 3: Add Your User to the libvirt Group
To use KVM without root privileges, add your user to the libvirt group.
sudo usermod -aG libvirt $(whoami)
newgrp libvirtStep 4: Start and Enable the libvirt Service
Ensure that the libvirt service is running and enabled to start at boot
sudo systemctl start libvirtd
sudo systemctl enable libvirtdStep 5: Verify Installation
Verify that KVM modules are loaded and the installation is correct.
lsmod | grep kvmStep 6: Create a Virtual Machine
You can create a virtual machine using virt-manager (GUI) or virt-install (CLI).
Using virt-manager (GUI):
Using virt-manager (GUI)Click on the Create a new virtual machine button and follow the wizard to set up your new VM.
Using virt-install (CLI):
virt-install \
--name myvm \
--ram 2048 \
--disk path=/var/lib/libvirt/images/myvm.img,size=20 \
--vcpus 2 \
--os-type linux \
--os-variant ubuntu20.04 \
--network bridge=virbr0 \
--graphics vnc \
--cdrom /path/to/ubuntu-20.04.iso--name: The name of the VM.
--ram: Amount of RAM.
--disk: Disk path and size.
--vcpus: Number of CPU cores.
--os-type and --os-variant: The type and variant of the OS.
--network: Network configuration.
--graphics: Graphics type (VNC in this case).
--cdrom: Path to the installation ISO file.
Step 7: Managing Virtual Machines
You can manage your VMs using virsh (CLI) or virt-manager (GUI).\
Using virsh (CLI):
Start a VM:
virsh start myvmStop a VM:
virsh shutdown myvmList all VMs:
virsh list --allUsing virt-manager (GUI):
Open virt-manager and you can start, stop, and manage VMs through the graphical interface.
By following these steps, you can set up and manage KVM virtualization on your Linux system. KVM provides a powerful and flexible way to run multiple virtual machines on a single physical machine, leveraging the capabilities of your hardware.
Done



Comments