Skip to content

Instantly share code, notes, and snippets.

@Nixtren
Last active August 29, 2015 14:25
Show Gist options
  • Select an option

  • Save Nixtren/d9244c4b8180320f058b to your computer and use it in GitHub Desktop.

Select an option

Save Nixtren/d9244c4b8180320f058b to your computer and use it in GitHub Desktop.
SSH Host Keys Regeneration Script
#!/bin/bash
#Quickly coded by Nixtren, licensed under public domain.
echo "Deleting old SSH Host keys..."
rm -f /etc/ssh/ssh_host_*
echo "Generating SSH Host keys..."
ssh-keygen -A
keyFileCount=$(ls -f /etc/ssh/ | grep ssh_host_ | wc -l)
if [ $keyFileCount == 0 ]
then
echo "ERROR: SSH Host keys have not been regenerated."
echo "Trying an alternative method. Don't enter any passphrase if requested, just press enter."
ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key
ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key
echo "Done generating keys with alternative method."
fi
echo "Restarting SSH service (it's okay if you get one error, if you get two you will have to manually restart the SSH service)..."
service sshd restart
service ssh restart
echo "Done!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment