{"id":3526,"date":"2023-03-28T06:31:45","date_gmt":"2023-03-28T10:31:45","guid":{"rendered":"https:\/\/dft.wiki\/?p=3526"},"modified":"2026-07-14T15:52:52","modified_gmt":"2026-07-14T19:52:52","slug":"kvm-and-qemu-cheat-sheet","status":"publish","type":"post","link":"https:\/\/dft.wiki\/?p=3526","title":{"rendered":"KVM and QEMU Cheat Sheet"},"content":{"rendered":"<p><strong>KVM<\/strong> (Kernel-based Virtual Machine) and <strong>QEMU<\/strong> (Quick EMUlator) are both open-source virtualization technologies that run on Linux, but they differ in how they accomplish the same tasks.<\/p>\n<p><strong>KVM<\/strong> [<a href=\"https:\/\/libvirt.org\/\">Link<\/a>] is a Linux kernel module that turns the kernel into a hypervisor using hardware virtualization features (Intel VT-x and AMD-V). <strong>QEMU<\/strong>, on the other hand, is an emulator capable of emulating different architectures and peripherals, at the cost of additional overhead. It can also work alongside KVM to take advantage of hardware virtualization.<\/p>\n<hr \/>\n<p><strong>KVM + QEMU<\/strong><\/p>\n<ul>\n<li>Install steps.<\/li>\n<\/ul>\n<pre>sudo apt-get update\r\nsudo apt-get install qemu-kvm libvirt-daemon-system libvirt-clients bridge-utils virt-manager -y\r\nsudo reboot<\/pre>\n<ul>\n<li>Checking<\/li>\n<\/ul>\n<pre>egrep -c '(vmx|svm)' \/proc\/cpuinfo\r\nsudo systemctl status libvirtd<\/pre>\n<p><strong>Note:<\/strong> if it returns <strong>0<\/strong>, virtualization must be enabled in the BIOS. The <code>libvirtd<\/code> service must be <strong>active (running)<\/strong>. Users that will manage VMs need to be members of the <code>libvirt<\/code> and <code>kvm<\/code> groups, or use <code>sudo<\/code>.<\/p>\n<p>GUI Interface<\/p>\n<pre>nohup sudo virt-manager &amp;<\/pre>\n<hr \/>\n<p><strong>KVM<\/strong><\/p>\n<ul>\n<li>Listing all VMs<\/li>\n<\/ul>\n<pre>virsh list --all<\/pre>\n<ul>\n<li>Managing VMs<\/li>\n<\/ul>\n<pre>virsh start vmName\r\nvirsh suspend vmName\r\nvirsh resume vmName\r\nvirsh shutdown vmName\r\nvirsh destroy vmName\r\nvirsh undefine vmName\r\nvirsh console vmName<\/pre>\n<ul>\n<li>Snapshots of a VM<\/li>\n<\/ul>\n<pre>virsh snapshot-list vmName\r\nvirsh snapshot-create-as --domain vmName --name snapshotName --description \"Add description or comments here.\"\r\nvirsh snapshot-revert vmName snapshotName\r\nvirsh snapshot-delete vmName snapshotName<\/pre>\n<ul>\n<li>Getting information about OS Variants.<\/li>\n<\/ul>\n<pre>virt-install --os-variant list\r\nvirt-install --osinfo ubuntu22.04<\/pre>\n<ul>\n<li>Deploying a Kali VM from an ISO with VNC and a bridged network.<\/li>\n<\/ul>\n<pre>virt-install --name Kali --memory 4096 --vcpus 4 --os-variant debian11 --network bridge=virbr0 --graphics vnc --cdrom kali.iso --disk size=100,format=qcow2<\/pre>\n<p><strong>Note:<\/strong> to proceed with the installation, use a VNC client from the host machine connecting to <strong>127.0.0.1<\/strong>, or use <strong>SSH<\/strong> to port forward port <strong>5900<\/strong>.<\/p>\n<ul>\n<li>Connecting via VNC<\/li>\n<\/ul>\n<pre>virsh vncdisplay vmName<\/pre>\n<p>In its VM configuration file it will look like the following:<\/p>\n<pre>&lt;graphics type='vnc' port='-1' autoport='yes'&gt;\r\n  &lt;listen type='address'\/&gt;\r\n&lt;\/graphics&gt;<\/pre>\n<p>Configure it accordingly. For example:<\/p>\n<pre>&lt;graphics type='vnc' port='5900' autoport='no' tls='yes'&gt;\r\n  &lt;listen type='address' address='192.168.10.10'\/&gt;\r\n&lt;\/graphics&gt;<\/pre>\n<ul>\n<li>Cloning a VM<\/li>\n<\/ul>\n<pre>virt-clone --original vmName --name vmNameCloned --auto-clone<\/pre>\n<p>OR<\/p>\n<pre>virt-clone --original vmName --name vmNameCloned --file \/var\/lib\/libvirt\/images\/vmNameCloned.qcow2<\/pre>\n<p><strong>Note:<\/strong> <code>\/var\/lib\/libvirt\/images<\/code> is where virtual disks are stored by default, and <code>\/etc\/libvirt\/qemu\/<\/code> is where VM configuration files are located.<\/p>\n<ul>\n<li>Configuring a VM<\/li>\n<\/ul>\n<pre>virsh dominfo vmName\r\nvirsh autostart vmName\r\nvirsh edit vmName<\/pre>\n<ul>\n<li>Managing Networks<\/li>\n<\/ul>\n<pre>virsh net-list --all\r\nvirsh net-define \/PATH\/networkConfig.xml\r\nvirsh net-start networkName\r\nvirsh net-autostart networkName<\/pre>\n<p>Example for adding a bridged network (<code>bridged-network<\/code>) by creating a bridge interface (<code>br0<\/code>) to a physical interface (<code>ens192<\/code>) on the host, which gives direct access to the <strong>LAN<\/strong>.<\/p>\n<pre>nmcli connection add type bridge autoconnect yes con-name br0 ifname br0\r\nnmcli connection modify br0 bridge.stp no\r\nnmcli connection add type bridge-slave autoconnect yes con-name ens192-br slave-type bridge master br0 ifname ens192\r\nnmcli connection modify br0 ipv4.method auto\r\nnmcli connection modify br0 ipv6.method ignore\r\nnmcli connection up br0\r\nnano bridged-network.xml<\/pre>\n<pre>&lt;network&gt;\r\n  &lt;name&gt;bridged-network&lt;\/name&gt;\r\n  &lt;forward mode='bridge'\/&gt;\r\n  &lt;bridge name='br0'\/&gt;\r\n&lt;\/network&gt;<\/pre>\n<pre>virsh net-define bridged-network.xml\r\nvirsh net-autostart bridged-network\r\nvirsh net-start bridged-network<\/pre>\n<p><strong>Note:<\/strong> out of the box, the <code>default<\/code> network may not be started or set to auto-start. As an alternative to the command line, KVM includes a <strong>GUI<\/strong> called <strong>Virtual Machine Manager<\/strong> for hosts running a graphical environment.<\/p>\n<hr \/>\n<p><strong>QEMU<\/strong><\/p>\n<ul>\n<li>Listing all VMs<\/li>\n<\/ul>\n<p>Because QEMU emulates virtual machines, each one will appear as a separate process and can be identified and killed using <code>htop<\/code>, for example.<\/p>\n<ul>\n<li>Creating a Disk<\/li>\n<\/ul>\n<pre>qemu-img create -f qcow2 \/PATH\/vmImage.qcow2 100G<\/pre>\n<ul>\n<li>Attaching a Disk<\/li>\n<\/ul>\n<pre>virsh attach-disk vmName --source \/var\/lib\/libvirt\/images\/vmImage.qcow2 --target vdb --persistent --subdriver qcow2 --driver qemu --type disk<\/pre>\n<ul>\n<li>Launching a VM &#8211; Basic Syntax\n<ul>\n<li><strong>[arch]<\/strong>\n<ul>\n<li><code>x86-64<\/code> for x86 64-bit.<\/li>\n<li><code>i386<\/code> for x86 32-bit.<\/li>\n<li><code>aarch64<\/code> for ARM 64-bit.<\/li>\n<li><code>arm<\/code> for ARM 32-bit.<\/li>\n<li><code>ppc<\/code> for PowerPC.<\/li>\n<li>and more.<\/li>\n<\/ul>\n<\/li>\n<li><strong>[memory]<\/strong>\n<ul>\n<li><code>2048<\/code> for megabytes (default).<\/li>\n<li><code>2G<\/code> for gigabytes.<\/li>\n<\/ul>\n<\/li>\n<li><strong>[cores]<\/strong>\n<ul>\n<li><code>2<\/code> for the number of virtual cores\/threads (integer).<\/li>\n<\/ul>\n<\/li>\n<li><strong>[boot]<\/strong>\n<ul>\n<li><code>order=d<\/code> (or just <code>d<\/code>) to boot from disk first.<\/li>\n<li><code>menu=on<\/code> prompts a menu to select the desired boot order.<\/li>\n<\/ul>\n<\/li>\n<li><code>-drive<\/code>\n<ul>\n<li>The full way to configure disks for the VM, with support for additional options. Compatible with <code>.vhd<\/code> (VirtualPC), <code>.vdi<\/code> (VirtualBox), <code>.vmdk<\/code> (VMware), and more.<\/li>\n<li>E.g. <code>-drive file=vmImage.qcow2<\/code> or <code>-drive file=vmImage.img,if=ide,format=raw,cache=writeback<\/code><\/li>\n<\/ul>\n<\/li>\n<li><code>-hda<\/code>, <code>-hdb<\/code>, <code>-hdc<\/code>, and so on.\n<ul>\n<li>A shorthand alternative to <code>-drive<\/code> for setting hard disk images.<\/li>\n<li>E.g. <code>-hda vmImage.qcow2<\/code><\/li>\n<\/ul>\n<\/li>\n<li><code>-cdrom<\/code>\n<ul>\n<li>Used to run an installation or recovery live CD.<\/li>\n<\/ul>\n<\/li>\n<li><code>-enable-kvm -cpu host<\/code>\n<ul>\n<li>Allows QEMU to use KVM hardware virtualization for better performance, since the CPU will not be emulated when the guest and host share the same architecture.<\/li>\n<\/ul>\n<\/li>\n<li><code>-vga virtio -display [display]<\/code>\n<ul>\n<li><code>virtio<\/code> is a paravirtualized video device.\n<ul>\n<li><code>vnc=127.0.0.1:0<\/code> is strongly recommended to <strong>bind on localhost only<\/strong> and access via an <strong>SSH tunnel<\/strong>. If bound to one or all host interfaces, ensure firewall rules are appropriately restricted.<\/li>\n<li><code>sdl<\/code> uses the Simple DirectMedia Layer library and provides good performance with <strong>3D<\/strong> emulation support.<\/li>\n<li><code>gtk<\/code> uses the GIMP Toolkit library but does not provide optimal performance.<\/li>\n<li><code>none<\/code> disables video output (headless).<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<li><code>-vga qxl<\/code>\n<ul>\n<li>Uses SPICE for remote desktop and requires enabling the module with <code>sudo modprobe qxl bochs_drm<\/code>.<\/li>\n<\/ul>\n<\/li>\n<li><code>-device virtio-gpu-pci<\/code>\n<ul>\n<li>Use this option to pass through a <strong>GPU<\/strong>\/video adapter in a <strong>PCIe<\/strong> slot to the VM.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre>qemu-system-<strong>[arch]<\/strong> -m <strong>[memory]<\/strong> -smp <strong>[cores]<\/strong> -boot <strong>[boot]<\/strong> -hda <strong>[PATH]<\/strong> -cdrom <strong>[PATH]<\/strong> -enable-kvm -cpu host -vga virtio -display <strong>[display]<\/strong><\/pre>\n<pre>qemu-system-<strong>x86_64<\/strong> -m <strong>4G<\/strong> -smp <strong>4<\/strong> -boot <strong>menu=on<\/strong> -hda <strong>\/PATH\/vmImage.qcow2<\/strong> -cdrom <strong>\/PATH\/file.iso<\/strong> -enable-kvm -cpu host -vga <strong>virtio<\/strong> -display <strong>vnc=127.0.0.1:0<\/strong><\/pre>\n<pre>qemu-system-<strong>x86_64<\/strong> -m <strong>2048<\/strong> -drive <strong>file=vmImage.qcow2,format=qcow2<\/strong> -netdev <strong>tap,id=net0,ifname=tap0,script=no,downscript=no<\/strong> -device <strong>virtio-net-pci,netdev=net0,mac=52:54:00:12:34:56<\/strong><\/pre>\n<pre>qemu-system-<strong>x86_64<\/strong> -m <strong>2G<\/strong> -hda <strong>vmImage.qcow2<\/strong> -usb -device <strong>usb-host,hostbus=2,hostaddr=4<\/strong><\/pre>\n<p><strong>Note:<\/strong> use <code>lsusb<\/code> to find the bus and address of the <strong>USB<\/strong> device you want to pass through.<\/p>\n<ul>\n<li>Managing Volumes<\/li>\n<\/ul>\n<pre>cd \/var\/lib\/libvirt\/images\/\r\nqemu-img info <strong>vmImage.qcow2<\/strong>\r\nqemu-img resize <strong>vmImage.qcow2<\/strong> +<strong>10<\/strong>G\r\nqemu-img commit <strong>vmImage.qcow2<\/strong>\r\nqemu-img convert -O <strong>vmdk<\/strong> -O <strong>qcow2<\/strong> vmImage<strong>.vmdk<\/strong> vmImage<strong>.qcow2<\/strong>\r\n<\/pre>\n<ul>\n<li>ARM VMs<\/li>\n<\/ul>\n<pre>sudo apt-get install qemu-system-arm -y\r\nqemu-system-arm -M help\r\nqemu-system-arm -M <strong>virt<\/strong> -enable-kvm -m 256 -kernel vmlinuz-3.2.0-4-<strong>virt<\/strong> -initrd initrd.img-3.2.0-4-<strong>virt<\/strong> -hda <strong>ARMvmImage.qcow2<\/strong> -append <strong>\"root=\/dev\/vda1\"<\/strong> -netdev <strong>user,id=usernet,hostfwd=tcp::2222-:22<\/strong> -device <strong>virtio-net-device,netdev=usernet<\/strong><\/pre>\n<p><strong>Note:<\/strong> volumes and networks are managed similarly across all architectures.<\/p>\n<hr \/>\n<p><strong>GUEST AGENT<\/strong><\/p>\n<ul>\n<li>Install on the guest OS<\/li>\n<\/ul>\n<pre>sudo apt update &amp;&amp; sudo apt install qemu-guest-agent -y\r\nsudo systemctl enable qemu-guest-agent --now<\/pre>\n<p><strong>OR<\/strong><\/p>\n<pre>sudo apt update &amp;&amp; sudo apt install spice-vdagent -y<\/pre>\n<hr \/>\n<p><strong>REFLECTIONS<\/strong><\/p>\n<p>Unless there is a reason to emulate a VM, such as running a different architecture, it is recommended to <strong>use KVM in most cases<\/strong>.<\/p>\n<p>The configuration files for each VM are stored at <code>\/etc\/libvirt\/qemu<\/code>.<\/p>\n<hr \/>\n<p><strong>BONUS<\/strong><\/p>\n<p>To install Windows 11 on QEMU\/KVM, it is often useful to send key combinations to the guest:<\/p>\n<pre>virsh send-key <strong>Win11_VM_Name<\/strong> --holdtime 1000 KEY_LEFTSHIFT KEY_F10<\/pre>\n<p>This opens a terminal where you can run the following command to bypass the sign-in requirement and install Windows 11 offline.<\/p>\n<pre>OOBE\\BYPASSNRO<\/pre>\n<p>To create a new user from the terminal, run the following commands.<\/p>\n<pre>net user Administrator \/active:yes\r\nnet user \/add <strong>userName<\/strong> userPassword\r\nnet localgroup administrators <strong>userName<\/strong> \/add\r\n%windir%\\system32\\oobe\\msoobe.exe<\/pre>\n<p>For troubleshooting guest memory dynamically:<\/p>\n<pre>virsh dommemstat <strong>vmName<\/strong>\r\nvirsh setmem <strong>vmName<\/strong> 32768M --live\r\nvirsh dumpxml <strong>vmName\r\n<\/strong><\/pre>\n<p>Disabling (the inverse would enable) nested virtualization.<\/p>\n<p><strong>For Intel:<\/strong><\/p>\n<pre>cat \/sys\/module\/kvm_intel\/parameters\/nested<\/pre>\n<pre>sudo modprobe -r kvm_intel\r\nsudo modprobe kvm_intel nested=0<\/pre>\n<pre>echo \"options kvm_intel nested=0\" | sudo tee \/etc\/modprobe.d\/kvm-nested.conf<\/pre>\n<p><strong>For AMD:<\/strong><\/p>\n<pre>cat \/sys\/module\/kvm_amd\/parameters\/nested<\/pre>\n<pre>sudo modprobe -r kvm_amd\r\nsudo modprobe kvm_amd nested=0<\/pre>\n<pre>echo \"options kvm_amd nested=0\" | sudo tee \/etc\/modprobe.d\/kvm-nested.conf<\/pre>\n<p><strong>For both:<\/strong><\/p>\n<pre>sudo update-initramfs -u\r\nsudo reboot<\/pre>\n<p>Verify<\/p>\n<pre>cat \/sys\/module\/kvm_intel\/parameters\/nested<\/pre>\n<p>Or<\/p>\n<pre>cat \/sys\/module\/kvm_amd\/parameters\/nested<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>KVM (Kernel-based Virtual Machine) and QEMU (Quick EMUlator) are both open-source virtualization technologies that run [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-3526","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/posts\/3526","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dft.wiki\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3526"}],"version-history":[{"count":29,"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/posts\/3526\/revisions"}],"predecessor-version":[{"id":5980,"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/posts\/3526\/revisions\/5980"}],"wp:attachment":[{"href":"https:\/\/dft.wiki\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3526"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dft.wiki\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3526"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dft.wiki\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3526"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}