Chuyển đến nội dung chính

Install KVM on Ubuntu 24.04

Duy Tran9 min
Install KVM on Ubuntu 24.04

KVM (Kernel-based Virtual Machine) is a powerful open source virtualization solution for Linux. When combined with a web-based management interface like Cockpit or Kimchi, virtual machine management becomes intuitive and accessible from anywhere. This tutorial will show you how to install KVM on Ubuntu 24.04 and set up a web interface for easy VM management.

System Requirements

  • Ubuntu 24.04 LTS installed
  • Root or sudo access
  • CPU that supports virtualization (Intel VT-x or AMD-V)
  • Minimum 4GB RAM (8GB or more recommended)
  • Enough disk space for VMs

Step 1: Check Hardware Virtualization Support

First, check if your CPU supports virtualization:

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

If the result is 0, virtualization is not supported or has not been enabled in the BIOS. Whichever number is larger 0 all show support.

You can also use:

kvm-ok

If kvm-ok not installed yet, install it first:

sudo apt install cpu-checker
kvm-ok

Expected results: INFO: /dev/kvm exists. KVM acceleration can be used

Step 2: Update System Packages

Update your system to ensure all packages are up to date:

sudo apt update
sudo apt upgrade -y

Step 3: Install KVM and Related Packages

Install KVM along with essential virtualization tools:

sudo apt install -y qemu-kvm libvirt-daemon-system libvirt-clients bridge-utils virtinst virt-manager

Explanation of packages:

  • qemu-kvm: KVM and QEMU main package
  • libvirt-daemon-system: Libvirt daemon for virtualization management
  • libvirt-clients: Client-side utilities for libvirt
  • bridge-utils: Network bridge utilities
  • virtinst: Command line tool to create VM
  • virt-manager: Desktop application to manage VMs (optional)

Step 4: Activate and Start Libvirt Service

Enable the libvirt service to start automatically on boot and start it now:

sudo systemctl enable libvirtd
sudo systemctl start libvirtd

Check the service is running:

sudo systemctl status libvirtd

Step 5: Add Users to Necessary Groups

Add your user to the group libvirt and kvm to manage VM without sudo:

sudo usermod -aG libvirt $USER
sudo usermod -aG kvm $USER

Log out and back in for the group changes to take effect, or run:

newgrp libvirt

Step 6: Verify KVM Settings

Check KVM is installed correctly:

virsh list --all

This command will return an empty VM list (unless you have created some VMs).

Step 7: Install Web Interface - Option 1: Cockpit (Recommended)

Cockpit is a modern, lightweight web interface for managing Linux servers, including KVM virtualization.

Cockpit Settings

sudo apt install -y cockpit cockpit-machines

Cockpit Activation and Start-up

sudo systemctl enable --now cockpit.socket

Access the Cockpit Web Interface

Open a web browser and access:

https://your-server-ip:9090

Or if accessing locally:

https://localhost:9090

Log in with your Ubuntu username and password. Navigate to Virtual Machines in the left sidebar to manage KVM.

Allow Cockpit Through Firewall (if UFW is enabled)

sudo ufw allow 9090/tcp

Step 8: Install Web Interface - Option 2: Kimchi

Kimchi is another web-based virtualization management tool that runs on Wok.

Install Dependencies

sudo apt install -y git python3-pip nginx

Install Wok and Kimchi

sudo apt install -y wok kimchi

If not in the repository, install from source:

# Cài đặt các phụ thuộc
sudo apt install -y python3-cherrypy3 python3-cheetah python3-pam python3-m2crypto gettext

Clone repositories

cd /tmp git clone https://github.com/kimchi-project/wok.git git clone https://github.com/kimchi-project/kimchi.git

Cài đặt Wok

cd wok sudo make install

Cài đặt Kimchi

cd ../kimchi sudo make install

Start Wok Service

sudo systemctl enable wokd
sudo systemctl start wokd

Access Kimchi Web Interface

Open a web browser and access:

https://your-server-ip:8001

Log in with your Ubuntu username and password.

Step 9: Configure Network Bridge (Optional)

To give the VM direct network access, configure the network bridge:

Using Netplan (Default Ubuntu 24.04)

Edit netplan configuration:

sudo nano /etc/netplan/00-installer-config.yaml

Example configuration:

network:
version: 2
ethernets:
enp0s3:
dhcp4: no
bridges:
br0:
interfaces: [enp0s3]
dhcp4: yes
parameters:
stp: false
forward-delay: 0

Replace enp0s3 with your actual network interface name (find with command ip a).

Apply configuration:

sudo netplan apply

Bridge Verification

ip addr show br0
brctl show

Step 10: Create Your First Virtual Machine

Using the Cockpit Web Interface

  1. Navigate to Virtual Machines in Cockpit
  2. Click Create VM
  3. Select installation source (ISO file, URL, or PXE)
  4. Configure VM settings (CPU, RAM, disk)
  5. Start installation

Using the Command Line

Download the ISO file (eg Ubuntu Server):

cd /var/lib/libvirt/images/
sudo wget https://releases.ubuntu.com/24.04/ubuntu-24.04-live-server-amd64.iso

Create a VM:

sudo virt-install 
--name ubuntu-vm
--ram 2048
--vcpus 2
--disk path=/var/lib/libvirt/images/ubuntu-vm.qcow2,size=20
--os-variant ubuntu24.04
--network bridge=virbr0
--graphics vnc,listen=0.0.0.0
--cdrom /var/lib/libvirt/images/ubuntu-24.04-live-server-amd64.iso

Troubleshooting

Problem: Permission Denied

If you get permission errors, make sure your users are in the correct groups:

groups $USER

Should be included libvirt and kvm.

Problem: libvirtd Service Failed

Check logs:

sudo journalctl -u libvirtd

Problem: Cannot Access Web Interface

Check if the service is running:

# Với Cockpit
sudo systemctl status cockpit.socket

Với Kimchi/Wok

sudo systemctl status wokd

Check firewall:

sudo ufw status

Problem: No Network Connection in VM

Verify the default network is working:

virsh net-list --all
sudo virsh net-start default
sudo virsh net-autostart default

Security Issues

  1. Firewall: Open web interface ports only to trusted networks
  2. SSL/TLS: Use the appropriate certificate for the production environment
  3. User access rights: Create a dedicated user with limited permissions
  4. Update: Keep KVM and management tools up to date
  5. Backup: Regularly back up VM configuration and data

Useful Commands

# Liệt kê tất cả VM
virsh list --all

Khởi động VM

virsh start vm-name

Tắt VM

virsh shutdown vm-name

Buộc tắt VM

virsh destroy vm-name

Xóa VM

virsh undefine vm-name

Chỉnh sửa cấu hình VM

virsh edit vm-name

Xem console VM

virsh console vm-name

Liệt kê các mạng

virsh net-list --all

Liệt kê storage pools

virsh pool-list --all

Conclusion

You now have a complete KVM virtualization environment on Ubuntu 24.04 with a web-based management interface. Cockpit provides a modern, easy-to-use interface for virtual machine management, while Kimchi offers more advanced features for professional users. Choose the one that best suits your needs and start creating and managing virtual machines efficiently.

Additional Resources