Kvm
Introduction
This page will get you started with KVM under Linux, specifically Ubuntu
Packages
Note: these instructions were used/tested on ubuntu 24.04. YMMV!
Install these packages.
sudo apt-get install qemu-kvm libvirt-daemon-system libvirt-clients bridge-utils
I also find these other packages useful on a desktop.
sudo apt-get install virt-manager virt-viewer
Paths
Libvirt is the method we're using to manage the VMs. It wants to put disk images in the folder /var/lib/libvirt/images/ but I prefer /opt/kvm/ so I just relink it. This is totally optional and depends on your disk partition layout.
sudo rmdir /var/lib/libvirt/images
sudo mkdir -p /opt/kvm
sudo ln -s /opt/kvm /var/lib/libvirt/images
Permissions
Run this command to put your user in the libvirt group.
sudo adduser `whoami` libvirt
newgrp libvirt
The libvirtd service must be running, this command will (re)start it...or you could reboot.
sudo systemctl restart libvirtd
virt-manager
Now you can create a VM using the virt-manager GUI.
Run this command (or it can be found in the desktop applications as Virtual Machine Manager)
virt-manager
Click on File=>Add Connection and choose KVM/QEMU Localhost. It may already be there though.
Take a moment to get your preferences right. Edit > Preferences.
Personally I like to use VNC instead of Spice for the Graphics type.
Right-click on QEMU/KVM and choose New. This will bring up the VM creation wizard.
From here the rest is up to you but should be fairly self-explanatory.
virsh
If you are (more) comfortable at the command line, virsh is the way to go.
List all VMs
virsh list --all
Stop a VM
virsh destroy vmname
Start a VM
virsh start vmname
Start a VM with serial console
virsh start --console vmname
Enable serial console in a VM
The serial console is great for various reasons. Here's how to activate it on a debian or ubuntu VM. Run these commands from INSIDE the VM.
First make sure grub has the right settings.
sudo vi /etc/default/grub
GRUB_CMDLINE_LINUX_DEFAULT="console=ttyS0 console=tty0, 115200n8"
Save & quite, then regenerate the grub.conf
sudo update-grub
Enable getty(1) for ttyS0 so you can also login from the serial console.
systemctl enable serial-getty@ttyS0.service
systemctl start serial-getty@ttyS0.service
Now stop and start the VM with console (instructions above) and you'll see both the kernel startup entries (like dmesg) and get a Login: prompt to boot.