Regenerate Ubuntu Open SSH Server Keys

Regenerate Ubuntu Open SSH Server Keys

Have you ever cloned a Ubuntu virtual machine? How many times have you actually regenerated the servers SSH keys? This is an important step and isn’t automatic. Below I will show you how to regenerate ubuntu open ssh server keys. I will not go through cloning machines here as this can be done on a variety of platforms.




First you need to change the hostname of the server. I use Vim to edit files but you can use an editor of your choice

vim /etc/hostname/


Replace the first line with the new hostname of your server and save
You now need to update your hostfile, so the server nows where to find itself.

vim /etc/hosts/


Replace the sections with your old hostname with the new one. You can now reboot.

The server will now of booted with it’s new hostname. We must now regenerate the keys

Log back in and delete the old keys

/bin/rm -v /etc/ssh/ssh_host_*

You will receive the output below:

 removed '/etc/ssh/ssh_host_dsa_key'
 removed '/etc/ssh/ssh_host_dsa_key.pub'
 removed '/etc/ssh/ssh_host_ecdsa_key'
 removed '/etc/ssh/ssh_host_ecdsa_key.pub'
 removed '/etc/ssh/ssh_host_ed25519_key'
 removed '/etc/ssh/ssh_host_ed25519_key.pub'
 removed '/etc/ssh/ssh_host_rsa_key'
 removed '/etc/ssh/ssh_host_rsa_key.pub'


You can now regenerate the keys.

# dpkg-reconfigure openssh-server


For the server to use the new keys you must restart the ssh service:

# systemctl restart openssh-server




Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.