|
| 1 | +--- |
| 2 | +title: Installing Virtual Machines with QEMU KVM on Ubuntu 24 |
| 3 | +image: /assets/img/default-banner.jpg |
| 4 | +author: jack |
| 5 | +date: 2025-05-27 20:55:00 +0800 |
| 6 | +categories: [blog, linux] |
| 7 | +tags: [linux] |
| 8 | +math: false |
| 9 | +pin: false |
| 10 | +--- |
| 11 | + |
| 12 | +QEMU/KVM, short for Quick Emulator, is an open-source virtualization solution that combines the emulation capabilities of QEMU with the hardware acceleration of KVM (Kernel-based Virtual Machine). QEMU is a Type 2 hypervisor that emulates virtual machines via software, but when paired with KVM—a Type 1 hypervisor—it can achieve near-native performance. Deploying QEMU/KVM on Ubuntu 24 allows you to efficiently create and manage virtual machines, making it a powerful and stable platform for development, testing, and learning. |
| 13 | + |
| 14 | +## Installing QEMU/KVM |
| 15 | + |
| 16 | +Enable the libvirtd virtualization service: |
| 17 | + |
| 18 | +``` |
| 19 | +$ sudo systemctl enable libvirtd.service |
| 20 | +$ sudo systemctl start libvirtd.service |
| 21 | +``` |
| 22 | + |
| 23 | +Install QEMU/KVM and related packages, then reboot: |
| 24 | + |
| 25 | +``` |
| 26 | +$ sudo apt update |
| 27 | +$ sudo apt install qemu-kvm virt-manager bridge-utils |
| 28 | +$ sudo reboot now |
| 29 | +``` |
| 30 | + |
| 31 | + |
| 32 | +To allow your user account to operate virtual machines without root privileges, you need to add your user to the following permission groups: |
| 33 | +- `libvirt`: Allows interaction with the libvirt daemon to create, start, pause, and manage VMs. |
| 34 | +- `libvirt-qemu` or `libvirt-kvm`: Grants access to QEMU/KVM backend, including VM images and virtual networks. |
| 35 | + |
| 36 | +``` |
| 37 | +$ groups |
| 38 | +lin adm cdrom sudo dip plugdev users lpadmin libvirt |
| 39 | +$ sudo useradd -g $USER libvirt |
| 40 | +$ sudo useradd -g $USER libvirt-kvm |
| 41 | +``` |
| 42 | + |
| 43 | +## Creating a Virtual Machine |
| 44 | + |
| 45 | +Download the ISO image for the Linux distribution you want to install. In this example, we use Ubuntu 24 Server. |
| 46 | + |
| 47 | +Launch the Virtual Machine Manager by running `virt-manager` in the terminal or finding it in your system’s application menu. |
| 48 | + |
| 49 | + |
| 50 | + |
| 51 | +Click **File -> New Virtual Machine** in the top-left corner. |
| 52 | + |
| 53 | +In the new window, choose **Local install media (ISO image or CDROM)**, then click **Forward**. |
| 54 | + |
| 55 | +Select the ISO image you downloaded earlier and click **Forward**. |
| 56 | + |
| 57 | +Set the virtual machine name, memory size, number of CPU cores, and other settings. |
| 58 | + |
| 59 | +After completing the configuration, the virtual machine will automatically boot and enter the Linux installation process. |
| 60 | + |
| 61 | +## Configuring NAT DHCP to Assign IP Addresses to Virtual Machines |
| 62 | + |
| 63 | +By default, QEMU/KVM uses NAT mode for networking, and will automatically assign a random IP address (e.g., 192.168.XXX.XXX) to each virtual machine. If you want to assign a **static IP address** to a VM, you can do so by editing the `/etc/libvirt/qemu/networks/default.xml` file. |
| 64 | + |
| 65 | +First, check the MAC addresses of each virtual machine. For example, assume there are four virtual machines: |
| 66 | + |
| 67 | + |
| 68 | + |
| 69 | +``` |
| 70 | +$ virsh dumpxml vm1 | grep 'mac address' |
| 71 | + <mac address='52:54:00:e4:de:fb'/> |
| 72 | +$ virsh dumpxml vm2 | grep 'mac address' |
| 73 | + <mac address='52:54:00:14:67:fa'/> |
| 74 | +$ virsh dumpxml vm3 | grep 'mac address' |
| 75 | + <mac address='52:54:00:39:aa:1d'/> |
| 76 | +$ virsh dumpxml vm4 | grep 'mac address' |
| 77 | + <mac address='52:54:00:95:aa:41'/> |
| 78 | +``` |
| 79 | + |
| 80 | +Check the current NAT network (usually named `default`): |
| 81 | + |
| 82 | +``` |
| 83 | +$ virsh net-list |
| 84 | + Name State Autostart Persistent |
| 85 | +-------------------------------------------- |
| 86 | + default active yes yes |
| 87 | +``` |
| 88 | + |
| 89 | +Edit the `default.xml` configuration file: |
| 90 | + |
| 91 | +``` |
| 92 | +$ virsh net-edit default |
| 93 | +``` |
| 94 | + |
| 95 | +Or in the GUI: go to **Edit -> Preferences -> Enable XML editing**, then use **Edit -> Connection Details -> Virtual Networks -> XML** to modify the configuration. |
| 96 | + |
| 97 | + |
| 98 | + |
| 99 | +The original `<dhcp>` block should look like this: |
| 100 | + |
| 101 | +```xml |
| 102 | +<dhcp> |
| 103 | + <range start="192.168.122.100" end="192.168.122.254"/> |
| 104 | +</dhcp> |
| 105 | +``` |
| 106 | + |
| 107 | +Add <host> entries below the <range> section to define static IPs for specific MAC addresses: |
| 108 | + |
| 109 | +```xml |
| 110 | +<dhcp> |
| 111 | + <range start="192.168.122.2" end="192.168.122.254"/> |
| 112 | + <host mac="52:54:00:e4:de:fb" name="vm1" ip="192.168.122.11"/> |
| 113 | + <host mac="52:54:00:14:67:fa" name="vm2" ip="192.168.122.12"/> |
| 114 | + <host mac="52:54:00:39:aa:1d" name="vm3" ip="192.168.122.13"/> |
| 115 | + <host mac="52:54:00:95:aa:41" name="vm4" ip="192.168.122.14"/> |
| 116 | +</dhcp> |
| 117 | +``` |
| 118 | + |
| 119 | +After saving and closing the file, restart the NAT network: |
| 120 | + |
| 121 | +``` |
| 122 | +$ virsh net-destroy default |
| 123 | +$ virsh net-start default |
| 124 | +``` |
| 125 | + |
| 126 | +Then reboot your virtual machines. If the OpenSSH server is installed, you can connect from your host machine via SSH like so: |
| 127 | + |
| 128 | +``` |
| 129 | +$ ssh user@192.168.122.11 |
| 130 | +``` |
| 131 | + |
| 132 | +## References |
| 133 | + |
| 134 | +- [How To install QEMU KVM & VirtManager on Ubuntu Run Virtual Machines On Ubuntu](https://www.youtube.com/watch?v=4m6eHhPypWI) |
| 135 | +- [KVM/libvirt: How to configure static guest IP addresses on the virtualisation host](https://serverfault.com/questions/627238/kvm-libvirt-how-to-configure-static-guest-ip-addresses-on-the-virtualisation-ho) |
0 commit comments