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.

[[email protected] ~]# 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.

[[email protected] ~]# 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

[[email protected] ~]# systemctl start libvirtd
[[email protected] ~]# systemctl enable libvirtd

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

[[email protected] ~]# lsmod | grep kvm
kvm_intel             162153  0
kvm                   525409  1 kvm_intel
[[email protected] ~]#

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.

[[email protected] ~]# 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.

[[email protected] ~]# 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.

[[email protected] ~]# cd /etc/sysconfig/network-scripts/
[[email protected] network-scripts]# cp ifcfg-eno49 ifcfg-br0
[[email protected] network-scripts]#

Edit the Interface file and set followings:

[[email protected] 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:

[[email protected] 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 Service to enable the bridge interface.

[[email protected] ~]# systemctl restart network
[[email protected] ~]#

Check the Bridge interface using below command :

[[email protected] ~]# 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.

[[email protected] ~]# 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.

23 Responses

  1. Julian says:

    Hey, who did this tutorial? Awesome!

  2. Makis says:

    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.

  3. joe glassy says:

    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.

  4. S.R.PrasannaKumar says:

    Excellent One.

  5. joe says:

    virt-manager failed to start with “can not open display”

  6. Brad Goodman says:

    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.

  7. Mohamed says:

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

    [[email protected] ~]# grep -E ‘(vmx|svm)’ /proc/cpuinfo

  8. Hi Mohamed,

    You need to enable nested virtualization in your virtualBox

  9. JuanJo says:

    Thanks for the tutorial, very well explained

  10. Steven says:

    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.

  11. Randy MacFarland says:

    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

  12. Roger Gardner says:

    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.

  13. Ming says:

    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.

  14. Prasad says:

    Superb Document

  15. Gilus says:

    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

  16. Tom says:

    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

  17. BazzieB says:

    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

  18. Don Cross says:

    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

  19. Jake S says:

    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?

  20. adinovic says:

    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

Leave a Reply

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

Pin It on Pinterest