Last active
October 12, 2017 03:59
-
-
Save santigarcor/af56fca5813ab6f82eee15e8a9c3cbc4 to your computer and use it in GitHub Desktop.
Add ssh key to server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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