
Fixing QEMU/KVM Connection Errors on Ubuntu 20.04: A Step-by-Step Guide
Setting up a virtualization environment with QEMU/KVM on Ubuntu is a powerful way to manage virtual machines. Tools like Virtual Machine Manager (virt-manager) provide a user-friendly graphical interface, but sometimes you hit a common and frustrating snag: a connection error.
You might see a message like “Unable to connect to libvirt” or a permission-related error when trying to access QEMU/KVM. This usually indicates that your user account doesn’t have the necessary privileges to communicate with the libvirt daemon, the service that manages virtualization.
Fortunately, this is almost always a straightforward permissions issue that can be resolved in a few steps. This guide will walk you through the troubleshooting process to get you back up and running.
The Root of the Problem: User Permissions
By default, for security reasons, only the root user and users belonging to specific administrative groups can manage virtual machines. The core service, libvirtd, controls access to the virtualization host. To interact with it as a standard user, you must be a member of the libvirt group.
In many cases, users also need to be part of the kvm group to have direct access to the KVM kernel module for hardware-accelerated virtualization.
Step 1: Check Your User’s Group Membership
The first step is to see which groups your current user account belongs to. Open a terminal and run the following command:
groups
This will output a list of all the groups you are a member of. If you don’t see libvirt and kvm in the list, you’ve found the source of your connection problem.
Step 2: Add Your User to the Correct Groups
To fix this, you need to add your user to both the libvirt and kvm groups. Use the usermod command with the -aG flags, which appends your user to the specified groups without removing them from existing ones.
Replace your_username with your actual username:
sudo usermod -aG libvirt your_username
sudo usermod -aG kvm your_username
For example, if your username is alex, the commands would be:
sudo usermod -aG libvirt alex
sudo usermod -aG kvm alex
Step 3: Apply the New Group Membership
This is the most commonly missed step. Changes to your user’s group membership do not take effect in your current session. You must completely log out of your system and log back in for the new permissions to be applied. A full system reboot will also accomplish this.
After logging back in, you can verify that the changes were successful by running the groups command again. You should now see libvirt and kvm in the list.
At this point, try launching Virtual Machine Manager or using a command-line tool like virsh again. Your connection issues should be resolved.
Further Troubleshooting: Is the Service Running?
If you’ve corrected your group memberships and still can’t connect, the next step is to ensure the libvirtd service is active and running correctly.
Check the libvirtd Service Status
Use systemctl to check the status of the virtualization daemon:
sudo systemctl status libvirtd
Look for output that says active (running). If the service is inactive or has failed, you’ll need to start it manually.
Start and Enable the libvirtd Service
To start the service for the current session, run:
sudo systemctl start libvirtd
To ensure the service starts automatically every time you boot your system, which is highly recommended, enable it with this command:
sudo systemctl enable libvirtd
After starting the service, try connecting with virt-manager again.
Advanced Check: Socket Permissions
In rare cases, the problem might stem from incorrect permissions on the libvirt socket file itself. The libvirtd service communicates via a Unix socket, which must be accessible to members of the libvirt group.
You can check the permissions of the socket file with this command:
ls -l /var/run/libvirt/libvirtd.sock
The output should show that the file’s group is libvirt. If the permissions appear incorrect, restarting the libvirtd service often corrects them automatically:
sudo systemctl restart libvirtd
Editing the main configuration file at /etc/libvirt/libvirtd.conf can also resolve deeper issues, but be cautious when modifying system files. In most scenarios, the user group membership is the primary cause of connection failures. By ensuring your user is in the libvirt and kvm groups and then logging out and back in, you can solve the vast majority of QEMU/KVM connection errors on Ubuntu.
Source: https://kifarunix.com/how-to-fix-qemu-kvm-not-connected-error-on-ubuntu-20-04/


