top of page

A Detailed Guide to Installing, Configuring, and Optimizing Virtual Servers on KVM on Ubuntu

Feb 13

9 min read

0

1

0

A Detailed Guide to Installing, Configuring, and Optimizing Virtual Servers on KVM on Ubuntu

This comprehensive guide provides a detailed walkthrough of installing, configuring, and optimizing Kernel-based Virtual Machine (KVM) on an Ubuntu system. KVM is a full virtualization solution for Linux on x86 hardware containing virtualization extensions (Intel VT or AMD-V). KVM is a Type 1 hypervisor, meaning it interacts directly with the hardware without needing an underlying operating system. This direct interaction results in near-native performance, making KVM a popular choice for developers and system administrators seeking to efficiently manage and operate multiple operating systems within a single computing environment1. Using KVM, one can run multiple virtual machines running unmodified operating systems like Windows, other Linux distributions, BSD, and Solaris.

Installing KVM on Ubuntu

Before diving into the installation process, it's crucial to ensure your system meets the prerequisites. These include a 64-bit processor with hardware virtualization support (Intel VT-x or AMD-V), sufficient RAM (4GB minimum recommended), adequate storage space (at least 20GB), and a stable internet connection1.

Step 1: Verify Hardware Virtualization Support

To confirm your CPU supports the necessary virtualization extensions, use the following command:


Bash



lscpu | grep -E 'vmx|svm'

If the output displays "vmx" (Intel) or "svm" (AMD), your CPU supports virtualization. If not, KVM cannot be installed on your system1.

Step 2: Enable Virtualization in BIOS/UEFI

If the previous command doesn't return the expected output, virtualization might be disabled in your BIOS/UEFI settings. To enable it:

  1. Reboot your system and enter the BIOS/UEFI settings (usually by pressing Del, F2, F10, or Esc during startup).

  2. Locate the virtualization options, often found under CPU configuration.

  3. Enable Intel VT-x or AMD-V, depending on your processor.

  4. Save the changes and exit BIOS/UEFI1.

Step 3: Update and Upgrade Ubuntu

It's recommended to update your system's package list to ensure you have the latest versions of all software. Open a terminal and execute the following commands:


Bash



sudo apt update && sudo apt upgrade -y

Step 4: Install KVM and Related Packages

KVM and its related packages are available in the default Ubuntu repositories. Install them using the following command:


Bash



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

This command installs the following packages:

  • qemu-kvm: This package combines QEMU, which provides the machine emulation, and KVM, which provides the kernel modules for virtualization3.

  • libvirt-daemon-system: The virtualization daemon that manages virtual machines.

  • libvirt-clients: Command-line tools for interacting with the libvirt daemon.

  • bridge-utils: Tools for configuring network bridges.

  • virt-manager: A graphical user interface for managing virtual machines2.

Step 5: Start and Enable libvirtd Service

The libvirtd service is responsible for managing virtual machines. Start and enable it to run at system startup using the following commands:


Bash



sudo systemctl enable --now libvirtd

Verify the service is running correctly by checking its status:


Bash



sudo systemctl status libvirtd

The output should indicate that the service is active and running2.

Step 6: Add User to KVM and Libvirt Groups

To allow your user to manage virtual machines without root privileges, add them to the libvirt and kvm groups:


Bash



sudo usermod -aG libvirt,kvm $USER

Log out and log back in for these changes to take effect2.

Step 7: Verify KVM Installation

To confirm the KVM installation was successful, use the virsh command, a command-line tool for managing virtual machines:


Bash



sudo virsh list --all

This command lists all active and inactive virtual machines. If you haven't created any VMs yet, the output will show an empty list4.

Configuring KVM on Ubuntu

KVM offers various configuration options to customize your virtual machine environment5. This section covers essential configuration steps, including network setup and virtual machine creation.

Network Bridge Configuration

A network bridge allows virtual machines to connect to the external network through the host machine's physical network interface. To configure a bridge:

  1. Determine the name of your Ethernet adapter: Use the ip command to identify the name of your network interface:Baship -c link

  2. Backup existing netplan configuration file: Before making any changes, back up the existing netplan configuration file:Bashsudo cp /etc/netplan/*.yaml /etc/netplan/$(date +%Y%m%d%H%M%S)-backup.yaml

  3. Edit Netplan config: Create a new netplan configuration file or modify the existing one (usually located in /etc/netplan/) to define the bridge. For example:YAMLnetwork:  version: 2  renderer: networkd  ethernets:    <your_ethernet_adapter_name>:      dhcp4: no  bridges:    br0:      interfaces: [<your_ethernet_adapter_name>]      dhcp4: yes      addresses: [192.168.1.100/24]      gateway4: 192.168.1.1      nameservers:        addresses: [8.8.8.8, 8.8.4.4]Replace <your_ethernet_adapter_name> with the actual name of your Ethernet adapter2.

  4. Verify and apply netplan config: Verify the syntax of the configuration file using sudo netplan try. If there are no errors, apply the changes:Bashsudo netplan apply

Understanding Network Modes

KVM supports different networking modes for virtual machines, each with its own characteristics and use cases. The most common modes include:

  • Bridged Networking: In this mode, the VM acts as a separate device on the network, with its own IP address and MAC address. It can communicate directly with other devices on the network as if it were a physical machine. This mode is suitable for scenarios where the VM needs to be a full participant in the network, such as hosting a web server or providing network services.

  • Network Address Translation (NAT): In NAT mode, the VM shares the host machine's IP address and uses NAT to communicate with the external network. This mode provides a simple way to give VMs internet access without assigning them individual IP addresses. It's suitable for scenarios where the VM primarily needs outbound internet access, such as for development or testing purposes1.

Verifying NAT Configuration

To verify that NAT is working correctly, you can check the following:

  • Connectivity from the VM: Ensure the VM can access the internet by pinging a public website (e.g., ping google.com).

  • IP address assignment: Check that the VM has been assigned an IP address in the private network range (usually 192.168.x.x).

  • Network traffic: Use tools like tcpdump or Wireshark to monitor network traffic and confirm that NAT is translating the VM's IP address correctly1.

Creating a Virtual Machine

You can create virtual machines using either the graphical Virtual Machine Manager (virt-manager) or the command-line virt-install tool.

Method 1: Using Virt-Manager

  1. Launch virt-manager by running sudo virt-manager.

  2. In the Virtual Machine Manager window, click the computer icon to create a new VM.

  3. Select the option to install the VM using an ISO image and click "Forward".

  4. Choose the storage volume to store the VM's disk image.

  5. Configure the VM's memory and CPU settings.

  6. Select the network bridge (br0 in this example) you created earlier.

  7. Name the VM and click "Finish" to start the installation3.

Method 2: Using virt-install

The virt-install command provides a more flexible way to create VMs, especially in server environments without a graphical interface. Here's an example command:


Bash



sudo virt-install \--name=myvm \--description='My Ubuntu VM' \--ram=2048 \--vcpus=2 \--disk path=/var/lib/libvirt/images/myvm.qcow2,size=15 \--cdrom /path/to/ubuntu.iso \--network bridge=br0 \--graphics vnc

This command creates a VM named "myvm" with 2GB of RAM, 2 virtual CPUs, a 15GB disk image, and connects it to the br0 bridge6.





Option

Description

--name

VM name

--description

Short description (usually OS name/version)

--ram

Amount of RAM

--vcpus

Number of virtual CPUs

--disk

The location of the VM

--cdrom

Location of the ISO file

--graphics

Specifies the display type

PCIe Passthrough

PCIe passthrough allows you to give a virtual machine direct access to a physical PCI device on the host system, such as a network card or a graphics card. This can provide significant performance improvements for applications that require high-bandwidth or low-latency access to the device.

There are two main methods for configuring PCIe passthrough:

  • Direct PCIe Passthrough: This method allocates exclusive use of a PCIe device to a single VM. It provides the best performance but limits the device's availability to other VMs.

  • Shared PCIe Passthrough (SR-IOV): This method allows multiple VMs to share access to an SR-IOV capable PCIe device. It offers a balance between performance and resource utilization5.

Configuring SSH Access for Remote Administration

When working with KVM on a server, it's often necessary to manage virtual machines remotely. To enable SSH access for remote administration:

  1. Install the OpenSSH server on the host machine:Bashsudo apt install openssh-server

  2. Configure the SSH server to allow connections from your remote machine.

  3. Use an SSH client to connect to the host machine and manage your virtual machines using tools like virsh or virt-manager3.

Optimizing KVM on Ubuntu

Optimizing KVM involves fine-tuning various aspects of the virtualization environment to improve performance and efficiency. Here are some key optimization techniques:

CPU Tuning

  • CPU Pinning: Assign specific physical CPU cores to virtual machines to prevent them from competing for resources and improve performance. This can be done using the virsh edit command to modify the VM's XML configuration. For example, pinning a VM with two vCPUs to cores 2 and 3 would look like this in the XML:XML<cputune>  <vcpupin vcpu='0' cpuset='2'/>  <vcpupin vcpu='1' cpuset='3'/></cputune>7

  • CPU Mode: Configure the CPU mode exposed to the guest VM. Options include host-model (default), host-passthrough, and custom. host-passthrough provides the best performance by exposing the exact host CPU to the guest, but it limits migration capabilities as the guest can only be migrated to a host with an identical CPU model8.

Memory Management

  • Hugepages: Hugepages allow the operating system to manage memory in larger units, reducing the overhead associated with page table lookups and improving performance, especially for memory-intensive applications. Configure hugepages on the host system and enable them for the VM by adding the following to the VM's XML configuration:XML<memoryBacking>  <hugepages/></memoryBacking>7

  • Memory Ballooning: Memory ballooning allows dynamically adjusting the memory allocated to a VM based on its needs. This helps optimize memory utilization across multiple VMs by allowing the hypervisor to reclaim unused memory from a VM and allocate it to others that require it10.

  • Kernel Same-page Merging (KSM): KSM allows the kernel to identify and merge identical memory pages across different VMs, reducing overall memory usage. This is particularly beneficial when running multiple VMs with similar operating systems or applications10.

Disk I/O Optimization

  • Virtio Drivers: Virtio drivers are paravirtualized drivers specifically designed for virtualization environments. They provide enhanced disk and network I/O performance compared to emulated devices. Using Virtio drivers can significantly improve disk I/O performance, especially for I/O-intensive applications11.

  • Disk Cache Modes: Choosing appropriate disk cache modes can impact disk I/O performance. Available modes include:

  • writeback: Data is written to the cache first and then to the disk later. This provides the best performance but can lead to data loss in case of a power failure.

  • writethrough: Data is written to the cache and the disk simultaneously. This ensures data integrity but can be slower.

  • none: Disables caching. This is the safest option but offers the lowest performance12.

Network Optimization

  • Virtio Network Interface: The Virtio network interface is a paravirtualized network device that provides improved network performance compared to emulated network cards12.

  • vhost: Enabling vhost in the VM's network configuration offloads network processing to the host kernel, further enhancing network performance. This can be done by adding <driver name='vhost'/> to the network interface definition in the VM's XML configuration12.

KVM Performance Tweaks

In addition to the optimization techniques mentioned above, there are some specific performance tweaks that can be applied to KVM:

  • Disable Kernel Same-page Merging (KSM): While KSM can be beneficial in some cases, it can also introduce overhead. If you're not running multiple VMs with similar workloads, disabling KSM might improve performance7.

  • Disable Flow Control: Disabling flow control on the network interface can reduce latency and improve network throughput7.

Guest Agent Support

A guest agent is a software component that runs inside a virtual machine and facilitates communication between the guest and the host. It enables features like:

  • Graceful shutdown: Allows the host to send a shutdown signal to the guest operating system, ensuring a clean shutdown instead of abruptly powering off the VM.

  • File injection: Enables transferring files between the host and the guest.

  • Synchronization: Helps synchronize the guest's time with the host's time.

Enabling guest agent support can improve the management and monitoring of virtual machines9.

Tools for Managing KVM Virtual Servers

Several tools are available for managing KVM virtual servers, both from the command line and through graphical interfaces:

| Name | Description |

Works cited

1. How to Install KVM on Ubuntu 24.04: Step-By-Step | Cherry Servers, accessed on February 13, 2025, https://www.cherryservers.com/blog/install-kvm-ubuntu

2. How to Install KVM on Ubuntu 24.04 Step-by-Step - LinuxTechi, accessed on February 13, 2025, https://www.linuxtechi.com/how-to-install-kvm-on-ubuntu/

3. iamdanielv/kvm: How to setup KVM on Ubuntu 20.04 and 22.04 - GitHub, accessed on February 13, 2025, https://github.com/iamdanielv/kvm

4. How to Install KVM on Ubuntu | phoenixNAP KB, accessed on February 13, 2025, https://phoenixnap.com/kb/ubuntu-install-kvm

5. KVM Network Configuration - Oracle Help Center, accessed on February 13, 2025, https://docs.oracle.com/en/operating-systems/oracle-linux/kvm-user/kvm-network-topicgroup.html

6. Setting Up a KVM on Ubuntu for Virtualization | Liquid Web, accessed on February 13, 2025, https://www.liquidweb.com/blog/how-to-set-up-virtualization-host-using-kvm-ubuntu/

7. KVM Performance Tuning - SBC Core 12.1.x Documentation, accessed on February 13, 2025, https://publicdoc.rbbn.com/display/SBXDOC121/KVM+Performance+Tuning

8. KVM - OpenStack Configuration Reference - juno, accessed on February 13, 2025, https://docs.openstack.org/juno/config-reference/content/kvm.html

9. OpenStack Docs: KVM, accessed on February 13, 2025, https://docs.openstack.org/mitaka/config-reference/compute/hypervisor-kvm.html

10. Optimizing Virtualization with KVM Hypervisor - TuxCare, accessed on February 13, 2025, https://tuxcare.com/blog/optimizing-virtualization-with-a-kvm-hypervisor/

11. How can I get good performance in a kvm? - Reddit, accessed on February 13, 2025, https://www.reddit.com/r/kvm/comments/kuzi8s/how_can_i_get_good_performance_in_a_kvm/

12. Optimize KVM virtual machines - IBM, accessed on February 13, 2025, https://www.ibm.com/docs/en/cic/1.2.3?topic=machines-optimize-kvm-virtual


Feb 13

9 min read

0

1

0

Related Posts

Comments

Share Your ThoughtsBe the first to write a comment.
bottom of page