Duplicate Virtual Machines
Introduction
This page is meant for Kernel Virtual Machines (KVM) only.
Cloning an existing VM is problematic because of the points mentioned in this article. You may want to create a VM Template for this purpose.
Create virtual machines by copying a previous one
Virtual machine environments allow to create vm from scratch, of course, but it is also possible and sometimes useful to be able to create one vm as a perfect copy of another one you already have.
There are issues you have to take care of, anyway:
- a duplicated vm will have exactly the same hostname of the original one
- a duplicated vm will have exactly the same mac address(es) of the original one
- a duplicated vm will have exactly the same ssh keys of the original one
- a duplicated vm will have exactly the same SID of the original one (windows machines only)
if both the original vm and the "copy" are running together in the same network, this will cause issues and/or unsafe vm environments.
How to change vm hostname
It should be sufficient to
- On Linux guest OSes
- log into a vm terminal as root
- change the /etc/hostname file content to the new hostname (eg: "#nano /etc/hostname")
- change the /etc/hosts file content accordingly (eg: "#nano /etc/hosts")
- reboot the vm ("#reboot now")
Beware: if necessary also edit /etc/network/interfaces
How to change the mac address(es)
Any network interface card has its (supposedly) unique MAC address, you know. Virtual NICs are pretty much the same as real, but easier to manage ;-). When you duplicate an existing vm, its MAC address also gets duplicated, and this is DANGEROUS.
Supposing your vm has only one virtual NIC, you can
- stop the vm
- from pve web interface, remove the virtual nic (eg: eth0)
- create a new one, this will get a new MAC, and your vm will always see it as eth0.
- restart the vm and check if everything works fine
This is done at the host level, so is guest OS independent.
How to change the SSH keys
The SSH host keys should be unique of each machine, much like MAC address above, but for security reasons. So, after duplicating a virtual machine as a new one, you need to re.generate the vm host ssh keys.
- ON a Linux OS
- access a vm terminal as root
- remove the previous keys: "rm /etc/ssh/ssh_host_*"
- reconfigure openssh: "dpkg-reconfigure openssh-server"
- your ssh keys should have been regenerated (even if you're on a ssh session!)
you should see an output like:
Creating SSH2 RSA key; this may take some time ... Creating SSH2 DSA key; this may take some time ... * Restarting OpenBSD Secure Shell server: sshd. [OK]
source: http://www.cyberciti.biz/faq/howto-regenerate-openssh-host-keys/