Skip to content

Instantly share code, notes, and snippets.

@santigarcor
Last active October 12, 2017 03:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save santigarcor/af56fca5813ab6f82eee15e8a9c3cbc4 to your computer and use it in GitHub Desktop.
Save santigarcor/af56fca5813ab6f82eee15e8a9c3cbc4 to your computer and use it in GitHub Desktop.
Add ssh key to server
#!/bin/bash
if [[ $(whoami) == "root" ]]; then
echo "Please don't run this script as sudo user"
exit 1
fi
username=$(whoami)
while [[ -z "$sshPublicKey" ]]
do
echo "Please generate a ssh key for the new user in your computer, use this command:"
echo "ssh-keygen -t rsa -b 4096 -C ${newHostname}-key -f ${newHostname}-key"
echo "\n"
echo "Please paste the contents of the public key(${newHostname}-key.pub) here and press enter: (Can't be empty)"
read -r sshPublicKey
done
echo "$sshPublicKey" >> "/home/$username/.ssh/authorized_keys"
echo "Restarting ssh service..."
sudo service ssh restart
clear
echo "Key added successfully"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment