How to install and configure KVM on CentOS

Last updated on November 26, 2020 by Dan Nanni

KVM is a kernel-based hypervisor which grows quickly in maturity and popularity in the Linux server market. Red Hat officially dropped Xen in favor of KVM since RHEL 6. With KVM being officially supported by Red Hat, installing KVM on RedHat-based systems should be a breeze.

In this tutorial, I will describe how to install and configure KVM and VirtManager on CentOS. To use this tutorial, it is not required to have CentOS desktop environment. This tutorial was in fact tested on CentOS 6.4 server.

Check Hardware Virtualization Supoort

KVM requires hardware virtualization support such as Intel VT or AMD's AMD-V, which are instruction set extensions for hardware-assisted virtualization. Check if hardware virtualization support is available on CentOS host machine:

$ egrep -i 'vmx|svm' --color=always /proc/cpuinfo

If CPU flags contain vmx or svm, it means hardware virtualization support is available.

Configure FQDN for local host

Configure FQDN (fully qualified domain name) for local host. Otherwise, you may get warnings while launching libvirtd daemon such as "getaddrinfo failed for 'myhost': Name or service not known".

To configure FQDN, edit the following configuration file:

$ sudo -e /etc/sysconfig/network
HOSTNAME=xxx.yyy

Disable SELinux

Before installing KVM, be aware that there are several SELinux booleans that can affect the behavior of KVM and libvirt. In this tutorial, I'm going to set SELinux to Permissive for demonstration purpose. If you do not wish to change SELinux mode, refer to the documentation on KVM SELinux booleans.

To disable SELinux on CentOS:

$ sudo -e /etc/selinux/config
SELINUX=permissive

Reboot the machine for the change to take effect.

Install KVM, QEMU and User-Space Tools

Install KVM and virtinst (a tool to create VMs) as follows:

$ sudo yum install kvm libvirt python-virtinst qemu-kvm

Start libvirtd daemon, and set it to auto-start:

$ sudo service libvirtd start
$ sudo chkconfig libvirtd on

Check if KVM has successfully been installed. You should see no error as follows.

$ sudo virsh -c qemu:///system list
 Id    Name                           State
----------------------------------------------------

Configure Linux Bridge for VM Networking

Installing KVM alone does not allow VMs to communicate with each other or access external networks. You need to configure VM networking separately. In this tutorial, I am going to set up bridged networking via Linux bridge.

Install a package needed to create and manage bridge devices:

$ sudo yum install bridge-utils

You need to disable Network Manager if it's enabled, and switch to default net manager as follows.

$ sudo service NetworkManager stop
$ sudo chkconfig NetworkManager off
$ sudo chkconfig network on
$ sudo service network start

Once Network Manager is disabled, you can configure a bridge interface by creating its configuration in /etc/sysconfig/network-scripts.

To configure a new bridge, you first have to pick an active network interface (e.g., eth0), and enslave it to the bridge. Depending on whether the network interface is assigned an IP address via DHCP or statically, there are two different ways to configure a new bridge.

To configure bridge br0 via DHCP:

$ sudo -e /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
TYPE=Ethernet
ONBOOT=yes
NM_CONTROLLED=yes
BRIDGE=br0
$ sudo -e /etc/sysconfig/network-scripts/ifcfg-br0
DEVICE=br0
NM_CONTROLLED=yes
ONBOOT=yes
TYPE=Bridge
BOOTPROTO=dhcp

To configure bridge br0 with a static IP address:

$ sudo -e /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
TYPE=Ethernet
ONBOOT=yes
NM_CONTROLLED=yes
BRIDGE=br0
$ sudo -e /etc/sysconfig/network-scripts/ifcfg-br0
DEVICE=br0
NM_CONTROLLED=yes
ONBOOT=yes
TYPE=Bridge
NM_CONTROLLED=yes
BOOTPROTO=none
IPADDR=10.10.1.105
NETMASK=255.255.255.0
GATEWAY=10.10.1.1
DNS1=8.8.8.8
DNS2=8.8.4.4

Note that the configuration for the enslaved interface (eth0) does not have BOOTPROTO field, but BRIDGE field added.

Once configuration files are generated accordingly, run the following to activate the change.

$ sudo service network restart

You should now see br0 bridge interface with a proper IP address as follows.

$ ifconfig
br0       Link encap:Ethernet  HWaddr D4:85:64:78:01:DC
          inet addr:10.10.1.105  Bcast:135.112.33.255  Mask:255.255.255.0
          inet6 addr: fe80::d685:64ff:fe78:1dc/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:182 errors:0 dropped:0 overruns:0 frame:0
          TX packets:38 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:15434 (15.0 KiB)  TX bytes:6648 (6.4 KiB)

eth0      Link encap:Ethernet  HWaddr D4:85:64:78:01:DC
          inet6 addr: fe80::d685:64ff:fe78:1dc/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:188 errors:0 dropped:0 overruns:0 frame:0
          TX packets:38 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:21424 (20.9 KiB)  TX bytes:6408 (6.2 KiB)

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)

virbr0    Link encap:Ethernet  HWaddr 52:54:00:59:A3:88
          inet addr:192.168.122.1  Bcast:192.168.122.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)

Install VirtManager

The final step is to install a desktop UI called VirtManager for managing virtual machines (VMs) through libvirt.

Install and Run VirtManager

$ sudo yum install virt-manager libvirt qemu-system-x86 openssh-askpass libcanberra-devel

Launch VirtManager Locally

If you are using CentOS desktop, you should be able to launch VirtManager locally at this point, by simply running:

$ sudo virt-manager

Launch VirtManager Remotely

However, if you are using CentOS server without desktop UI, follow these steps to launch VirtManager.

First, enable X11 forwarding on sshd running on CentOS server:

$ sudo yum install xauth
$ sudo -e /etc/ssh/sshd_config
X11Forwarding yes
$ sudo service sshd restart

Then, create a following wrapper script for virt-manager.

$ sudo -e /usr/bin/vm
#! /bin/bash
xauth list | while read line; do
 sudo -i xauth add $line
done
sudo -i virt-manager
$ sudo chmod +x /usr/bin/vm

Finally, connect to your CentOS server from a separate desktop machine, and run the wrapper script vm to launch VirtManager remotely.

$ ssh -X dev@centos_server

Troubleshooting KVM and VirtManager Setup

1. If you see the following error when attempting to launch VirtManager remotely, make sure that you use the wrapper script (vm) to launch it, as described above.

X11 connection rejected because of wrong authentication.
Traceback (most recent call last):
  File "/usr/share/virt-manager/virt-manager.py", line 383, in 
    main()
  File "/usr/share/virt-manager/virt-manager.py", line 286, in main
    raise gtk_error
RuntimeError: could not open display

2. If you see the following D-Bus error:

D-Bus library appears to be incorrectly set up; failed to read machine
uuid: UUID file '/var/lib/dbus/machine-id'

Then run the command below and reboot the host machine.

$ sudo sh -c 'dbus-uuidgen > /var/lib/dbus/machine-id'

3. If you have font issue while running VirtManager, install the following font, and relaunch it.

$ sudo yum install dejavu-lgc-sans-fonts

Support Xmodulo

This website is made possible by minimal ads and your gracious donation via PayPal or credit card

Please note that this article is published by Xmodulo.com under a Creative Commons Attribution-ShareAlike 3.0 Unported License. If you would like to use the whole or any part of this article, you need to cite this web page at Xmodulo.com as the original source.

Xmodulo © 2021 ‒ AboutWrite for UsFeed ‒ Powered by DigitalOcean