clone a KVM guest

"Clone" a KVM debian guest notes.

Shutdown or Suspend the host.

Create a clone of the host democritos.
# virt-clone -o democritos -n thales -f /home/vm/thales.qcow2 -d
Clone 'thales' created successfully.
The clone disk is at /home/vm/thales.qcow2

This is good enough if we just need a clone with a different MAC Address and a different UUID. However, if we need a host that can work simultaneously with the original host we (most likely) need a bit more variation.

Log in to the clone or mount it's image to change hostname, IP address(es), etc.

Change Hostname.
# cd /etc
# grep -ril `hostname -f` |tee hostname.file.list
# perl -i.0 -p -e 's/demokritos/thales/g;' `cat hostname.file.list`

Change IP address.
# grep -ril '' /etc |tee ip.file.list
# perl -i.old_ip -p -e 's/;' `cat ip.file.list`

Reboot Clone
# shutdown -r now

Log in to thales ( the cloned system )

Create a new RSA ssh key
# ssh-keygen -f /etc/ssh/ssh_host_rsa_key -N '' -t rsa
Generating public/private rsa key pair.
/etc/ssh/ssh_host_rsa_key already exists.
Overwrite (y/n)? y
Your identification has been saved in /etc/ssh/ssh_host_rsa_key.
Your public key has been saved in /etc/ssh/
The key fingerprint is:
a6:fc:76:OF:F1:33:7C:04:77:07:ce:5a:cf:23:48:3a root@thales
The key's randomart image is:
+--[ RSA 2048]----+
|                 |
|                 |
|             .   |
|            . .  |
|        S  . ----|
|     . o   .=  o.|
|      +   o..o..=|
|       ..E....o++|
|       ....  o=++|

Overwrite the DSA SSH key with a new one.
# ssh-keygen -f /etc/ssh/ssh_host_dsa_key -N '' -t dsa

Overwrite the ECDSA SSH key with a new with the largest (practical) key-size (allowed).
# ssh-keygen -f /etc/ssh/ssh_host_ecdsa_key -N '' -t ecdsa -b 521

In a debian based system you may use dpkg to replace the SSH keys
# dpkg-reconfigure openssh-server

clone a KVM guest