SSH RSA Login Setup Script
I use SSH a lot and like the ease of not having to use passwords. However I can never remember the exact commands needed to set it up. So recently I put together a shell script to do if for me automatically.
I use SSH on an hourly basis and am often logging into numerous servers. I often times set up RSA login on the servers I access the most but usually when I want to do this I've forgot the exact procedures I needed and I end up Googling it. This works but I'd prefer a quick and easy way to set it up. Recently I wrote a script that automates the process for me and saves me time. Here is the code:
#!/bin/bashecho "Server to Connect To"
read SERVERecho "Server Username"
read UNecho "Server Port (Defaults to 22)"
read PORTif [ "$PORT" = "" ]; then
PORT="22";
fi#OK We've gathered the required info for the server we'll connect to#Now we'll check to see if there is already a rsa key and if not we'll create one.if [ ! -f ~/.ssh/id_rsa.pub ]; then
echo "No RSA key found."
ssh-keygen -t rsa
fi#We've now either checked that the rsa key is there or created a new one.#we'll now connect and create the .ssh directoryecho "Creating the .ssh directory on the remote server"
ssh -p $PORT $UN@$SERVER mkdir -p .ssh
#We'll now upload the rsa key and also make some changes to the files on the server to accomodate the various serversecho "Uploading your rsa keys to the remote server"
cat .ssh/id_rsa.pub | ssh -p $PORT $UN@$SERVER 'cat >> .ssh/authorized_keys; cp .ssh/authorized_keys .ssh/authorized_keys2; chmod 700 .ssh; chmod 640 .ssh/authorized_key*;'
#You should now beable to login without a password.echo "You should now be able to sign in without a password."
echo "You will now be signed in to the remote server"
ssh -p $PORT $UN@$SERVER
What it does is asks the user for the basic server info, checks if an RSA key exists, then does the necessary step to setup the remote server for RSA login. It takes a few moments and saves me time.
/*DISCLAIMER*/
If you decided to use this it might break something. Don't blame me. If it does break something let me know and I'll continue to tweak/improve the script
To use is just download the file below, chmod it to make it executable, then run it and follow the prompt.
Search Site
Latest Posts
Get In Touch
Feel free to contact me about how I can help with your next Web project.
Telephone: 910-808-1717
E-mail: info@adamagregory.com


Add Your Comment