0

I am trying to automate VM installations for ubuntu 22.04 using autoinstall,cloud-init, qcow2 and virt-install. When I am reinstalling the OS on the same qcow2 file, I have a script that is run in early-commands that checks if there already is an OS present, if there is, it mounts that file system and backs up the host ssh keys from under /etc/ssh/ to RAM, and I have another script that is run in late-commands that restores those backed up keys from RAM into the newly installed system, basically copying them from RAM into /target/etc/ssh/ . Now up until this part, everything works, but then cloud-init generates new host ssh keys for the VM and overwrites the host ssh keys that I copied over from the previous installation. This happens when cloud-init runs the first reboot. Is there a way to change this behavior so cloud-init doesn't generate new host keys if they are already present under /etc/ssh/ ?

1 Answer 1

0

Looking through the source, it looks like you just need to reset the ssh_deletekeys configuration setting (which you can find in the documentation).

The key generation code will only generate keys if they don't already exist. In other words, cloud-init isn't overwriting your restored host keys, it is (a) removing them and then (b) generating new keys because they don't exist. If you stop (a) from happening, then (b) won't need to generate any new keys.

1
  • Yeah, I have tried doing that. I put it as ssh_deletekeys: false under the ssh key but that didn't help. It still generates new keys and overwrites. The default behavior is set to true and that one also was causing the same problem so I think either this key doesn't work or it's doing something else.
    – mavenHawk
    Oct 4, 2022 at 1:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .