Install KVM Hypervisor on CentOS 7.x and RHEL 7.x

KVM is an open source hardware virtualization software through which we can create and run multiple Linux based and windows based virtual machines simultaneously. KVM is known as Kernel based Virtual Machine because when we install KVM package then KVM module is loaded into the current kernel and turns our Linux machine into a hypervisor.

In this post first we will demonstrate how we can install KVM hypervisor on CentOS 7.x and RHEL 7.x and then we will try to install virtual machines.

Before proceeding KVM installation, let’s check whether your system’s CPU supports Hardware Virtualization.

Run the beneath command from the console.

[root@linuxtechi ~]# grep -E '(vmx|svm)' /proc/cpuinfo

We should get the word either vmx or svm in the output, otherwise CPU doesn’t support virtualization.

Step:1 Install KVM and its associate packages

Run the following yum command to install KVM and its associated packages.

[root@linuxtechi ~]# yum install qemu-kvm qemu-img virt-manager libvirt libvirt-python libvirt-client virt-install virt-viewer bridge-utils

Start and enable the libvirtd service

[root@linuxtechi ~]# systemctl start libvirtd
[root@linuxtechi ~]# systemctl enable libvirtd

Run the beneath command to check whether KVM module is loaded or not

[root@linuxtechi ~]# lsmod | grep kvm
kvm_intel             162153  0
kvm                   525409  1 kvm_intel
[root@linuxtechi ~]#

In Case you have Minimal CentOS 7 and RHEL 7 installation , then virt-manger will not start for that you need to install x-window package.

[root@linuxtechi ~]# yum install "@X Window System" xorg-x11-xauth xorg-x11-fonts-* xorg-x11-utils -y

Reboot the Server and then try to start virt manager.

Step:2 Start the Virt Manager

Virt Manager is a graphical tool through which we can install and manage virtual machines. To start the virt manager type the ‘virt-manager‘ command from the terminal.

[root@linuxtechi ~]# virt-manager

virt-manger-CentOS7

Step:3 Configure Bridge Interface

Before Start creating VMs , let’s first create the bridge interface. Bridge interface is required if you want to access virtual machines from outside of your hypervisor network.

[root@linuxtechi ~]# cd /etc/sysconfig/network-scripts/
[root@linuxtechi network-scripts]# cp ifcfg-eno49 ifcfg-br0
[root@linuxtechi network-scripts]#

In cp command replace the interface name that suits to your system and then edit the Interface file and set followings:

[root@linuxtechi network-scripts]# vi ifcfg-eno49
TYPE=Ethernet
BOOTPROTO=static
DEVICE=eno49
ONBOOT=yes
BRIDGE=br0

Edit the Bridge file (ifcfg-br0) and set the followings:

[root@linuxtechi network-scripts]# vi ifcfg-br0
TYPE=Bridge
BOOTPROTO=static
DEVICE=br0
ONBOOT=yes
IPADDR=192.168.10.21
NETMASK=255.255.255.0
GATEWAY=192.168.10.1
DNS1=192.168.10.11

Replace the IP address and DNS server details as per your setup.

Restart the network Manager Service to enable the bridge interface.

[root@linuxtechi ~]# systemctl restart NetworkManager
[root@linuxtechi ~]#

Note: By default, in CentOS 7 / RHEL 7 Network Manager service is enabled and if have made changes in network files then we must restart Network Manager service to make the changes into the effect. In Case network Manager service is disabled and stopped then we can use network service (systemctl restart network)

Check the Bridge interface using below command :

[root@linuxtechi ~]# ip addr show br0

Step:4 Start Creating Virtual Machines.

Now Create Virtual Machine either from the command line using ‘virt-install‘ command or from GUI (virt-manager )

Let’s Create a virtual machine of “Windows Server 2012 R2” using virt-manager.

Start the “virt-manager”

Go to the File Option, click on “New Virtual Machine”

create-new-virtual-machine-virt-manager

We will be using ISO file as installation media. In the next step Specify the path of ISO file.

path-iso-file-windows2012r2

Click on Forward.

Specify the Compute Resources : RAM and CPU as per your setup.

compute-resources-for-vm-in-virtmanager

Click on Forward to proceed further.

Specify the storage Size of Virtual Machine, In my case I am using 25G.

Storage-for-virtual-machine-virt-manager

In the Next step Specify the Name of Virtual Machine and select network as ‘ Bridge bro’

Name-network-selection-virtual-machine-virtmanager

Click on Finish to start the installation.

windows2012-intsallation-virtmanager

Follow the screen instructions and complete the installation.

Creating a virtual Machine from Command Line:

Virtual Machines can be created from the console as well using ‘virt-install’ command. In the following example i going to virtual machine of Ubuntu 16.04 LTS.

[root@linuxtechi ~]# virt-install --name=Ubuntu-16-04 --file=/var/lib/libvirt/images/ubuntu16-04.dsk --file-size=20 --nonsparse --graphics spice --vcpus=2 --ram=2048 --cdrom=ubuntu-16.04-server-amd64.iso --network bridge=br0 --os-type=linux --os-variant=generic
Starting install...
Allocating 'ubuntu16-04.dsk'               | 20 GB 00:00:00
Creating domain...

ubuntu-16-04-virt-install

Follow the instruction now and complete the installation.

In the above ‘virt-install’ command we have used following options :

  • –name = <Name of the Virtual Machine>
  • –file = <Location where our virtual machine disk file will be stored >
  • –file-size = < Size of the Virtual Machine, in my case it is 20GB >
  • –nonsparse = < Allocate the whole storage while creating>
  • –graphics = < Specify the graphical tool for interactive installation, in above example I am using spice >
  • –vcpu = < Number of virtual CPU for the Machine >
  • –ram = < RAM size for the virtual Machine >
  • –cdrom = < Virtual CD ROM which specify the installation media like ISO file >
  • –network = < it is used to specify which network we will use for the virtual machine, in this example I am bridge interface>
  • –os-type = < Operating system type like linux and window>
  • –os-variant= <KVM maintains the OS variants like ‘fedora18′, ‘rhel6’ and ‘winxp’ , this option is optional and if you not sure about OS variant you can mentioned it as generic>

Once the Installation is completed we can access the Virtual Machine console from ‘virt-manager‘ as shown below.

Ubuntu-16-04-virtual-machine-virt-manager

That’s it, basic installation and configuration of KVM hypervisor is completed.

29 thoughts on “Install KVM Hypervisor on CentOS 7.x and RHEL 7.x”

  1. congrats! Very helpful tutorial.
    On the first step you can also add below of : lsmod | grep kvm. That if this command returns nothing execute those commands:
    modprobe kvm
    modprobe kvm-intel or kvm-amd(depends on your cpu)
    In order to load the module.

    Reply
  2. Thanks for this tutorial! On CentOS 7 I believe it’s quite a similar process, however, I think that the default /etc/qemu.conf has to be edited, e.g. un-comment out (enable) user=root and group=root, or substitute other desired accounts. Then restart libvirtd, run status on it, and it will look much better.

    Reply
  3. This is awesome. I have been using it as my “gold standard” guide for getting VMs running on a CentOS 7 host. In fact, I don’t even use the GUI to get stuff going – I use the “Creating from a Command Line” as described above, then I do a “virsh console” to get the text console of the VM (as it boots from the install media), and do a “text” install (add “text” to the kernel/boot command line). No GUI ever used (just the way God intended Unix to be)!

    The only part that I thought was a bit confusing was the network and bridge setup. The way it works is – the address info all gets set in the BRIDGE device – all your “real” device does is kind of “point” to the bridge.

    Reply
  4. I am runing centos on virtualbox VM on windows
    I have configured paravirtualization on virtualbox but always I have null putput on running this

    [root@linuxtechi ~]# grep -E ‘(vmx|svm)’ /proc/cpuinfo

    Reply
  5. What is best backup software to backup virtual machine reside inside the KVM hypervisor online (not offline)?

    Eg. Veeam is best for vmware and hyperV, but do not support KVM.

    Reply
  6. I don’t get X running when entering the command above. When I reboot I get a license prompt which I except but then it just fails to the shell

    Reply
  7. Hello, I have RHEL 7.3 installed and can not attach a CD/DVD or boot from one. I keep getting: Driver ‘host:cdrom’ is not whitelisted.
    I have found that alot of People have this Problem, but never found a solution. Can anyone here help.

    Reply
  8. Hi , it’s a very good introduction. But I don’t understand why it’s necessary to manually setup a bridge in step2.
    This bridge is used as a physical network interface. I still have to create a ‘virtual network bridge. eg. virbr1’ with or without this one, and attached to the physical interface.

    Reply
  9. Hi, i follow the instruction you give in the tuto, but i have error when i launch my VM.
    g-invoke-error-quark: Could not locate spice_gtk_session_get: ‘spice_gtk_session_get’: /lib64/libspice-client-glib-2.0.so.8: undefined symbol: spice_gtk_session_get (1)
    After search on google, i saw this https://forums.linuxmint.com/viewtopic.php?t=227025, but if i try to install gir1.2-spice-client-gtk-3.0, it tell me no packets available.
    I’m on Centos 7

    Reply
  10. How about libguestfs-tools?

    yum install qemu-kvm qemu-img virt-manager libvirt libvirt-python libvirt-client virt-install virt-viewer bridge-utils libguestfs-tools

    Reply
  11. Hi There

    Awesome guide. I just have a slight problem when I create the bridge adapter. I do everything as you have it but when I systmectl restart network i loose connectivity to my server. I cant even ping it afterwards. I have to go to the server and delete the bridge adapter and then reboot. Any suggestions?

    Kind Regards

    Reply
  12. I was able to create everything per your instruction but I realized I can’t ssh into the host machine. Upon checking the ip address, the was no IP for the host adapter but IP for the bridge. Now the question is if I want to ssh into the host server what do I do. Thanks

    Reply
  13. I’m doing something stupid, I know it. Everything works up to the last frame – “centos_01 login:” I have no login, how do I create a root user and/or a regular user?

    Reply
  14. when running virt-manager, in case of : ImportError: No module named ‘requests.packages.urllib3’ , i did the following :
    pip uninstall requests
    pip uninstall urllib3
    yum remove python-urllib3
    yum remove python-requests

    and then reinstall virt-manager
    yum install virt-manager virt-install virt-viewer

    Reply
  15. This tutorial no longer works, I tried to follow the steps religiously 2 times, and had to format my server because I lost remote SSH access when setting up the bridged network.

    Reply
    • Hi Thiago,

      Reason for losing the connectivity might be of network manager. It seems like Network Manager service was enabled on your system and to make network changes into the effect you might have restarted the network service. So to avoid these conditions either use Network Manager or stop and disable the Network Manager service and then restart network service.

      I have modified the article as well.

      Reply
  16. Hi.. i didn’t use default virbr0(NAT)… but i create bridge br0 that mapping with enp0s3 interface and VM host able to access internet and lan. When I create guest VM and attach to br0, the vm can only ping to host VM and physical host but not able to ping gw router and cannot access internet. What could be wrong here. Please advise. Thank you

    Reply

Leave a Reply to Thiago Queiroz Cancel reply