Run Linux in KVM/QEMU

Overview

KVM (Kernel-based Virtual Machine) is the native Linux virtualization solution. Combined with QEMU and virt-manager, it offers near-native performance for running Linux guests on a Linux host.

What You Need

Install KVM and Tools

Ubuntu/Debian

sudo apt update
sudo apt install qemu-kvm libvirt-daemon-system libvirt-clients bridge-utils virt-manager
sudo usermod -aG libvirt,$USER
sudo usermod -aG kvm,$USER
# Log out and back in for group changes

Fedora/RHEL

sudo dnf install @virtualization
sudo systemctl enable --now libvirtd
sudo usermod -aG libvirt,$USER

Arch Linux

sudo pacman -S qemu-full virt-manager libvirt dnsmasq vde2 bridge-utils ebtables openbsd-netcat
sudo systemctl enable --now libvirtd
sudo usermod -aG libvirt,$USER

Verify Installation

# Check KVM modules are loaded
lsmod | grep kvm

# Should show kvm_intel or kvm_amd

Create a VM Using virt-manager (GUI)

  1. Launch Virtual Machine Manager from your applications menu
  2. Click the computer icon with "+" to create a new VM
  3. Select "Local install media" → Forward
  4. Browse to your Linux ISO file → Forward
  5. Choose OS type and version (or let it auto-detect) → Forward
  6. Allocate RAM and CPU cores → Forward
  7. Create or select virtual disk storage → Finish

The installer will launch — follow the prompts for your chosen distribution.

Create a VM Using Command Line (virt-install)

virt-install \
  --name ubuntu-server \
  --ram 4096 \
  --vcpus 2 \
  --disk size=25,format=qcow2 \
  --cdrom /path/to/ubuntu.iso \
  --os-type linux \
  --os-variant ubuntu24.04 \
  --network network=default \
  --graphics spice \
  --noautoconsole

Use virt-viewer ubuntu-server to connect to the VM console after creation.

Networking Options

NAT (Default)

The VM shares the host's IP. The host can access the VM, and the VM can reach the internet.

# Find VM's internal IP from the host
virsh net-dhcp-leases default

Bridged Networking

The VM gets its own IP on your physical network.

# Create a bridge (example for Ubuntu with Netplan)
sudo nano /etc/netplan/01-netcfg.yaml
# Add bridge configuration, then:
sudo netplan apply

Host-Only Network

VM can only communicate with the host — useful for isolated testing.

Storage Management

Disk Formats

Snapshots

# Create a snapshot
virsh snapshot-create-as ubuntu-server pre-install

# List snapshots
virsh snapshot-list ubuntu-server

# Revert to a snapshot
virsh snapshot-revert ubuntu-server pre-install

Add Extra Disk

# Create new disk image
qemu-img create -f qcow2 /var/lib/libvirt/images/extra-disk.qcow2 10G

# Attach to running VM
virsh attach-disk ubuntu-server /var/lib/libvirt/images/extra-disk.qcow2 vdb --subdriver qcow2

Advanced Configuration

Passthrough USB Device

# List USB devices
virsh nodedev-list --tree

# Attach USB to VM
virsh attach-device ubuntu-server usb.xml --config --live

Enable VRAM for Graphics

Edit VM XML: virsh edit ubuntu-server, find the graphics section, and add:

<video>
  <model type='virtio' vram='16384' heads='1' primary='yes'/>
</video>

Clone a VM

virt-clone --original ubuntu-server --name ubuntu-test --auto-clone

Troubleshooting