Last active
February 24, 2017 15:13
-
-
Save mikaelleven/e591cf09d825505d1ba46bfe8d07072c to your computer and use it in GitHub Desktop.
Script to automagically enable SSH login through RSA key for the current user on a remote machine
This file contains hidden or 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 | |
############################################################################### | |
# ALLOW-SSH SCRIPT # | |
# Author: Mikael Levén # | |
############################################################################### | |
# What this script basically does: # | |
# 1. Reads your SSH key # | |
# 2. Logins in to the remote server # | |
# 3. Creates a new user using your current username # | |
# 4. Creates the /home/{user}/.ssh/ folder # | |
# 5. Adds your RSA key to the 'authorized_keys' # | |
# 6. Sets required permissions # | |
############################################################################### | |
USER=$(id -u -n) | |
if [ "$1" = "" ]; then | |
echo "" | |
echo "USAGE: AllowSSH [{username}@]{host}" | |
echo "" | |
echo "This script will SSH into the server and add your SSH RSA key to the 'authorized keys' and thus enable passwordless SSH login automatically." | |
echo "" | |
echo "The username ('{username}@') is optional and if omitted the current username '$USER' will be used." | |
echo "" | |
echo "Some examples:" | |
echo "AllowSSH [email protected]" | |
echo "AllowSSH root@myhost" | |
echo "AllowSSH 192.168.0.1" | |
echo "" | |
else | |
SERVER=$1 | |
KEY=$(cat ~/.ssh/id_rsa.pub) | |
ssh $SERVER "adduser -q --disabled-password $USER; mkdir -p /home/$USER/.ssh/; touch /home/$USER/.ssh/authorized_keys; chmod 700 /home/$USER/.ssh; chmod 640 /home/$USER/.ssh/authorized_keys; echo $KEY >> /home/$USER/.ssh/authorized_keys; chown $USER:$USER /home/$USER/.ssh; chown $USER:$USER /home/$USER/.ssh/authorized_keys;" | |
echo "Key for local user '$USER' has been added to user '$USER' at the remote host '$SERVER'" | |
fi |
This file contains hidden or 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 | |
wget https://gist.githubusercontent.com/mikaelleven/e591cf09d825505d1ba46bfe8d07072c/raw/b3ee7a646410a3f9e28ee6d84bc98baeaf70c2cf/AllowSSH.sh | |
chmod +x AllowSSH.sh |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment