How to install and configure KVM on CentOS

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

Disable Network Manager service 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

To configure a new bridge, you 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.

To install VirtManager:

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

1. 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

2. Launch VirtManager Remotely

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

Enable X11 forwarding on SSH server:

$ sudo yum install xauth

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

Create a following executable "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

Then 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

Subscribe to Xmodulo

Do you want to receive Linux FAQs, detailed tutorials and tips published at Xmodulo? Enter your email address below, and we will deliver our Linux posts straight to your email box, for free. Delivery powered by Google Feedburner.

The following two tabs change content below.
Dan Nanni is the founder and also a regular contributor of Xmodulo.com. He is a Linux/FOSS enthusiast who loves to get his hands dirty with his Linux box. He likes to procrastinate when he is supposed to be busy and productive. When he is otherwise free, he likes to watch movies and shop for the coolest gadgets.

7 thoughts on “How to install and configure KVM on CentOS

  1. Thanks for the article. Some notes:

    "SELINUX=disabled"

    You really, really shouldn't do that. If you really want to 'turn SELinux off', at least set it to Permissive mode, don't just disable it. But it'd be best to advise people simply to leave it on. I only have to flip I think one boolean for my use of libvirt, it is not a difficult thing to do.

    "sudo rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY* "

    You don't really need to do that manually. If it hasn't been done yet, the first 'yum install' command you run should prompt for it.

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

    I don't know if this is true for CentOS, but for Fedora, there is a virtualization group defined which should installed all required packages: "yum groupinstall Virtualization".

    "Create a following executable “wrapper” script for virt-manager...Then connect to your CentOS server from a separate desktop machine, and run the wrapper script vm to launch VirtManager remotely."

    That's a really weird way of doing it. virt-manager and libvirt actually have several native methods for accessing VMs on a remote host, including ssh: you just have to configure things so you can log into the VM host machine as root via ssh, then you can run virt-manager locally on any other machine, click 'File / Add connection...', check the 'Connect to remote host' box, leave Method as SSH, and enter the hostname for the VM host machine.

    • These are good comments. Thanks for taking the time to respond!
      Post slightly updated. Let me check out the native method of virt-manager to access a remote host.

  2. I am trying to set the host machine and the guest (KVM) client to have static IP addresses on the same subnet. Funny thing I can get both of them to see the www but they can't see (ping) each other on the same network, subnet, etc. I am using ifcfg-eth0 for both systems with their appropriate ip address, gateway, dns. Any help would be greatly appreciated. host machine tiger.example.com 192.168.0.100 KVM machine lion.example.com 192.168.0.200 gw=192.168.0.1, Netmask 255.255.255.0 I don't have the ifcfg-eth0 pointing to the ifcfg-br0 bridge, but it is needed because my KVM client wouldn't load without it.

    • You need ifcfg-eth0 pointing to ifcfg-br0 (which contains the static ipaddress, dns, netmask) on the host, and ifcfg-eth0 with the static addres (like a normal eth0)

      Then you connect your client to the host device eth0 (bridge br0) in the client settings.

      Cheers

  3. I am studying for RHCSA. I'm following Jang's book. I have hit a snag in chapter 2 when I power on server1.example.com from the KVM GUI. I receive an error message in the graphical desktop window after starting the VM that states something similar to "Error: The connection was refused or disconnected!" I cannot identify whether this is a KVM configuration issue, user error, or otherwise. I have followed Jang's guide to the letter and this is the result. Any ideas?

  4. Installing KVM virtualization on CentOS seems to be pretty complex for beginners like myself. This post help me a lot. :)

Leave a comment

Your email address will not be published. Required fields are marked *

Current ye@r *