Useful commands for configuring KVM virtual machines


install KVM

apt install --no-install-recommends qemu-system libvirt-daemon-system virtinst dnsmasq qemu-utils

note: this command is intended for use on Debian 12


list networks

virsh net-list --all

create a temporary network config file

cat <<EOF > /tmp/br0
  <bridge name='br0' stp='on'/>
  <ip address='' netmask=''>
      <range start='' end=''/>
  <forward mode='nat'>
      <port start='1024' end='65535'/>

define a network

virsh net-define /tmp/br0

configure a network autostart

virsh net-autostart br0

activate a network

virsh net-start br0

deactivate a network

virsh net-destroy br0

undefine a network

virsh net-undefine br0

attach a network

virsh attach-interface --domain test0 --type bridge --source tmp0 --model virtio --config

detach a network

virsh detach-interface --domain test0 --type bridge --mac 00:11:22:33:44:55 --config

Storage pool

list storage pools

virsh pool-list --all

create a directory for a storage pool

mkdir /mnt/pool0

create a temporary storage pool config file

cat <<EOF > /tmp/pool0
<pool type='dir'>

define a storage pool

virsh pool-define /tmp/pool0

configure a pool autostart

virsh pool-autostart pool0

activate a storage pool

virsh pool-start pool0

deactivate a storage pool

virsh pool-destroy pool0

undefine a storage pool

virsh pool-undefine pool0

Storage volume

create a temporary volume config file

cat <<EOF > /tmp/vol0
<volume type='file'>
  <capacity unit='G'>20</capacity>
    <format type='qcow2'/>

create a volume

virsh vol-create pool0 /tmp/vol0

delete a volume

virsh vol-delete vol0 pool0

attach a volume

virsh attach-disk --domain test0 --source /mnt/pool0/tmp0 --target vdb --driver qemu --subdriver qcow2 --config

detach a volume

virsh detach-disk --domain test0 --target vdb --config


list VMs

virsh list --all

list available OS names

virt-install --osinfo list

create a VM

virt-install --name test0 --osinfo debian11 --cpu host-passthrough --vcpus 4 --memory 4096 --graphics vnc,listen= --disk /mnt/pool0/vol0,bus=virtio --network bridge=br0,model=virtio --noautoconsole --noreboot --import

start a VM

virsh start test0

suspend a VM

virsh suspend test0

resume a VM

virsh resume test0

stop a VM gracefully

virsh shutdown test0 --mode acpi

stop a VM forcefully

virsh destroy test0

reboot a VM gracefully

virsh reboot test0

reboot a VM forcefully

virsh reset test0


get a listening port

virsh dumpxml test0 | grep vnc | sed -rn "s/.port='([0-9])'.*/\1/p"

connect to a VM using any VNC client


enter a VM config editing mode

virsh edit test0

add the following lines

  <disk type='file' device='cdrom'>
    <source file='/home/ISO/debian-12.2.0-amd64-netinst.iso'/>
    <target dev='hda' bus='sata'/>

edit the following lines

  <boot dev='cdrom'/>

restart a VM


stop a VM

clone a VM

virt-clone --original test0 --name test99 --file /mnt/pool0/vol99

note: if multiple disks are connected to the VM, then "--file" options should be specified in the same order in which these disks are specified in the VM XML description


list snapshots

virsh snapshot-list test0

create a snapshot config file

cat <<EOF > /tmp/snapshot0

create a snapshot

virsh snapshot-create --atomic --domain test0 --xmlfile /tmp/snapshot0

apply a snapshot

virsh snapshot-revert --domain test0 --snapshotname snapshot0

delete a snapshot

virsh snapshot-delete --domain test0 --snapshotname snapshot0


vcpu usage

virsh vcpuinfo --domain test0

memory usage

virsh dommemstat --domain test0

network interfaces

virsh domifaddr --domain test0


Network XML format -
Storage pool and volume XML format -
Domain XML format -
Snapshot XML format -
Manual Snapshots -

Leave a Reply