SSH RSA Login Setup Script

picture

SSH RSA Login Setup Script

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:

  1. #!/bin/bash
  2. echo "Server to Connect To"
  3. read SERVER
  4. echo "Server Username"
  5. read UN
  6. echo "Server Port (Defaults to 22)"
  7. read PORT
  8. if [ "$PORT" = "" ]; then
  9.   PORT="22";
  10. fi
  11.  
  12. #OK We've gathered the required info for the server we'll connect to
  13. #Now we'll check to see if there is already a rsa key and if not we'll create one.
  14.  
  15. if [ ! -f ~/.ssh/id_rsa.pub ]; then
  16.      echo "No RSA key found."
  17.      ssh-keygen -t rsa
  18.      fi
  19.  
  20. #We've now either checked that the rsa key is there or created a new one.
  21. #we'll now connect and  create the .ssh directory
  22. echo "Creating the .ssh directory on the remote server"
  23. ssh -p $PORT $UN@$SERVER mkdir -p .ssh
  24.  
  25. #We'll now upload the rsa key and also make some changes to the files on the server to accomodate the various servers
  26. echo "Uploading your rsa keys to the remote server"
  27. 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*;'
  28.  
  29. #You should now beable to login without a password.
  30. echo "You should now be able to sign in without a password."
  31. echo "You will now be signed in to the remote server"
  32.  
  33. 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.

sshrsa.sh

Add Your Comment

The content of this field is kept private and will not be shown publicly.
Image CAPTCHA
Enter the characters shown in the image.

Search Site

Latest Posts

picture
One of Drupal longtime woes has been it’s GUI. It has...
picture
Long time users of Drupal will be familiar with Fields...
picture
I use SSH on an hourly basis and am often logging into...
picture
One of the most common tasks in module development is...

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