10 Jan 2009

How to save SSH passwords in Terminal (on linux Ubuntu)

This simple tutorial explains you how to save a SSH password using one simple alias.

  1. Open the terminal and edit with your favourite text editor (vim in this case) the file .bashrc:
    vim ~/.bashrc
  2. Add the new alias to the file (obviously change “user” with your SSH username and “host.com” with your SSH address host):
    alias s='ssh -2 -p 22 user@host.com'

    This means that every time you will type “s” in your terminal, that line will be executed. You can change the letter “s” to another one if you prefer.
    The “-2” option is used to force SSH2 connections instead of simple SSH.
    The “-p 22” option is used to connect via SSH to the port 22 (usually the default port).

  3. Run these commands one time only (you can copy and paste each one) in the following order; remember to change “user” and “host” with your data:

    # generate pub and priv keys, leave the passphrase empty
    # (simply press ENTER when asked for it)
    #copy the pub key to the remote computer
    #(change port number if different from the usual 22)
    #change "user" to your user name
    #change "host" to your domain name
    scp -P 22 ~/.ssh/id_rsa.pub user@host:~/
    #log on to the remote computer
    ssh -p 22 user@host
    #create the .ssh directory in the root login directory, if it doesn't already exist
    mkdir .ssh
    #append key to file
    cat id_rsa.pub >> ~/.ssh/authorized_keys
    #delete the public key file, no longer needed
    rm -f id_rsa.pub
    #log off the remote server
    #logon to the remote server, without password prompt
    ssh -2 -p 22 user@host
  4. That’s it!
    Simply type “s” in your terminal and this will instantly launch the ssh connection without asking your password anymore!

Thanks to Alessio and sniptools.
Note: I used the .bashrc file instead of .profile file (as suggested from sniptools) to save the alias however it did not work.

Tags: , , , ,

Subscribe to Comments

5 Responses to “How to save SSH passwords in Terminal (on linux Ubuntu)”

  1. Thanks,
    it worked just fine!



  2. Very good! ^^



  3. Appreciating the hard work you put into your blog and in depth information you provide. It’s awesome to come across a blog every once in a while that isn’t the same outdated rehashed information. Fantastic read! I’ve bookmarked your site and I’m adding your RSS feeds to my Google account.


    Reynaldo Hultz

  4. Thanks Reynaldo!
    Unfortunately I did not update the blog very often lately…




  5. Nice it worked



Leave a Reply


  • Ricerca

  • Language

  • Categorie

  • Delicious

  • Statistiche

    Warning: Illegal string offset 'timestamp' in /home/mhd-01/www.micheledallatorre.it/htdocs/blog/wp-content/plugins/firestats/firestats-wordpress.php on line 1081

    Warning: Illegal string offset 'timestamp' in /home/mhd-01/www.micheledallatorre.it/htdocs/blog/wp-content/plugins/firestats/firestats-wordpress.php on line 1081

    Warning: Illegal string offset 'timestamp' in /home/mhd-01/www.micheledallatorre.it/htdocs/blog/wp-content/plugins/firestats/firestats-wordpress.php on line 1081

    Warning: Illegal string offset 'timestamp' in /home/mhd-01/www.micheledallatorre.it/htdocs/blog/wp-content/plugins/firestats/firestats-wordpress.php on line 1081
    • Pages displayed : 33362
    • Unique visitors : 18465
    • Pages displayed in last 24 hours : 0
    • Unique visitors in last 24 hours : 0