Installing KVM / QEMU on Ubuntu 24.04 and fixing the NAT

Once I had my Scaleway bare metal server up and running to my liking (see my post here), the next step in the process was to get KVM / QEMU working so I could transfer a number of virtual machines from my previous server to this equipment. One of the nice things of moving servers is to install the latest software version of products, in this case Ubuntu 24.04 and KVM/QEMU to escape eventual technical debt. On the other hand, I am always surprised when things do not work out of the box. After all, it was so easy last time on the previous version…

Case in point Ubuntu 24.04 and KVM/Qemu: Out of the box, KVM does NOT run on Ubuntu 24.04. When starting a virtual machine with a private IP address and hence requiring Network Address Translation (NAT), the startup of the VM fails with a rather ominous error message:

unexpected exit status 2: dnsmasq: failed to create listening socket for 192.168.122.1: Address already in use

What? Fortunately, I’m not the only one experiencing this issue and help on the net is close. It turned out that the named service on Ubuntu 240.04 has a new default that KVM doesn’t like. The fix: In /etc/bind/named.conf.options change the following configuration:

# add:
listen-on { 127.0.0.1; };

# Comment out:
//listen-on-v6 { any; };

Frankly, when I read how to fix this I was very skeptical that this would indeed be the issue. But after a reboot, the VM with NAT started just fine. I’m a bit puzzled how that could have slipped past quality control!?

And just for good measure, here’s the installation procedure for KVM/QEMU on Ubuntu 24.04:

### Install required packages
sudo apt install qemu-kvm libvirt-daemon bridge-utils virt-manager virtinst libvirt-daemon-system

### KVM access for normal user
sudo usermod -aG libvirt $(whoami)
sudo usermod -aG kvm $(whoami)
sudo systemctl enable --now libvirtd

### optional: check status 
### --> probably has some red lines (address already in use)
### We will FIX THIS below!

sudo systemctl status libvirtd

### Executable rights required throughout the chain to access this.
chmod -R +x /home/ubuntu

### Activate IPv4 routing. Required by KVM NAT!
sudo nano /etc/sysctl.conf

net.ipv4.ip_forward=1

### Activate BBR for better congestion handling, 
### also in sysctl.conf file!
net.ipv4.tcp_congestion_control=bbr

### named is not configured for KVM, change this...

sudo cp /etc/bind/named.conf.options /home/ubuntu
sudo nano /etc/bind/named.conf.options

# add:
listen-on { 127.0.0.1; };

# Comment out:
//listen-on-v6 { any; };

### Reboot, now it should work...
sudo reboot

### Now check libvirtd status check, should show green now :)
sudo systemctl status libvirtd 

And once this works, here’s how to get NAT port forwarding going for incoming connections in case that is required.