Skip to content

Instantly share code, notes, and snippets.

@gadelkareem
Forked from enoch85/lussh
Created October 20, 2020 19:46
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 gadelkareem/c69bd844f8bb49d8b9ccac367d80b6b3 to your computer and use it in GitHub Desktop.
Save gadelkareem/c69bd844f8bb49d8b9ccac367d80b6b3 to your computer and use it in GitHub Desktop.
lussh - script to authorize your SSH key on a server
#!/bin/bash
# make sure to run this with /bin/bash, NOT /bin/sh
echo
echo This script will help you setup ssh public key authentication.
host=dummy
port=22
while [ -n "$host" ]; do
echo -n "SSH server: "
read host
if [ -n "$host" ]; then
echo -n "user[$USER]: "
read usr
#If user not provided use current user
if [ -z "$usr" ]; then
usr=$USER
fi
echo -n "port[$port] "
read port
if [ -z "$port" ]; then
port=22
fi
echo "Setting up RSA authentication for ${usr}@${host} (port $port)..."
if [ -f ~/.ssh/id_rsa.pub ]; then
echo "RSA public key OK."
else
ssh-keygen -b 4096 -f ~/.ssh/id_rsa -N ""
fi
echo "Appending your RSA public key to the server's authorized_keys"
scp -P $port ~/.ssh/id_rsa.pub ${usr}@${host}:~/
# Append public key to authorized keys and fix common
# permission problems, eg. a group-writable .ssh dir,
# or authorized_keys being readable by others.
ssh ${usr}@${host} -p $port "if [ ! -d ~/.ssh ]; then
mkdir ~/.ssh
fi
chmod 700 ~/.ssh
cat ~/id_rsa.pub >> ~/.ssh/authorized_keys
chmod 0600 ~/.ssh/authorized_keys
rm ~/id_rsa.pub"
echo
echo "You should see the following message without being prompted for anything now..."
echo
ssh ${usr}@${host} -p $port "echo !!! Congratulations, you are now logged in as ${usr}@${host} !!!"
echo
echo "If you were prompted, public key authentication could not be configured..."
echo
echo "Enter a blank servername when done."
echo
fi
done
echo "End of configuration."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment