Table of Contents

Ubuntu - KVM - Setup KVM with bridged networking and virt-manager

KVM is the Linux kernel's virtualization tool.

Install The Packages

Even though the capabilities for KVM are built into the kernel itself, there are a couple of packages that you'll need to get started.

They're all standard packages in the default Ubuntu repositories, so install them first.

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

Verify the status of the libvirtd status

The libvirtd service should be started automatically. Check this:

sudo systemctl status libvirtd.service

Start default network

List available networks:

sudo virsh net-list --all

To make it active and auto-start across boots:

sudo virsh net-start default
sudo virsh net-autostart default

Add vhost_net module

To offload the mechanism of virtio-net and to improve performance of KVM VMs add the vhost_net kernel module.

sudo modprobe vhost_net
 
echo "vhost_net" | sudo tee -a /etc/modules vhost_net
 
lsmod | grep vhost

Configure The Network Bridge

Before you can dive in and start making virtual machines, you're going to need to set up a network bridge.

Bridged networking is what allows your VMs to access your network and be assigned their own IP addresses.

Determine your network interface

Find out what names your network interfaces have been assigned.

This guide will use eth0, but your interface will probably be different.

To find out your network interfaces, run ip a. Note the interface with your computer's local IP next to it. Chances are, it'll be something like enp5s0.

ip a

Configure the Bridge

Once you have your network interface, you need to edit a configuration file to tell Ubuntu that your connection is going to be bridged.

This process won't negatively impact your connection at all.

It'll just allow that connection to be shared with the VMs.

Edit /etc/network/interfaces.

/etc/network/interfaces
auto lo br0
iface lo inet loopback
 
iface eth0 inet manual
 
iface br0 inet dhcp
    bridge_ports eth0

or for a static IP address.

/etc/network/interfaces
auto lo br0
iface lo inet loopback
 
auto ens33
iface ens33 inet manual
 
auto br0
iface br0 inet static
        address 192.168.1.69
        netmask 255.255.255.0
        network 192.168.1.1
        broadcast 192.168.1.255
        gateway 192.168.1.1
        bridge_ports ens33
        bridge_stp off
        bridge_fd 0
        bridge_maxwait 0
        dns-nameservers 1.1.1.1
...

If netplan is used:

/etc/netplan/01-network-manager-all.yaml
# Let NetworkManager manage all devices on this system
network:
  version: 2
#  renderer: NetworkManager
  renderer: networkd
  ethernets:
    enp3s0:
      dhcp4: no
      # disable existing configuration for ethernet
      #addresses: [192.168.1.69/24]
      #gateway4: 192.168.1.1
      #nameservers:
        #addresses: [192.168.1.1]
      dhcp6: no
 
  # add configuration for bridge interface
  bridges:
    br0:
      interfaces: [enp3s0]
      dhcp4: no
      addresses: [192.168.1.69/24]
      gateway4: 192.168.1.1
      nameservers:
        addresses: [192.168.1.2,192.168.1.1,1.1.1.1]
      parameters:
        stp: false
      dhcp6: no

In order to bring your new bridge interface up by default, br0 is added at the end of the auto lo.

The bridge information lines tell Ubuntu that your bridge will use DHCP for automatic IP address assignment, and your bridge will manage your current interface.

Ubuntu 18.04 moves to using Netplan to handle Networking, and this therefore requires a different configuration to support bridging.

See:

That's it for your bridge.

Save and exit.


Add Your User To The Groups

Add your user to the appropriate groups so you don't need root privileges to manage your virtual machines.

There are two groups that you need.

sudo adduser username libvirt
sudo adduser username libvirt-qemu

Restart your system

That's the best way to ensure that both the networking and user changes take effect.

Alternatively, to refresh or reload group membership:

newgrp libvirt
newgrp libvirt-qemu

Verify that the bridge has come up

ip a s br0

Create a VM using Virt-Manager (GUI)

Use virt-manager to create VMs.

The window that opens up is fairly plain, but it has everything that you need to manage your VMs.

To start making a new one, click on the icon that looks like a shining screen.

It's the first one in the icon row.

A new window will pop open to walk you through the process.

The first thing that it will ask you to do is select a source.

In most cases, you're going to use a regular install ISO to make your VMs, so leave the first option selected and keep going.

The next screen asks you to select your image.

Browse to the location of your image.

If the folder with your image isn't available, use the + icon at the bottom of the left side to add it.

On the following screen, you can allocate memory and cpu cores to the VM.

Don't give it all of your system's resources. That obviously won't go well.

The next screen lets you determine the hard drive size of your VM. It's just a VM, so it doesn't need a huge hard drive. Just make sure to give it enough to install and run what you need.

Finally, you can see an overview of your VM before you finalize it. On this screen, you can also name it.

Once you finalize your VM, a new window will open and boot up the VM. In that window, you will see the installer for the image that you selected. Everything from here is exactly the same as installing on a regular computer.


Create a VM from command line

sudo virt-install \
  --name ubuntu-vm \
  --os-type linux \
  --os-variant ubuntu18.04 \
  --ram 2048 \
  --vcpu 1 \
  --disk path=/var/lib/libvirt/images/ubuntu-vm.qcow2,size=10 \
  --graphics vnc,listen=0.0.0.0 \
  --noautoconsole \
  --hvm \
  --cdrom /home/peter/vm/ubuntu-18.04-live-server-amd64.iso \
  --boot cdrom,hd

Find VNC display port for ubuntu-vm

sudo virsh list --all

returns:

Id    Name          State
------------------------------
1     ubuntu-vm     running
2     debian-vm     running
...

or

sudo virsh vncdisplay ubuntu-vm

returns:

:0

Connect using Remote Desktop Viewer using IP address and VNC port as “0”.


Closing Thoughts

KVM provides a great amount of flexibility and power to your computer.

It lets you easily run and manage multiple VMs from a convenient interface when paired with virt-manager. With KVM set up, you'll have access to just about any operating system in a virtualized form, directly from your Ubuntu desktop.