Kernel-based Virtual Machine (KVM) is virtualization software for CentOS or RHEL 7. KVM turn your server into a hypervisor. Let's see how to setup and manage a virtualized environment with KVM in CentOS 7 or RHEL 7. 


Run the following command to test if CPU Support Intel VT and AMD-V Virtualization tech:

$ lscpu | grep Virtualization
Virtualization: VT-x

 

Follow installation steps of KVM on CentOS 7/RHEL 7 headless server

Step 1: Install KVM

Type the following yum command:

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



Start the libvirtd service:



# systemctl enable libvirtd
# systemctl start libvirtd


Step 2: Verify KVM installation


Make sure KVM module loaded using lsmod command and grep command:

 

# lsmod | grep -i kvm


Step 3: Configure bridged networking

By default dhcpd based network bridge configured by libvirtd. You can verify that with the following commands:

 

# brctl show
# virsh net-list



All VMs (guest machine) only have network access to other VMs on the same server. A private network 192.168.122.0/24 created for you. Verify it:

 

# virsh net-dumpxml default



If you want your VMs available to other servers on your LAN, set up a network bridge on the server that connected to your LAN. Update your nic config files such as ifcfg-enp3s0 or em1:

 

# vi /etc/sysconfig/network-scripts/enp3s0



Add line:

BRIDGE=br0


Save and close the file in vi. Edit /etc/sysconfig/network-scripts/ifcfg-br0 and add:



# vi /etc/sysconfig/network-scripts/ifcfg-br0



Append the following:

DEVICE="br0"
# I am getting ip from DHCP server #
BOOTPROTO="dhcp"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
ONBOOT="yes"
TYPE="Bridge"
DELAY="0"


Restart the networking service (warning ssh command will disconnect, it is better to reboot the box):



# systemctl restart NetworkManager

Verify it with brctl command:

# brctl show



Step 4: Create your first virtual machine

I am going to create a CentOS 7.x VM. First, grab CentOS 7.x latest ISO image using the wget command:

 

# cd /var/lib/libvirt/boot/
# wget https://mirrors.kernel.org/centos/7.4.1708/isos/x86_64/CentOS-7-x86_64-Minimal-1708.iso
Verify ISO images:
# wget https://mirrors.kernel.org/centos/7.4.1708/isos/x86_64/sha256sum.txt
# sha256sum -c sha256sum.txt



CREATE CENTOS 7.X VM

In this example, I’m creating CentOS 7.x VM with 2GB RAM, 2 CPU core, 1 nics and 40GB disk space, enter:



# virt-install \
--virt-type=kvm \
--name centos7 \
--ram 2048 \
--vcpus=1 \
--os-variant=centos7.0 \
--cdrom=/var/lib/libvirt/boot/CentOS-7-x86_64-Minimal-1708.iso \
--network=bridge=br0,model=virtio \
--graphics vnc \
--disk path=/var/lib/libvirt/images/centos7.qcow2,size=40,bus=virtio,format=qcow2



To configure vnc login from another terminal over ssh and type:



# virsh dumpxml centos7 | grep vnc
<graphics type='vnc' port='5901' autoport='yes' listen='127.0.0.1'>



Please note down the port value (i.e. 5901). You need to use an SSH client to setup tunnel and a VNC client to access the remote vnc server. Once you have ssh tunnel established, you can point your VNC client at your own 127.0.0.1 (localhost) address and port 5901.

You should see CentOS Linux 7 guest installation.

Now just follow on screen instructions and install CentOS 7. Once installed, go ahead and click the reboot button. The remote server closed the connection to our VNC client. You can reconnect via KVM client to configure the rest of the server including SSH based session or firewall.


How to create RHEL 7.x VM

Make sure you have rhel-server-7.3-x86_64-dvd.iso stored locally. In this example, I’m creating RHEL 7.x VM with 2GB RAM, 2 CPU core, 1 nic and 40GB disk space, enter:



# virt-install \
--virt-type=kvm \
--name rhel7 \
--memory=2048,maxmemory=4096 \
--vcpus=2 \
--os-variant=rhel7.3 \
--cdrom=/var/lib/libvirt/boot/rhel-server-7.3-x86_64-dvd.iso \
--network=bridge=virbr0,model=virtio \
--graphics vnc \
--disk path=/var/lib/libvirt/images/rhel7.qcow2,size=40,bus=virtio,format=qcow2

To configure vnc login from another terminal over ssh and type:

$ sudo virsh dumpxml rhel7 | grep vnc
<graphics type='vnc' port='5904' autoport='yes' listen='127.0.0.1'>



Please note down the port value (i.e. 5904). You need to use an SSH client to setup tunnel and a VNC client to access the remote vnc server. 

Once you have ssh tunnel established, you can point your VNC client at your own 127.0.0.1 (localhost) address and port 5904 to continue with RHEL 7.x installation.


Step 5: Using cloud images

The above installation method is okay for learning purpose or a single VM. Do you need to deploy lots of VMs? Try cloud images. You can modify pre-built cloud images as per your needs. For example, add users, ssh keys, setup time zone, and more using Cloud-init which is the defacto multi-distribution package that handles early initialization of a cloud instance. Let us see how to create CentOS 7 vm with 1024MB ram, 20GB disk space, and 1 vCPU.

GRAB CENTOS 7 CLOUD IMAGE

# cd /var/lib/libvirt/boot
# wget http://cloud.centos.org/centos/7/images/CentOS-7-x86_64-GenericCloud.qcow2

CREATE REQUIRED DIRECTORIES

# D=/var/lib/libvirt/images
# VM=centos7-vm1 ## vm name ##
# mkdir -vp $D/$VM
mkdir: created directory '/var/lib/libvirt/images/centos7-vm1'


CREATE META-DATA FILE

# cd $D/$VM
# vi meta-data

 

Append the following:

instance-id: centos7-vm1
local-hostname: centos7-vm1


CRETE USER-DATA FILE

I am going to login into VM using ssh keys. So make sure you have ssh-keys in place:



# ssh-keygen -t ed25519 -C "VM Login ssh key"

See “How To Setup SSH Keys on a Linux / Unix System” for more info. Edit user-data as follows:



# cd $D/$VM
# vi user-data



Add as follows (replace hostname, users, ssh-authorized-keys as per your setup):

#cloud-config
 
# Hostname management
preserve_hostname: False
hostname: centos7-vm1
fqdn: centos7-vm1.rootadminz.com
 
# Users
users:
    - default
    - name: vyga
      groups: ['wheel']
      shell: /bin/bash
      sudo: ALL=(ALL) NOPASSWD:ALL
      ssh-authorized-keys:
        - ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIIMP3MOF2ot8MOdNXCpHem0e2Wemg4nNmL2Tio4Ik1JY VM Login ssh key
 
# Configure where output will go
output:
  all: ">> /var/log/cloud-init.log"
 
# configure interaction with ssh server
ssh_genkeytypes: ['ed25519', 'rsa']
 
# Install my public ssh key to the first user-defined user configured
# in cloud.cfg in the template (which is centos for CentOS cloud images)
ssh_authorized_keys:
  - ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIIMP3MOF2ot8MOdNXCpHem0e2Wemg4nNmL2Tio4Ik1JY VM Login ssh key
 
# set timezone for VM
timezone: Asia/Kolkata
 
# Remove cloud-init 
runcmd:
  - systemctl stop network && systemctl start network
  - yum -y remove cloud-init


COPY CLOUD IMAGE

# cd $D/$VM
# cp /var/lib/libvirt/boot/CentOS-7-x86_64-GenericCloud.qcow2 $VM.qcow2


CREATE 20GB DISK IMAGE

# cd $D/$VM
# export LIBGUESTFS_BACKEND=direct
# qemu-img create -f qcow2 -o preallocation=metadata $VM.new.image 20G
# virt-resize --quiet --expand /dev/sda1 $VM.qcow2 $VM.new.image


Overwrite it resized the image:

 

# cd $D/$VM
# mv $VM.new.image $VM.qcow2


CREATING A CLOUD-INIT ISO

# mkisofs -o $VM-cidata.iso -V cidata -J -r user-data meta-data

CREATING A POOL

# virsh pool-create-as --name $VM --type dir --target $D/$VM
Pool centos7-vm1 created


INSTALLING A CENTOS 7 VM

# cd $D/$VM
# virt-install --import --name $VM \
--memory 1024 --vcpus 1 --cpu host \
--disk $VM.qcow2,format=qcow2,bus=virtio \
--disk $VM-cidata.iso,device=cdrom \
--network bridge=virbr0,model=virtio \
--os-type=linux \
--os-variant=centos7.0 \
--graphics spice \
--noautoconsole



Delete unwanted files:



# cd $D/$VM
# virsh change-media $VM hda --eject --config
# rm meta-data user-data centos7-vm1-cidata.iso



FIND OUT IP ADDRESS OF KVM VM (DHCP ADDRESS)


# virsh net-dhcp-leases default



LOG IN TO YOUR VM

 

Use ssh command:

# ssh vyga@152.185.175.45

 

Was this answer helpful? 0 Users Found This Useful (11 Votes)