How to Install QEMU and Near-Native VMs on Linux: Ultimate Guide

How to Install QEMU and Near-Native VMs on Linux: Ultimate Guide


Hi all, welcome to my blog, MusaBase! In this comprehensive guide, I`ll walk you through installing QEMU and creating Kernel-based Virtual Machines(KVM) that deliver near-native performance on Linux. Traditionally, many users have relied on separate installation to run different operating system, which requires restarting your machine every time you switch. With virtualization, you can run multiple enviornments simultaneously with performance that rivals native hardware. In this guide I`m using Arch Linux with KDE Plasma, but you can follow this guide on any linux distribution with only minor adjustments to package names or your package manager.







What is QEMU?

QEMU, short for Quick Emulator is a free, open-source emulator and virtualizer that can run entire operating system or individual programs.It leverages dynamic binary translation to support multiple CPU architectures.Developers user it for cross-platform testing, debugging, and isolated deployments.When paired with KVM (Kernel-based Virtual Machine) , it delivers near-native performance.Its flexibility, protability, and cost-effectiveness make it a top choice over proprietary solutions.




Prerequisites

To run QEMU effectivley on Linux, you need a recent 32- or 64-bit Linux distribution. Many full-featured distros, such as Ubuntu, Linux Mint, Fedora and openSUSE typically come with standard build tools (like GCC, make, and pkg-config). However, if you`re using a minimalor non-development-focused system (or a distro like Arch Linux without the base-devel group pre-installed), you may need to manually insatll these packages along with necessary libraries like glib2.0, libfdt, pixman and zlib.
Specifications:

  •   Processor: Intel or AMD x86-64-bit compatible with virtualization support.
  •   Ram: 8GB or more for higher performance.
  •   Hard-Drive: Based on your needs. Basically you will be allocating space according to the OS or program you will try to run.
  •   Operating System: Any recent Linux distribution 32-bit or 64-bit.

If you need detailed information about the architectures and technologies required for creating a virtual machine with Hardware Acceleration



Most modern CPUs even back from 2011-2012 era include built-in virtualization extensions. Many ARM64 processors from 2012 onward also support these features.


If you are not sure whether your CPU supprots virtualization, follows these steps:

  • Reboot your pc.
  • During startup, press the BIOS/UEFI key (commonly F2, F10, F12, Del, Esc, depending on your motherboard).
  • Look for sections labeled Advanced, CPU Configuration, Processor Settings, System Configuration, or Security.
  • Locate Virtualization Options:
    • For Intel CPUs: Look for Inetl Virtualization Technology or VT-x.
    • For AMD CPUs: Look for AMD Virtualization (AMD-V) or SVM Mode.
    • Some systems might also showIntel VT-d (for directed I/O virtualization).
  • Enable Virtualization.
  • Save Settings and Exit.
  • Reboot.

Now, in your linux terminal, run::

LC_ALL=C lscpu | grep Virtualization
  • This command checks whether your CPU support hardware virtualization.
  • For Intel CPUs, this command will return: Virtualization: VT-x.
  • For AMD CPUs, this command will return: Virtualization: AMD-V.
  • This confirms that hardware-assisted virtualization is supported, which significantly improves performance compared to software emulation.

If your CPU doesn`t support virtualization, you can still create and use VMs but then they will rely solely on Software Emulation.




Step 1: Installing QEMU and Core Virtualization Packages

Now it`s time to install the QEMU package along its full suite of virtualization tools. Although installing QEMU alone will work, adding a few extra packages can significantly enhance your virtualization experience.

Installing Packages

For the best daily VM experience, I recommand installing the following packages. Use the appropriate command for your Linux distribution:


Arch-based distros:

sudo pacman -S qemu-full qemu-img libvirt virt-install virt-manager dnsmasq libosinfo edk2-ovmf

Ubuntu/Debian-based distros:

sudo apt install qemu-system-x86 qemu-utils libvirt-daemon-system libvirt-clients virt-manager virtinst dnsmasq-base libosinfo ovmf

Red Hat-Based distros:

sudo dnf install qemu-kvm qemu-img libvirt virt-install virt-manager dnsmasq libosinfo ovmf

Note: Replace the pacman, apt, dnf package manager command with the one used on your Linux distribution.

Package Name Reference Table

Since pacakge names can vary between distributions, refer to the table below for a quick overview:

Arch-based Ubuntu/Debian-based Red Hat-based
qemu-full qemu-system-x86,
qemu-utils
qemu-kvm,
qemu-img
qemu-img qemu-utils qemu-img
libvirt libvirt-daemon-system,
libvirt-clients
libvirt
virt-install virtinst virt-install
virt-manager virt-manager virt-manager
dnsmasq dsnmasq-base dnsmasq
libosinfo libosinfo-bin libosinfo
edk2-ovmf ovmf ovmf
  • qemu-full: Provides a comprehensive set of QEMU's capabilities for emulating varios hardware architectures. It is the core engine for running virtual machines.
  • qemu-img: A utility for managing disk image. i.e, create, convert, and modify virtual hard drive files essential for storing VM data.
  • libvirt: A virtualization management API and daemon (libvirtd). It provides a standardized way to interact with various virtualization technologies, including QEMU/KVM.
  • virt-install: A command-line tool for creating new virtual machines from scratch, handling tasks like configuring storage, network settings, and installation media.
  • virt-manager: A graphical user interface for managing virtual machines. It provides an easy-to-use way to create, configure, and monitor VMs, making virtualization more accessible.
  • dnsmasq: A lightweight DNS forwarder and DHCP server used in virtualized enviornment to assigning IP addresses and resolving domain names for guest VMs.
  • libosinfo: A library provides detailed information about operating system, aiding virtualization management tools in automatically configuring VMs.
  • edk2-ovmf: OVMF (Open Virtual Machine Firmware) is a UEFI (Unified Extensible Firmware Interface) firmware for virtual machine. It enables modern operating systems that require UEFI support to run in a virtualized environment.

Note: Avoid running VMs as root user to maintain security and system stability.




Step 2: Enabling and Configurinng Core Virtualization Services

Now we need to enable essential services and adjust configuration file forthe packages we installed in Step 1. Follow these steps:

2.1. Enable Virtualization Services:
  • Open your terminal and run the following command to start and enable the libvirtd service immediately:
sudo systemctl enable --now libvirtd
  • libvirtd: Is the libvirt daemon, responsible for managing virtual machines (VMs). Enabling it ensures your system can control VMs properly.

Note: If your init system is not systemd, you may need to use different commands to enable libvirtd service.

For SysVinit:

  • Starting the libvirtd service:
sudo /etc/init.d/libvirtd start
#or
sudo service libvirtd start
  • Enabling the service so it automatically start on boot:
    • This varies significantly between distributions.
    • On Debian-based systems:
sudo update-rc.d libvirtd defaults
#or
sudo update-rc.d libvirtd enable
    • On Red Hat-based systems:
sudo chkconfig libvirtd on
The "defaults" key words in the debian command, will set the run levels that the service will start in. The chkconfig command, simply turns the service on, for the default run levels.

For OpenRC:

  • Starting the service:
sudo rc-service libvirtd start
  • Enabling the service so it automatically start on boot:
sudo rc-update add libvirtd default
    Important Notes:
  • The SysVinit "enable" step is the most vairable. Distributions using SysVinit have implemented their own vairations of how runlevels and service startup are managed.
  • These commands assume the libvirtd service has been properly installed. If the service files are missing, these commands will fail.

2.2. Add User to the Libvirt Group:

By default, libvirt requires root privileges to manage VMs. Adding your user to the libvirt group allows you to manage virtual machines without sudo, which is both safer and more convenient.

  • To add User to libvert group, run the command:

Arch-based distros & Red Hat-Based distros:

sudo usermod -aG libvirt $USER

Ubuntu/Debian-based distros:


  • If sudo usermod -aG libvirt $USER doesn`t work:
sudo adduser $USER libvirt
Log out and back in to apply group changes.
    Command Purpose:
  • sudo usermod: This is the command used to modify a user`s account information.
  • -aG libvirt:
    • -a: appends the specified group to the user`s existing group.
    • -G libvirt: specifies the libvirt group.
  • $USER:This variable expands to the current user`s username.

2.3. Load KVM Kernel Modules:

These modules are required for KVM to work because they enable the CPU’s hardware virtualization features. If these modules aren’t loaded, your system can’t use KVM, and virtualization won’t work properly.

  • For Intel CPUs, type:
sudo modprobe kvm-intel

  • For AMD CPUs, type:
sudo modprobe kvm_amd
  • kvm_intel or kvm_amd enables the CPU`s hardware virtualization features.

2.4. Configure Libvirt (Optional):
  • Open the libvirt configuration file with your preferred text editor (e.g., kate or nano):
kate /etc/libvirt/libvirtd.conf
OR
nano /etc/libvirt/libvirtd.conf
  • Use the editor`s search function ctrl + f to locate and uncomment/modify these lines by removing the leading (#):
unix_sock_group = "libvirt"
unix_sock_rw_perms = "0770"
  • unix_sock_group = "libvirt": Ensures that the libvirt group has access to the daemon.
  • unix_sock_rw_perms = "0770": Sets proper read/write permissions for the libvirt group.
  • These changes allow non-root users to control virutal machines securely.

2.5. Restart the Libvirt Service:
  • Apply any configuration changes by restarting the libvirtd service:
sudo systemctl restart libvirtd

If you`re using a different init system, adjust the command accordingly. For example:

  • SysVinit:
sudo service libvritd restart
#OR
sudo /etc/init.d/libvirtd restart
  • OpenRC:
sudo rc-service libvirtd restart
These command will:
  • Stops and then starts the libvirtd service: This ensures that any changes made to the configuration file (/etc/libvirt/libvirtd.conf) are applied.
  • Reinitializes the libvirt daemon: It refreshes the virtualization service without requiring a full system reboot.
  • Restarts any related background processes: This can help fix issues where virtual machines fail to start due to service misconfigurations or crashes.
  • Ensures the latest settings are in effect: If you`ve made modifications to permissions, socket groups, or other configurations, restarting the service is necessary for them to take effect.




Step 3: Launching a Virtual Machine using QEMU with Virt-Manager
Launch Virt-Manager:
  • Finally, test your setup by launching virt-manager with:
virt-manager
This command opens the GUI for managing your virtual mahcines.

We have now successfully configured a working virtual machine environment. By following the above steps, we ensure that KVM is properly enabled, users have the right permissions, and virtual machines can be created and managed efficiently.

Understanding QEMU Basics

Before you start using QEMU in depth, here are a few important points to understand first:

QEMU Core:
  • QEMU itself is primarily a command-line tool. This means that its fundamental operations relies on typing commands into a terminal. So in it`s basic form, these is no Graphical User Interface.
  • However, QEMU does have UI subsystem, that allows for different user interfaces to be creatd.
Graphical User Interfaces (GUI) and Launchers:
  • Several GUI are available to simplify QEMU usage. The most prominent example is virt-manager.
  • virt-manager provides a user-friendly interface for managing virtual machines, and it heavily relies on QEMU and libvirt.
  • Therefore, while QEMU`s core is commadn-line based, it`s frequently used with GUIs that make it more accessible.



Step 4: Booting QEMU with an OS Image (Optional)

Now, to test if our setup acutally works by installing an Operating System in our Virtual Machine. This process involves:

  • Creating a Virtual hard drive for the OS installation.
  • Attaching anOS Image.
  • Allocating Ram and CPU cores.
  • Passing USB-Devices(mouse, keyboard, audio devices).
  • Enabling hardware acceleration for better performance.

I`ll demonstrate using Windows 11, but you can use any OS.ISO file. You can also try recovery images (usually in .bin or .img formats) with a few tweaks.

4.1. Getting things ready

Before booting QEMU with the Widnows 11 ISO, we need 2 files:

  • Windows 11 ISO file.
  • Virtual hard drive.

I have the ISO file already so i will create a virtual hard-drive.

But before that i would recommend creating a separate empty folder for each OS in which you will store operating system image file, virtual hard disk file and any other files needed for OS to run in QEMU. For instance, i have an empty folder named VirtualWindows11. I will cd into this folder from my terminal like this:

cd /mnt/sdc1/VirtualWindows11

Now, in this folder, i will create a virtual disk of 64GB of storage for Windows 11.

4.2. Creating Virtual Drive with QEMU
  • Next, create a 64GB virtual disk for Windows 11 with the following command:
qemu-img create -f qcow2 virtualdrive.qcow2 64G
  • qemu-img create: The QEMU disk utility, used to create, modify, and manage virtual disk images.
  • create: Instructs qemu-img to create new disk image.
  • -f qcow2: Specifies the virtual disk format as qcow2 (QEMU Copy-On Write version 2). You can change the format to raw, VDI or VHD depending on your needs. I chose qcow2 because its supprots compressions and its efficient.
  • virtualdrive.qcow2: The filename for your virtual hard disk. The .qcow2 extension clarifies its format. Ensure the name has no spaces.
  • 64G: The maximum virtual size of the disk. The G character specifies that this is going to be the 64 gigabytes of stroage. Adjust the size as needed.
4.3. Verifying Required Files

Ensure you have the following additional files (needed for UEFI support):

  • OVMF.fd
  • OVMF_VARS.fd

Now, i have already saved my windows11.iso file inside my VirtualWindows11 folder with two additional .fd format files, these files are needed to run modern operating systems that require UEFI support, such as recent versions of Windows and Linux.

Please see the image below for clarification:

I have got total of 4 files here:
  • OVMF.fd: This file contains the actual UEFI firmware code. It essentially acts as the virtual machine`s "motherboard firmware". It is the equivalent of a computer bios, or uefi chip.
  • OVFM_VARS.fd: This file stores UEFI variables. UEFI variables are settings that the firmware and the operating system use to store configuration information. It is the equivalent of the cmos battery backed storage of a computer bios/uefi.
  • virtualdrive.qcow2: This is the virtual hard-disk we just created.
  • Win.11.Pro.26100.3476.iso: This is .ISO image for Windows 11.

These 2 additional files comes with the ovmf package and usually located in directories like:

  • usr/share/ovmf/x64/
  • usr/share/ovmf/
  • usr/share/qemu-firmware/

If you can`t locate them, run:

sudo find / -name OVMF_VARS.fd
#OR
sudo find / -name OVMF_CODE.fd

Note: On some distributions (for example, Arch Linux) the filenames might be slightly different (e.g., OVMF.4m.fd and OVMF_VARS.4m.fd). In such cases, remove the ".4m" for simplicity.


4.4. Install Windows 11 in QEMU

Now that we have everything read, let`s install Windows 11 on QEMU.

  • Open terminal and type the following command:
qemu-system-x86_64 \
  -cpu host -enable-kvm -smp 2 -m 8G \
  -drive if=pflash,format=raw,unit=0,file=OVMF.fd,readonly=on \
  -drive if=pflash,format=raw,unit=1,file=OVMF_VARS.fd \
  -drive file=virtualdrive.qcow2,format=qcow2 \
  -drive file=Win11Pro.iso,media=cdrom,readonly=on \
  -device virtio-vga-gl -display "gtk,gl=on,show-cursor=on" \
  -netdev user,id=net0 -device virtio-net-pci,netdev=net0 \
  -usb -device usb-tablet -device usb-kbd \
  -device ich9-intel-hda -device hda-duplex
  
  • qemu-system-x86_64: This runs QEMU using the x86_64 architrcture, meaning it will emulate a 64-bit pc.
  • -cpu host Uses the same CPU as the host machine for better performance.
  • -enable-kvm: Enables KVM (Kernal-based Virtual Machine) for hardware acceleration.
  • -smp 2: Allocates 2 CPU cores to the VM. Adjust this value as needed.
  • -m 8G: Allocates 8GB of RAM to the VM. Adjust this value as needed.
  • -drive if=pflash,format=raw,unit=0,file=OVMF.fd,readonly=on
    -drive if=pflash,format=raw,unit=1,file=OVMF_VARS.fd:
    • OVMF.fd: This file is the UEFI firmware for the VM (instead of a legacy BIOS).
    • OVMF_VARS.fd: Stores UEFI variables (boot settings, secure boot status, etc).
    • if=pflash: Treats these as flash storage for firmware emulation.
    • readonly=on: Ensures the firmware itself is not modified.
  • -drive file=virtualdrive.qcow2,format=qcow2
    -drive file=Win.11.Pro.iso,media=cdrom,readonly=on
    • virtualddrive.qcow2: This is the main virtual disk (created earlier).
    • Win.11.Pro.iso: This attaches a Windows 11 installation ISO as a virtual CD-ROM.
  • -device virtio-vga-gl: Uses a VirtIO GPU with OpenGL acceleration for better performance.
  • -display "gtk,gl=on,show-cursor=on": Uses GTK UI, enables OpenGL, and ensures the cursor is visible.
  • -netdev user,id=net0: Creates a user-mode network, which allows internet access.
  • -device virtio-net-pci,netdeb=net0: Uses a VirtIO network adapter for better performance.
  • -usb: Enables USB support.
  • -device usb-tablet: Improves mouse input accuracy (prevents cursor jumping in VMs).
  • -device usb-tablet: Improves mouse input accuracy (prevents cursor jumping in VMs).
  • -device ich9-intel-hda -device hda-duplex: Integrates an Intel High Definition Audio (HDA) controller and a duplex HDA codec, providing audio input and output capabilities for the VM.

    Below QEMU options/flags are completely optional so if you don`t want to use them, just remove these options from the qemu-system command.
  • "show-cursor=on".
  • netdev user,id=net0.
  • -device virtio-net-pci,netdev=net0.
  • -device usb-kbd.

4.5. Optional: Fixing Internet Connection in Windows 11 VM

If you follow along and install Windows 11 with the optional flags, you may experience internet connection issues inside the VM. This is because Windows needs VirtIO Network Drivers to properly recognize and use the virtual network adapter. You can easily fix this by installing the VirtIO drivers within your virtual machine. These drivers are available from the Fedora Project`s website and are usually distributed as an ISO image.

Copy and paste the link below. It will take you to fedorapeople.org website page and from there you can get the ISO image for drivers.

https://fedorapeople.org/groups/virt/virtio-win/direct-downloads/archive-virtio/

Make sure you download the latest version of the ISO file (the filename may be something like virtio-win-0.1.266.iso).

Once downloaded, copy the virtio-win-0.1.266.iso file into your VirtualWindows11 Folder or Directory and run QEMU again with this new VirtIO.Win.iso image.

  • We just need to add an extra -drive command with previous qemu-system command:
-drive file=virtio-win-0.1.266.iso,media=cdrom,readonly=on
  • The full command will now look like this:
qemu-system-x86_64 \
  -cpu host -enable-kvm -smp 2 -m 8G \
  -drive if=pflash,format=raw,unit=0,file=OVMF.fd,readonly=on \
  -drive if=pflash,format=raw,unit=1,file=OVMF_VARS.fd \
  -drive file=virtualdrive.qcow2,format=qcow2 \
  -drive file=virtio-win-0.1.266.iso,media=cdrom,readonly=on \
  -device virtio-vga-gl -display "gtk,gl=on,show-cursor=on" \
  -netdev user,id=net0 -device virtio-net-pci,netdev=net0 \
  -usb -device usb-tablet -device usb-kbd \
  -device ich9-intel-hda -device hda-duplex

Install Virtio-Win-Drivers in QEMU

Follow these steps:

  • Open My Computer.
  • There you should have two Cd-Drives one will be containing Windows11Installer.ISO and the second one containing VirtIO-Win.
  • Open the CD-Drive which contains virtio-win image, it will named something like CD-Drive virtio-win etc.
  • Open virtio-win-gt-x64.exe file.
  • Select Network and press Next to install.
  • It will take short time and you would have network drivers installed for virtio-network.
  • Restart the VM or Windows inside VM so Windows can configure the changes.

And there you have it, everything is configured and running. The ViriIO-Win utiliy offers various drivers for different scenarios when running Windows in a virtual machine. It can even pass throuigh GPU features (which i`ll cover in another article). Now you have a near-native running virtual machine in Linux. Feel free to experiment with different QEMU flags or options to see how your machine or the operating system inside reacts.




QEMU Terminal Commands and Options

Here are some common, everyday QEMU terminal options:

  • Basic System Emulation:
qemu-system-x86_64 # Emualtes a 64-bit x86(AMD64/Intel64) system.
qemu-system-arm # Emulates a 32-bit ARM system.
qemu-system-aarch64 # Emulates a 64-bit ARM system.
  • System Configuration:
-m "size": Allocate RAM (e.g., -m 4G for 4GB).
-smp "n": Set CPU cores (e.g., -smp 4 for 4 cores).
-cpu "model": Specify CPU model (e.g., -cpu host for passthrough, -cpu qemu64)
-machine "type": Select emulated machine (e.g., -machine q35, -machine virt,accel=kvm).
-accel "type": Enable acceleration (e.g., -accel kvm, -accel tcg).
-name "Windows": Name the VM (for monitoring).
  • Storage Options:
-drive file=disk.qcow2,format=qcow2,if=virtio
file="path/to/file": Disk Image path.
format="type": Image format (e.g., qcow2, raw).
if="interface": Interface type (e.g., virtio, ide, scsi).
media="type": disk or cdrom.
-accel "type": Enable acceleration (e.g., -accel kvm, -accel tcg).
-enable-kvm: Enable KVM acceleration (legacyalternative to -accel kvm).
-name "Windows": Name the VM (for monitoring).
-cdrom "file": Attach a CD/DVD image (e.g., -cdrom ubuntu.iso).
-boot "order": Set boot order (e.g., -boot order=c,menu=on).
  • Display/Graphics:
-display "type": Select display backend (e.g., gtk, sdl, none, cocoa).
-vga "model": Set VGA card (e.g., virio, std, qxl).
-nographic: Disable graphical outpu (use terminal).
-spice "options": Enable SPICE remote desktop (e.g., -spice port=5900).
-device "usb-tablet": Improve moue integration (aviods cursor drift).
  • Networking:
-netdev "type",id="name": Define a network backend.
Types: user (NAT), tap (bridged), socket.
Example: -netdev user,id=net0 -device virtio-net-pci,netdev=net0.
-nic "options": Simplified network setup (e.g., -nic user, model=virtio).
-net user -net nic: Legace networking (NAT with virtual NIC).
  • USB/Peripherals:
-usb: Enable USB bus.
-device usb-host,vendorid=0x1234,productid=0x5678: Passthrough a USB device.
-device usb-mouse: Emulate a USB mouse.
  • Audio:
-audiodev "backend",id="name": Configure audio backend (e.g., pa, alsa).
-soundhw "model": Emulate audio hardware (e.g., hda, ac97).
  • Advanced Options:
-kernal "vmlinuz": Directly boot a linux kernal.
-initrd "file": Specify an initramfs.
-append "cmline": Kernel command-line arguments.
-monitor "type": Expose QEMU monitor (e.g., -monitor stdio).
-snapshot: Run in temporary mode (no disk changes).
-loadvm "snapshot": Restore a saved VM state.
Example Commands
1. Basic Boot:
qemu-system-x86_64 -smp 2 -m 8G -drive file=OS.qcow2,format=qcow2 -enable-kvm

2. Boot from ISO:
qemu-system-x86_64 -m 8G -cdrom steamos.iso -drive file=disk.qcow2,format=qcow2

3. Networked VM:
qemu-system-x86_64 -nic user,model=virtio -drive file=disk.qcow2,format=qcow2

  • For a full list, check the QEMU man page (man qemu-system-x86_64) or QEMU Documentation. Options may vary slightly between QEMU versions.



Afterwards

Well, if you`ve followed the steps above, you now have a full functional QEMU/KVM virtual machine enviornmnet on your Linux system. Unlike the old method of having seprate installations for each operating system, this virtual machine setup allows you to run multiple enviornmnets concurrently. This means you can easily test and use different systems without the need to reboot.At this point, your setup is not only capable of running various operating systems (such as Windows 11) with near-native performance, but it also provides a solid foundation for futher customization and advanced virtualizaion techniques.

Next Steps:
  • Experiment with Advanced Options:
    • Test different QEMU flags and configurations to understand how each impacts performance and functionality. This knowledge will help you tailor your virtual machines to specific use cases, whether for development, testing, or production.
  • Monitor and Optimize:
    • Use performance monitoring tools and logging options (e.g., -d exec,cpu and -D logfile) to identify bottlenecks. Fine-tuning resources like CPU allocation, memory, and disk I/O can significantly improve efficiency.
  • Plan for Future Scalability:
    • As your virtualization needs grow, consider integrating additional tools like virt-manager, libvirt and advanced networking setups to build a scalable, production-grade environment.

Don`t stop here test with it, make tweaks and push QEMU to its limit.
1O1 out, I`ll see you around!




FIN!!!

Load comments