This guide runs through installing KVM and setting up a first VM on a CentOS (8) server, with no GUI.
Install QEMU & KVM
Install the virt
module and some additional helper packages:
dnf install @virt
dnf install virt-install virt-viewer libguestfs-tools
Now validate everything is OK:
virt-host-validate
Start & enable libvirtd
:
systemctl enable libvirtd.service
systemctl start libvirtd.service
Configure Networking
Public VMs
If VMs are to be available on public internet, this requires setting up bridged networking. Roughly:
- Disable
NetworkManager
if using CentOS <8 - Take the current public interface, copy IP information to a new bridge (
br0
) - Remove IP information from the public interface, make it bridge onto the
br0
interface - Remove default KVM network
virsh net-destroy default
Private VMs
The default setup is suitable, where each VM gets assigned an IP via DNSMasq and there are NAT rules which enable them to reach the internet.
Create a VM
Start the Install
First, get the install media/ISO from wherever.
virt-install \
--name my-magical-server \
--virt-type=kvm \
--vcpus=4 \
--memory=4096 \
--graphics=vnc \
--disk size=100 \
--os-variant=ubuntu20.04 \
--cdrom=/tmp/ubuntu-20.04.1-live-server-amd64.iso
disk
: size is in GBos-variant
: this comes fromosinfo-query os
, selected closest available
The command will do some stuff and then hang waiting for the install - at this point you need to get the VNC port, SSH forward it from your laptop and use a VNC client to complete the install.
# On the server:
virsh dumpxml my-magical-server | grep vnc
# Locally:
ssh host-server-address -L 8000:127.0.0.1:5902 <-- any port can do locally (8000)
Now fire up a VPC client (TigerVNC works/is cross platform), point it at the local address (127.0.0.1:8000
) and follow the install. Once complete, the virt-install
command running on the server will exit.
Connect to the VM
You can repeat the VNC method to gain access, but SSH is a much better option. Install SSH daemon on the box and use that for access (via the host if private network only).
Setup fixed IP Address
By default KVM instances receive IPs from dnsmasq
via DHCP - setting a static IP for a server can be done by updating the default network. First get the MAC address of the instance's network interface:
virsh dumpxml my-magical-server | grep "mac address"
Then add this to the network definition:
virsh net-update default \
add ip-dhcp-host "<host mac='MACADDRESS' name='my-magical-server' ip='IPADDRESS'/>" \
--live --config
Once done, you can either restart the instance or renew the DHCP request with (inside the container):
sudo dhclient -r && sudo dhclient