KVM virtualization on CentOS 7

kvm-logoThis short tutorial will help you setup your CentOS 7 as a host for KVM virtualization via NAT IP. Using this method, we can install many VMs on CentOS 7 using only 1 IP as we did the same thing with Proxmox before.

Preparation for Prerequisite softwares

  • First, check if the current machine support hardware virtualization:

    [bash]egrep -c ‘(vmx|svm)’ /proc/cpuinfo[/bash]

    . The result should return >1 this case.

  • Next, install required softwares for kvm:

    [bash]sudo yum install kvm virt-manager libvirt virt-install qemu-kvm xauth dejavu-lgc-sans-fonts[/bash]

  • Allow IP forwarding for networking:

    [bash][[email protected] ~]# echo "net.ipv4.ip_forward = 1"|sudo tee /etc/sysctl.d/99-ipforward.conf
    net.ipv4.ip_forward = 1
    [[email protected] ~]# sysctl -p /etc/sysctl.d/99-ipforward.conf
    net.ipv4.ip_forward = 1[/bash]

  • Setting up bridge networking between the host and guest VMs
    • In the host machine, there is one real network interface (enp3s0 or eth0 or sth like that), we will need to edit this interface (/etc/sysconfig/network-scripts/ifcfg-enp3s0 or /etc/sysconfig/network-scripts/ifcfg-eth0) and comment out IP-related information as follows:

      #SCOPE="peer XXX.XXX.XXX.33"

    • Then, we will need to create a new bridge network interface (/etc/sysconfig/network-scripts/ifcfg-virbr0) and put IP information with some additional information as follows:

      SCOPE="peer XXX.XXX.XXX.33"[/bash]

    • Then, restart the network service.
  • Finally, check the setup environment:


Manage VMs using CLI

After completing the KVM installation, it’s time to start using the host. First thing we need to do is to create a new domain or VM.

Adding a new VM

To create a new virtual machine using the CLI, we need to know which template we will use to install the system. To get a list of templates that are known in our KVM installation, you can do the following:

Example to add a windows-guest

After launching the above command, you should be able to connect with VNC to the host and get on the console-display of the VM. The console displays what would normally, on a physical machine, appear on the attache monitor.

By default, VNC will use the first available screen on port 5900. To be sure which screen is used, we can use virsh to show the attached console-screens for VNC:


From this point, we can complete the windows installation as if it would be a normal physical system:


After completing the installation with VNC, we end up with a Windows-VM that is running on our KVM-host:


As for the networking part, we use the earlier created bridge (virbr0) to do NAT. This means that the KVM-host NAT’s all our connections to the real network connected to the KVM-host. If DHCP is active on that network, it can be used in the VM. Otherwise you will have to configure a static IP in the same subnet.


Example to add a Linux-guest

To add a Linux guest, next to the already added Windows-guest is quite similar:

To know which VNC-display number (and port) is used for a certain VM, the same command as used earlier should do:

[bash][[email protected] ~]$ sudo virsh vncdisplay vmdeb7

Above command gives :1 as result, meaning that the guest vmdeb7 can be contacted with VNC on port 5901:


After finishing the installation, we end up with a Linux guest running on top of our KVM-host. Which Linux distro we are using doesn’t matter since we’re doing full virtualization.

Besides creating VM’s, it’s a good thing to know some basic operations regarding VM-managent.

List the active virtual machines:

Get more information about a guest:

Stop a running guest:

To stop a running VM in a clean way (as you would press the power button to start the shutdown sequence):

Start a guest

Delete a guest

First we need to make sure that the guest is stopped before it can be deleted. In case you don’t want the virtual disk image anymore either, you’ll have to delete it manually after undefining the guest.

Automatically let a guest start when the host starts

When rebooting your host, you probably want some or all the guests that are defined on that host to start at the same time. By default, the guest are not automatically started.

Using KVM with the virt-manager GUI

Starting the GUI

Managing KVM with the CLI is not so difficult and it can be ver handy to script certain day-to-day tasks. Sometimes, you just need to keep an overview and require a little more user-friendliness. For that, you can use virt-manager, which is a graphical interface for libvirt and is mainly built for KVM. When you want to manage your guest with virt-manager, you can either do it on the host itself, by starting an X-server locally or use X11 forwarding on a headless server (more information here).

Make sure that you have enough permissions to use virt-manager and simply execute virt-manager from the command line:


Basic actions

From the initial start-up screen, you can immediately see a list of configured guests on this host and take actions on them like: Run, Pause, Shutdown, Reboot, Force off,…

When selecting a guest, you can also click on Open to display the console as we did earlier using VNC:


Advanced actions

Other possibilities using the virt-manager interface:

Connect to another host-system to manage the VM’s running there, using File -> Add connection (like using the –connect on the CLI)

Migrate a VM to another KVM-host: right click on the VM and choose Migrate…

Clone a VM to the same or another KVM-host: right click on the VM and choose Clone…


As you can see, the virt-manager interface is not very complicated and most of the basic tasks don’t need any explanation.

After completing all of the above steps, basic installation and using KVM shouldn’t have any secrets anymore for you. The next thing to do is experiment and test a little more with KVM and hopefully start to use it in your production environment.


About NhocConan

A super lazy guy who tries to write tech blog entries in English. He is lazy, so he can only write when he is in a good mood or when he is tired of coding.

Leave a comment

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