{"id":3526,"date":"2023-03-28T06:31:45","date_gmt":"2023-03-28T10:31:45","guid":{"rendered":"https:\/\/dft.wiki\/?p=3526"},"modified":"2026-02-09T14:50:02","modified_gmt":"2026-02-09T19:50:02","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 are different in the way they accomplish the same tasks.<\/p>\n<p>While <strong>KVM<\/strong> [<a href=\"https:\/\/libvirt.org\/\">Link<\/a>] is a Linux kernel module that makes it function as a hypervisor with the usage of hardware virtualization features (Intel VT-x and AMD-V), QEMU is an emulator.<\/p>\n<p><strong>QEMU<\/strong> it is capable of emulating different architectures and peripherals at the expense of related overhead. But it can also work with KVM to take advantage of the 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>, the virtualization must be enabled on the BIOS. The service <code>libvirtd<\/code> must be <strong>active (running)<\/strong>. The users that will manage the VMs need to be members of <code>libvirt<\/code> and <code>kvm<\/code> group 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> the path <code>\/var\/lib\/libvirt\/images<\/code> is where virtual disks are stored by default, while <code>\/etc\/libvirt\/qemu\/<\/code> is where each VM configuration is 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 bridge network (<code>bridged-network<\/code>) by creating a bridge interface (<code>br0<\/code>) to a physical interface (<code>ens192<\/code>) on the host that will give 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 might be not started and not set to auto-start. And alternatively, to the command line, KVM has a nice <strong>GUI<\/strong> called <strong>Virtual Machine Manager<\/strong> if you are running on a host with a graphic 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 be shown 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>Attacking 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 bits.<\/li>\n<li><code>i386<\/code> for x86 32 bits.<\/li>\n<li><code>arch64<\/code> for ARM 64 bits.<\/li>\n<li><code>arm<\/code> for ARM 32 bits.<\/li>\n<li><code>ppc<\/code> for PowerPC.<\/li>\n<li>more<\/li>\n<\/ul>\n<\/li>\n<li><strong>[memory]<\/strong>\n<ul>\n<li><code>2048<\/code> for explicit in megabytes (default).<\/li>\n<li><code>2G<\/code> for explicit in 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 first try to boot the disk.<\/li>\n<li><code>menu=on<\/code> will prompt a menu that will allow the selection of the desired boot order.<\/li>\n<\/ul>\n<\/li>\n<li><code>-drive<\/code>\n<ul>\n<li>It is the complete way or setting disks to the VM and optionally accepts additional options. It is 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>\u00a0or <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>Alternatively to the <code>-drive<\/code>, it is a shorthand to set 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>It is 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 the hardware virtualization features of KVM for better performance because it will not emulate the CPU if the Guest has the same architecture of the Host.<\/li>\n<\/ul>\n<\/li>\n<li><code>-vga virtio -display [display]<\/code>\n<ul>\n<li><code>virtio<\/code> is a paravirtualised video device.\n<ul>\n<li><code>vnc=127.0.0.1:0<\/code> it is strongly recommended to <strong>only bind on localhost<\/strong> and use an <strong>SSH tunnel<\/strong> to connect to. If set to bind on one or all interfaces of the Host, make sure the firewall rules as restricted appropriately.<\/li>\n<li><span style=\"font-size: 1rem;\"><code>sdl<\/code> uses Simple DirectMedia Layer library and provides a good performance because it supports <strong>3D<\/strong> emulation.<\/span><\/li>\n<li><code>gtk<\/code> uses GIMP Toolkit library but does not provide not optimal performance.<\/li>\n<li><code>none<\/code> does not display video output (headless).<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<li><code>-vga qxl<\/code>\n<ul>\n<li>Uses SPICE to remote desktop and requires enabling the module by issuing <code>sudo modprobe qxl bochs_drm<\/code>.<\/li>\n<\/ul>\n<\/li>\n<li><code>-device virtio-gpu-pci<\/code>\n<ul>\n<li>Check this option to &#8220;pass-through&#8221; a <strong>GPU<\/strong>\/Video adapter placed in a <strong>PCI Express<\/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 out the bus and address of the <strong>USB<\/strong> device you want to <strong>pass-through<\/strong>.<\/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\r\n<\/strong>qemu-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 -device virtio-net-device,netdev=usernet<\/strong><\/pre>\n<p><strong>Note:<\/strong> the volumes and networks are managed similarly for 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 -y 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, like a different architecture for example, 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 usually 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 will allow to enter the following command to bypass the sign-in feature and install Windows 11 offline.<\/p>\n<pre>OOBE\\BYPASSNRO<\/pre>\n<p>Or to create a new user from the Terminal, enter 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 dynamically guest memory.<\/p>\n<pre>virsh dommemstat <strong>vmName<\/strong>\r\nvirsh setmem <strong>vmName<\/strong> 32768M --live\r\nvirsh dumpxml <strong>vmName<\/strong><\/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":27,"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/posts\/3526\/revisions"}],"predecessor-version":[{"id":5288,"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/posts\/3526\/revisions\/5288"}],"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}]}}