Ubuntu KVM KB ID 0001890
Problem
I’ve been looking at KVM for a couple of reasons, firstly people are looking at VMware alternatives, now there’s no ‘free version‘, and secondly the firm I work for have potentially a large KVM to VMware migration on the horizon, so I thought I’d build it on the test bench and see how best to address that migration scenario.
Note: This was written with Ubuntu version 24.04, which was current at the time of publication, if the experience has taught me anything it’s the commands and procedures may well change in future versions. If you are reading this in the distant future and something needs tweaking let me know below, so I can try to keep things up to date.
Solution : Ubuntu KVM
Update Ubuntu.
I’m assuming you’ve already got an Ubuntu server installed ready to go, the first task is to ensure its fully up to date.
sudo apt update && sudo apt upgrade -y
Go and have a coffee, when complete simply reboot the server.
[ -e /var/run/reboot-required ] && sudo reboot
Ubuntu KVM (CPU Checker)
All modern physical servers will now have the virtualisation CPU elements enabled in BIOS, It’s been many years since I had to go and enable them, but if you on an old piece of tin, or someone’s disabled them, you need to check they are available. Note: This is more a problem id you intent to run Ubuntu nested inside another hypervisor like VMware ESX, or Hyper-V where you have to manually expose the virtualisation elements to a guest VM (often called nested virtualisation).
To make sure, we install cpu-checker.
sudo apt install cpu-checker
Then to test the CPU run the kvm-ok command and ensure it responds KVM acceleration can be used.
sudo kvm-ok
Ubuntu Install KVM
Use the following command.
sudo apt -y install libvirt-daemon-system bridge-utils qemu-kvm libvirt-daemon
Then install the additional components and tools we may require.
sudo apt install virtinst libosinfo-bin virt-top libguestfs-tools
Finally ensure all is well run virsh version and ensure the components look like the following (note some may have newer versions depending on how far in the future you are following along).
virsh version
Ubuntu KVM: Install Cockpit
Cockpit is a web based GUI where you can directly interrace with Linux, you can create run and manage your virtual machines from command line, but this is a little easier for most people.
sudo apt install cockpit
When complete add the machines plugin (for managing virtual machines) and podman plugin (for managing containers).
sudo apt install cockpit-{machines,podman}
Then enable Cockpit to AutoStart with the host and check its status. Take note of the port it is running on (highlighted below, this is usually TCP port 9090).
sudo systemctl enable --now cockpit.socket systemctl status cockpit.socket
Connect to the Ubuntu KVM server using a web browser to port 9090 (https://{ip-address-or-host-name}:9090 and log in.
Select “Turn on administrative access” and supply your password to authenticate again.
Ubuntu KVM Creating Guest VMs
I prefer to have the ISO files that I will build my VMs from on the server itself, so I upload them into the /tmp directory on the Ubuntu host. Below I’m using WinSCP because its free and it’s simple to use,
In Cockpit navigate to virtual Machines > Create VM.
Enter the details, and the path to the ISO file you uploaded above > Create and Run.
Now if you select the server you get a nice VNC remote console which you can interact with to build and manage the server remotely.
What you will notice is at this point your VMS get an IP address from the KVM host which will NAT the traffic to the outside world, which is fine. But if you want to access these VMs FROM the outside world then you have a problem (no, routing the traffic back to the KVM server manually or adding static routes to your other devices does not work) Well it didn’t for me! So a more likely scenario is you want bridged networking, where your VMs will get an IP address on your live LAN. I’ll be showing you how to do that next