I’m connecting remotely with ssh to a debian machine and like to install a virtual debian on it using the command line. I chose to give libvirt and it’s toolchain a chance.

Install tools

sudo apt install virtinst

check, if libvirtd is active:

systemctl status libvirtd

list all virtual systems on host:

sudo virsh list --all

The names are the domain names.
Connect to running domain

sudo virsh console domainname

Stop a domain

sudo virsh shutdown domainname

If the domain hangs or doesn’t understand the shutdown signal (Windows), you may want to destroy it:

sudo virsh destroy domainname

Delete a domain and all it’s resources:

sudo virsh undefine domainname --managed-save --snapshots-metadata --checkpoints-metadata --nvram --remove-all-storage --delete-storage-volume-snapshots --tpm

sudo virsh pool-refresh default

Start screen before starting the installation:

screen

Create a debian virtual machine on command line, output will be sent over ssh:

virt-install --connect qemu:///system\
--virt-type kvm\
--name debiankvm\
--ram 4096\
--disk /var/lib/libvirt/images/debiankvm.qcow,format=qcow2,size=100\
--location https://deb.debian.org/debian/dists/bookworm/main/installer-amd64/\
--network bridge=br0\
--os-variant debian12\
--console pty,target_type=serial \
--extra-args "console=ttyS0"

Because we have started the installation in a screen session, we can detach anytime, even log out from our server and the installation will continue anyway.

To leave the virtual machine, you can simply leave the screen session. Screen slang, detach from screen session, hit Ctrl + a then d.

To get to the virtual server again. Screen slang, attach to the session, write:

screen -r

If you have multiple sessions, you will have to choose which one. If there is just one you will be attached immediatly.

If you want to destroy a running session, but you are no longer attached to it, you may use these commands:

screen -ls s
screen -XS <pid> quit

the <pid> is the numer before the dot given by the first command.