How to install KVM on CentOS 7 / RHEL 7 Headless Server
======
How do I install and configure KVM (Kernel-based Virtual Machine) on a CentOS 7 or RHEL (Red Hat Enterprise Linux) 7 server? How can I setup KMV on a CentOS 7 and use cloud images/cloud-init for installing guest VM?
Kernel-based Virtual Machine (KVM) is virtualization software for CentOS or RHEL 7. KVM turn your server into a hypervisor. This page shows how to setup and manage a virtualized environment with KVM in CentOS 7 or RHEL 7. It also described how to install and administer Virtual Machines (VMs) on a physical server using the CLI. Make sure that **Virtualization Technology (VT)** is enabled in your server 's BIOS. You can also run the following command [to test if CPU Support Intel VT and AMD-V Virtualization tech][1]
```
$ lscpu | grep Virtualization
Virtualization: VT-x
```
### Follow installation steps of KVM on CentOS 7/RHEL 7 headless sever
[![How to install KVM on CentOS 7 RHEL 7 Headless Server][3]][3]
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][4]:
`# 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
```
[![KVM default networking][5]][5]
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 avilable to other servers on your LAN, setup a a network bridge on the server that connected to the your LAN. Update your nic config file such as ifcfg-enp3s0 or em1:
`# vi /etc/sysconfig/network-scripts/enp3s0 `
Add line:
```
BRIDGE=br0
```
[Save and close the file in vi][6]. 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:
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. Type the following SSH port forwarding command from your client/desktop/macbook pro system:
Once you have ssh tunnel established, you can point your VNC client at your own 127.0.0.1 (localhost) address and port 5901 as follows:
[![][7]][7]
You should see CentOS Linux 7 guest installation screen as follows:
[![][8]][8]
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.
#### 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][9] 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.
# 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 VM
`# virsh net-dhcp-leases default`
[![CentOS7-VM1- Created][15]][15]
##### Log in to your VM
Use ssh command:
`# ssh vivek@192.168.122.85`
[![Sample VM session][16]][16]
### Useful commands
Let us see some useful commands for managing VMs.
#### List all VMs
`# virsh list --all`
#### Get VM info
```
# virsh dominfo vmName
# virsh dominfo centos7-vm1
```
#### Stop/shutdown a VM
`# virsh shutdown centos7-vm1`
#### Start VM
`# virsh start centos7-vm1`
#### Mark VM for autostart at boot time
`# virsh autostart centos7-vm1`
#### Reboot (soft & safe reboot) VM
`# virsh reboot centos7-vm1`
Reset (hard reset/not safe) VM
`# virsh reset centos7-vm1`
#### Delete VM
```
# virsh shutdown centos7-vm1
# virsh undefine centos7-vm1
# virsh pool-destroy centos7-vm1
# D=/var/lib/libvirt/images
# VM=centos7-vm1
# rm -ri $D/$VM
```
To see a complete list of virsh command type
```
# virsh help | less
# virsh help | grep reboot
```
### About the author
The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on [Twitter][17], [Facebook][18], [Google+][19].