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:
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:
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:
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:
Start libvirtd daemon, and set it to auto-start:
$ sudo chkconfig libvirtd on
Check if KVM has successfully been installed. You should see no error as follows.
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:
Disable Network Manager service if it's enabled, and switch to default net manager as follows.
$ 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:
DEVICE=eth0 TYPE=Ethernet ONBOOT=yes NM_CONTROLLED=yes BRIDGE=br0
DEVICE=br0 NM_CONTROLLED=yes ONBOOT=yes TYPE=Bridge BOOTPROTO=dhcp
To configure bridge br0 with a static IP address:
DEVICE=eth0 TYPE=Ethernet ONBOOT=yes NM_CONTROLLED=yes BRIDGE=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=18.104.22.168 DNS2=22.214.171.124
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.
You should now see br0 bridge interface with a proper IP address as follows.
br0 Link encap:Ethernet HWaddr D4:85:64:78:01:DC inet addr:10.10.1.105 Bcast:126.96.36.199 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)
The final step is to install a desktop UI called VirtManager for managing virtual machines (VMs) through libvirt.
To install VirtManager:
1. Launch VirtManager Locally
If you are using CentOS desktop, you should be able to launch VirtManager locally at this point, by simply running:
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:
Create a following executable "wrapper" script for virt-manager.
#! /bin/bash xauth list | while read line; do sudo -i xauth add $line done sudo -i virt-manager
Then connect to your CentOS server from a separate desktop machine, and run the wrapper script vm to launch VirtManager remotely.
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.
3. If you have font issue while running VirtManager, install the following font, and relaunch it.
Subscribe to Xmodulo
You can have daily Linux tutorials and FAQs delivered to your email inbox. Simply enter your email address below to subscribe to our mailing list. You will receive hands-on guides and carefully written tutorials related to Linux, everything for free.