Skip to content

KVM/QEMU Hypervisor on Ubuntu

Purpose

This guide will describe how to take a bare-metal x86_64 machine and turn it into a KVM/QEMU-based hypervisor.

The machine will boot to a text console, but a VNC desktop is available over the network for managing VMs.


Requirements

To build a hypervisor, you'll need:

  • some x86_64 system with:
    • at least 16GB of RAM
    • enough storage for VM images
    • public Internet access
  • some other system with SSH and VNC clients installed on it
  • the Ubuntu 22.04 LTS Server installation media

Procedure

You will be:

  • installing the OS
  • setting up a bridged network adapter
  • installing X-Windows with a lightweight Xfce4 desktop available via VNC
  • installing virtualization tools

All commands should be performed as the root user unless otherwise specified.


Installing the OS

OS Install and Naming

Install Ubuntu 22.04 LTS on the target system. If asked on the boot screen, select the LTS kernel. Name the machine:

hostnamectl set-hostname kvm-01

If you have an FQDN, set the hostname to the full FQDN:

hostnamectl set-hostname kvm-01.internal.my-domain.net

Edit the /etc/hosts file and change the entry for 127.0.1.1 from whatever your system was named to your new hostname.

Running hostname should show your changes.


Install Bridge Utils

Install the package needed to configure the Ethernet interface as a bridge. This allows you to run VMs on the same network as the attached Ethernet interface.

apt -y install bridge-utils

If you fail to perform this step, your system will be unavailable on the network after running netplan apply in the next section.


Assign a Static IP

Edit your NetPlan configs to:

  • create a bridge interface named br0
    • assign a static IP/mask, gateway, and dns resolver to the br0 interface
    • attach your physical network interface to the virtual bridge interface
  • update the /etc/hosts file to reflect the IP address change

Create br0 Interface

Edit your /etc/netplan/00-installer-config.yaml file to look like the config below.

Swap your IP/mask, default gateway, resolver, and interface names because they are probably different from the example.

You can get a list of interface names on your system by running ip addr.

This is a YAML file, so it's sensitive to white spaces and indenting. Use two spaces to indent to avoid parsing issues.

network:
  ethernets:
    enp8s0f0:
      dhcp4: false
      dhcp6: false
  bridges:
    br0:
      dhcp4: false
      dhcp6: false
      interfaces: [enp8s0f0]
      addresses: [192.168.201.100/23]
      gateway4: 192.168.200.1
      nameservers:
        addresses: [10.12.0.5]
        search: [internal.my-domain.net]
      parameters:
        stp: yes
        forward-delay: 4
  version: 2

Apply the changes:

netplan apply

Update Hosts File

You must update the entry for your system's hostname in the /etc/hosts file.

Change the 127.0.1.1 IP address for your hostname to the static IP address you bound to the br0 adapter.

Your hosts file should look something like this if you are using an FQDN:

127.0.0.1 localhost
192.168.201.100 kvm-01.internal.my-domain.net kvm-01

If you are only using a hostname and are not using an FQDN, your hosts file should look something like this:

127.0.0.1 localhost
192.168.201.100 kvm-01

Testing Static IP

You should now be able to SSH into your system at the static IP you configured. If this works, your bridged networking is now configured properly.


Update the OS

Apply all updates from upstream and remove unneeded packages:

apt update && \
apt -y upgrade && \
apt -y autoremove

Reboot to use the new kernel it probably installed.


Installing the VNC Desktop

While you could do 100% of your OS installs via serial text console, that would suck pretty hard, and this is the 21st century. A GUI desktop is more accessible. We can install the VNC server and the Xfce4 desktop easily.
Install the software:

apt -y install tightvncserver xfce4 xfce4-goodies xfce4-terminal moka-icon-theme

Run this as your regular non-root user to configure a VNC password:

vncpasswd

and answer the questions. You do not want a view-only account.

Replace the contents of your non-root user's ~/.vnc/xstartup file with:

#!/bin/sh

xrdb $HOME/.Xresources
startxfce4 &

Set the file executable:

chmod +x ~/.vnc/xstartup

Start the VNC server as your non-root user with:

/usr/bin/vncserver -depth 24 -geometry 1920x1200 :99

You should have a VNC Xfce4 desktop on port 5999 on your target machine.

The VNC server can be stopped by running this command as your non-root user:

/usr/bin/vncserver -kill :99

Installing the Virtualization Tools

Install the virtualization software by running:

apt -y install libvirt-clients libvirt-daemon-system virt-manager qemu-system-arm qemu-system-common qemu-system-mips qemu-system-misc qemu-system-ppc qemu-system-s309x qemu-system-sparc qemu-system-x86

Add yourself to the 'libvirt' group so you can interact with the hypervisor without being root:

/usr/sbin/usermod -aG libvirt `whoami`

Log all the way out of your user's login sessions and back in to pick up the group changes. If you are unsure, reboot the machine.

You can examine the details of your group membership by running id. You should be a member of the libvirt group.


Creating VMs

Connecting to the Desktop

Log into your VNC desktop by connecting to the IP address you bound to the br0 interface on the hypervisor on port 5999. Use the password you set when you ran vncpasswd.


Starting virt-manager

Start an app named 'virt-manager' from the menu at the top-left of the screen. It is under 'Applications --> System'.

This is where you make VMs, power them off/on, and can see and interact with their console.


Configure Defaults

The SPICE type display will not work when a VNC client connects to the desktop. You must change the default display type to VNC. To do this:

  1. click on the 'Edit' dropdown menu in virt-manager
  2. click on 'Preferences'
  3. click on the 'New VM' tab
  4. change the 'Graphics Type' to 'VNC'
  5. ensure the 'x86 Firmware' option is set to 'UEFI'

Creating a New VM

Your VMs' vNICs should be attached to the br0 virtual interface if you want them to be on the same network as the hypervisor.

In some cases, the virt-manager software does not honor the 'new VM defaults' settings.

Ensure your new VMs are created with a VNC display type by enabling the 'Customize configuration before install' option when creating a new VM:

  • remove the 'Display Spice' virtual device
  • click on 'Add Hardware' to add a new display device:
    • click on 'Graphics' in the left-nav list
    • make sure the 'Type' is set to 'VNC Server'
    • click 'Finish'
  • remove the 'Sound ich9' virtual device
  • click on the 'Finish' button

The VM will start and boot into whatever environment your boot media contains.