KVM is a powerful hypervisor that's tightly integrated into Linux systems. It requires minimal resources, and it's free to use. As an added bonus, Red Hat is one of the primary developers behind KVM, so you can expect it to work well on RHEL 8 / CentOS 8.In this tutorial you will learn:
- How to Set Up a Network Bridge
- How to Install KVM
- How to Start KVM
- How to Create a VM
- How to Configure VNC
Software Requirements and Conventions Used
|Category||Requirements, Conventions or Software Version Used|
|System||RHEL 8 / CentOS 8|
|Other||Privileged access to your Linux system as root or via the |
|Conventions|| # - requires given linux commands to be executed with root privileges either directly as a root user or by use of |
How to Set Up a Network Bridge
You can absolutely set up KVM without a network bridge, but bridging allows you virtual machines to function like independent physical machines on your network. That alone makes one worth setting up.
Begin by creating a file at
/etc/sysconfig/network-scripts/ifcfg-br0. This file will contain everything that you'll need for your bridge configuration.
Next, open the file, and place the following configuration in the file. Obviously, modify it as you need to fit your network.
DEVICE=br0 TYPE=Bridge IPADDR=192.168.1.110 NETMASK=255.255.255.0 GATEWAY=192.168.1.1 DNS=192.168.1.1 ONBOOT=yes BOOTPROTO=static DELAY=0
Next, you're going to need to create a configuration for the interface that you want to bridge.
Create a new file at
eth0 to the name of your actual interface. Chances are, there's already something there. Modify that or delete it and place the following configuration in the file. Remember to change it to reflect you system's interface.
DEVICE=eth0 TYPE=Ethernet HWADDR=AA:BB:CC:DD:EE:FF BOOTPROTO=none ONBOOT=yes BRIDGE=br0
The regular interface is configured to use the bridge interface to connect with a static IP address. If you have multiple interfaces to bridge, you can use the same configuration with each one, changing the device and hardware address, to bridge them too.
When you're ready, you can restart the system for the bridge to take effect. RHEL 8 uses NetworkManager and only NetworkManager. Restarting it independently doesn't always work.
How to Install KVM
You're ready to install KVM now. There are a series of packages that you need, so install all packages with DNF.
# dnf install qemu-kvm qemu-img libvirt virt-install libvirt-client
Make sure that the KVM module is loaded now.
# lsmod | grep kvm
As long as you see the KVM modules, you'll be good to go.
How to Start KVM
You'll need to start the
libvirtd service in order actually create any virtual machines. Start and enable the service.
# systemctl start libvirtd # systemctl enable libvirtd
How to Create a VM
Now, you can start creating virtual machines to work with. Before you start, you should probably have a Linux install ISO somewhere to work with.
If you're doing this on a workstation, you can install
virt-manager, and use the graphical tools to get set up easily. That's probably not the case, though, so you'll be working with the shell and VNC.
There are plenty of options you can use when setting up your VMs. The command below is a good template that includes virtual CPU cores, memory, hard drive space, and some OS specific configuration.
# virt-install \ --virt-type=kvm \ --name Fedora29 \ --ram 4096 \ --vcpus=4 \ --os-variant=fedora29 \ --cdrom=/path/to/install.iso \ --network=bridge=br0,model=virtio \ --graphics vnc \ --disk path=/var/lib/libvirt/images/fedora29.qcow2,size=20,bus=virtio,format=qcow2
You should see something like the image above, letting you know that your VM is running and waiting for you to finish the installation. That part you can do over VNC.
How to Connect With VNC
Start by taking a look at which port your VM is using for VNC. You can do this easily by opening another terminal and running the following command.
# virsh dumpxml Fedora29 | grep vnc
Use the name that you assigned to your VM. Take note of the port number associated with the VM.
In order to access your VM from another computer, you're going to need to tunnel the port running your VM over SSH. It's not as complex as it sounds. From your client computer, run the following command, substituting the IP and port number from the server
$ ssh firstname.lastname@example.org -L 5901:127.0.0.1:5901
Now, on your client machine or workstation, open up your VNC client. Enter the localhost IP,
127.0.0.1, and the VNC port,
5901 in this case. As soon as you connect, you'll be able to see your VM's installer in the VNC client window. Finish the installation process of your VM. You'll be able to get back any time by starting the VM with
virsh and connecting over VNC.
You're now set up and ready to use KVM to create and configure as many VMs as your system will support. VNC will be your gateway for graphical machines, but you can avoid it entirely on servers,following the initial install.