Cloning KVM Virtual Machines with virt-clone, virt-sysprep and cloud-init
Clone KVM VMs the right way: full clones with virt-clone, linked clones with qcow2 backing files, and resetting identity with virt-sysprep or cloud-init.
Published November 27, 2026
Building every VM from an ISO is slow. The faster workflow is to build one golden image, then stamp out copies in seconds. The trick is that a copy must not keep the original's identity: same hostname, SSH host keys, machine-id or static IP will cause subtle and confusing problems. This article shows full clones, linked clones, and how to reset identity properly.
The concept
virt-clone copies a shut-off VM: it duplicates the disk files, generates a new UUID and new MAC addresses, and defines a new domain. That handles the hypervisor side, but the guest operating system inside still believes it is the original machine.
Identity inside the guest lives in a handful of places: /etc/machine-id (used by systemd and DHCP client IDs), SSH host keys in /etc/ssh, the hostname, persistent network rules, and sometimes registered subscriptions. virt-sysprep (from libguestfs) wipes these offline, without booting the image. cloud-init solves the same problem from the other direction: the image is generic, and identity is injected on first boot from a seed.
The golden image pattern
Full vs linked clones
| Approach | Speed | Disk use | Independence |
|---|---|---|---|
| Full clone (virt-clone) | Seconds to minutes (copies data) | Full size per VM | Fully independent |
| Linked clone (qcow2 backing) | Instant | Only the changes | Depends on the base image |
| Cloud image + cloud-init | Instant | Only the changes | Base shared, identity injected |
Preparing a template
- Install and update the template VM, add common packages
- Shut it down cleanly
- virt-sysprep -d template to wipe machine-id, SSH keys, logs
- Never boot the template again; clone from it
- Each clone generates new keys and machine-id on first boot
virt-sysprep then virt-clone
Practical examples
Linked clone in one line
Create an overlay that uses the template as its backing file, then import it.
qemu-img create -f qcow2 -F qcow2 -b /var/lib/libvirt/images/template.qcow2 web02.qcow2
virt-install --name web02 --import --disk web02.qcow2 --memory 2048 \
--os-variant ubuntu24.04 --network network=default --noautoconsoleSet hostname and user while sysprepping
virt-sysprep can also customise as it seals.
virt-sysprep -a web03.qcow2 --hostname web03 \
--ssh-inject ubuntu:file:/home/me/.ssh/id_ed25519.pub \
--run-command 'dpkg-reconfigure openssh-server'cloud-init user-data for a clone
Pass with virt-install --cloud-init user-data=web04.yaml.
#cloud-config
hostname: web04
users:
- name: deploy
sudo: ALL=(ALL) NOPASSWD:ALL
ssh_authorized_keys: [ "ssh-ed25519 AAAA... me@laptop" ]
packages: [nginx]
runcmd: [ systemctl enable --now nginx ]Common mistakes
- Cloning a running VM: the copy is crash-consistent at best and corrupt at worst.
- Skipping sysprep so every clone shares SSH host keys, causing "host key changed" warnings and a security weakness.
- Duplicate /etc/machine-id makes DHCP hand the same IP to several clones.
- Booting the template after creating linked clones, which modifies the base and corrupts every overlay.
Key takeaways
- virt-clone handles hypervisor identity (UUID, MAC); virt-sysprep or cloud-init handle guest identity.
- Linked clones via qcow2 backing files are instant and space-efficient.
- Treat templates as read-only once clones depend on them.
- Cloud images plus cloud-init are the most repeatable workflow.
FAQ
Can virt-clone clone to a different pool?
Yes, pass --file /path/in/other/pool.qcow2 for each disk instead of --auto-clone.
Does virt-sysprep work on Windows?
Partially. For Windows use Microsoft sysprep /generalize inside the guest before cloning.
How do I flatten a linked clone?
Run qemu-img convert -O qcow2 clone.qcow2 standalone.qcow2, or virsh blockpull on a running VM to copy base data into the overlay.