Installing and configuring the KVM hypervisor on CentOS.

In this article, we will cover the installation and basic configuration of the KVM hypervisor on a server running Linux CentOS. We will show how to create a virtual machine in KVM, change its settings, install a guest operating system and basic VM management commands.

KVM (Kernel-based Virtual Machine) is a Linux virtualization tool that uses hardware virtualization based on Intel VT or AMD SVM . With KVM, you can create isolated virtual machines with your own virtual hardware: network cards, disks, video cards, and other devices. You can install any OS on virtual machines (not just Linux).

Installing KVM on CentOS.

When setting up KVM on a server, you need to start by checking your processor. You need to find out if the CPU installed on your server supports hardware virtualization. From the server console, run the command:

# cat /proc/cpuinfo | egrep "(vmx|svm)"
If your processor supports VT-x technology , you should have something like this:

If the command did not produce anything, but your processor definitely supports virtualization, check if this option is disabled in the server BIOS . Look for "Intel Virtualization Technology" or "SVM MODE" options.

On my server, support for this technology is enabled, so you can start installing the necessary components in CentOS using the yum / dnf package manager:

# yum install libvirt libvirt-python libguestfs-tools qemu-kvm virt-install –y

  • qemu-kvm - the KVM hypervisor itself;
  • libvirt - virilization control libraries;
  • virt-install - Commands for managing KVM virtual machines.

A large number of packages will be installed on the server, make sure that you do not have any errors during the installation process.

Now you need to add the libvirtd service to startup and start it:

# systemctl enable libvirtd
# systemctl start libvirtd

Check if the kvm_intel and kvm kernel modules have loaded:

[root@localhost /]# lsmod | grep kvm

kvm_intel 188688 0
kvm 636931 1 kvm_intel
irqbypass 13503 1 kvm

If you don't see anything, restart the server and check again.

Network setup in KVM.

Now let's create directories where virtual machine disks and iso images will be stored:

# mkdir -p /vz/{disk,iso}
In order for the created virtual machines to have access to the Internet, we need to configure the network via bridge. This will require the bridge-utils package, check if it is installed on Linux with the command:
# rpm -qa | grep bridge-utils
Install it if needed:
# yum install bridge-utils -y
After installing the packages for KVM, the network interface virbr0 appeared in the system :
# ip a

To create a network bridge ( bridge ), you need to set up a configuration file for the server network interface:

# cp /etc/sysconfig/network-scripts/ifcfg-enp1s0f0 /etc/sysconfig/network-scripts/ifcfg-enp1s0f0_bak - make a backup of the file

# nano /etc/sysconfig/network-scripts/ifcfg-enp1s0f0 - open for editing

Delete all content and replace with the following:

DEVICE="enp1s0f0"
ONBOOT="yes"
BRIDGE=br0
In your case, the interface name may be different (look through the ip a command).

Then create a file:
# nano /etc/sysconfig/network-scripts/ifcfg-br0
with content:
DEVICE="br0"
TYPE=BRIDGE
ONBOOT=yes
BOOTPROTO=static
IPADDR="Server_IP" NETMASK="255.255.255.0"
GATEWAY="Master Gateway"
DNS1="77.88.8.8"
DNS2="8.8.4.4"

Where to enter your details: server IP address and default gateway. After editing the configuration files of network interfaces, you need to restart the eservice:

# service network restart

If you lost access to the server after network restart, try restarting it. Sometimes this is required to correctly configure the bridge.

To check the status of the bridge, use the command:

# brctl show

bridge name bridge id STP enabled interfaces
br0 8000.ac1f6b945206 no enp1s0f0
virbr0 8000.5254003f23cf yes virbr0-nic

The last step is to configure network traffic redirection:
# echo -e "net.ipv4.ip_forward=1" >> /etc/sysctl.conf && sysctl -p
Now restart the libvirtd service :
# service libvirtd restart

This completes the basic KVM setup, you can create a virtual machine.

KVM: creating a virtual machine.

Before creating the virtual machine, I downloaded the CentOS 8 image from the official mirror to the /vz/iso directory:

# cd /vz/iso && wget https://mirror.google.com/centos/8.1.1911/isos/x86_64/CentOS-8.1.1911-x86_64-dvd1.iso

To create a new KVM virtual machine, run:

 virt-install -n test-centos \
--noautoconsole \
--network=bridge:br0 \
--ram 2048 --arch=x86_64 \
--vcpus=4 --cpu host --check-cpu \
--disk path=/vz/disk/test-centos.img,size=32 \
--cdrom /vz/iso/CentOS-8.1.1911-x86_64-dvd1.iso \
--graphics vnc,listen=IP,password=123456789 \
--os-type linux --os-variant=rhel7 --boot cdrom,hd,menu=on

  • test-centos - VM name;
  • noautoconsole - after creation, you do not need to connect to the console of the virtual machine automatically;
  • network – network type (in our case bridge);
  • ram - the amount of RAM in the VM;
  • vcpus – number of processor cores (vCPU setting in KVM);
  • disk is a virtual disk, path is the path to the disk. size - volume (in the future it can be expanded / reduced);
  • cdrom - virtual cdrom, into which iso image is mounted to install guest OS;
  • graphics - parameters for connecting to the machine using the graphics console. We connect via VNC, so in listen it specifies the IP of the server where we created the VM and the password for connecting in the virtual machine console (password).
To make the VM boot automatically, run:
# virsh autostart test-centos

Connecting to a VM on KVM via VNC, OS installation.

To connect to a virtual machine via VNC, we need to find out the port it is running on:

[root@local vz]# virsh vncdisplay test-centos

IP:0

0 means that the port for connecting via VNC is 5900, if you get a different result, just add the number that the command will give you to 5900.

To connect via VNC to servers, I use the TightVNC utility . Run it and specify the server IP address and VNC port that we learned earlier (separated by a double colon).


Click “ Connect ”, in the window that opens, enter the password that you set when creating the VM. If everything is done correctly, you will open the server console on which the installation of CentOS (or another OS, the image of which you mounted) is running.

If you have a firewall installed on the server, open network ports for VNC 5900-5910 in firewalld / iptables, this should be enough. After installing the guest OS on the virtual machine, start the VM from the server console:

To shut down the KVM virtual machine, use the command:
# virsh shutdown test-centos
To list all registered KVM virtual machines:
# virsh list --all

If you prefer graphical management, you can use the graphical KVM console, virt-manager.

This completes the basic server setup for KVM and the creation of a virtual machine. You can create an unlimited number of virtual machines on KVM, it all depends on the resources of your server and your needs.

Отправить комментарий

Добавлять новые комментарии запрещено.*

Новые Старые