Save SSH password for use in "Terminal" (OSX or *Nix)

Any­one who runs hosted remote servers and has to log into remote ter­mi­nals for reg­u­lar use, it is vital to have short­cuts that allow for quick login. SSH2 is the rec­om­mended way.

On Win­dows, there is the fan­tas­tic SSH2 tool Secure­CRT. Or if you're cash crunched, a com­bi­na­tion of Putty and Putty Con­nec­tion Man­ager works for many.

On Mac OSX and Unix/Linux sys­tems, one doesn't truly need an SSH client at all, because the "Ter­mi­nal" appli­ca­tion is inbuilt. Peo­ple talk of iTerm and such, but I have still to see a value add for such tools.

But one does miss the con­ve­nience of Secure­CRT on OSX, because I have still to find a true Secure­CRT alter­na­tive for the Mac plat­form. Some­thing that allows me to make pre-determined con­nec­tions so I can just click on them to con­nect (which tools like Jel­ly­fiSSH do) and then logs me in directly with­out prompt­ing for a pass­word (which Jel­ly­fiSHH does not do).

So I have sim­ply made aliases in my [code].profile[/code] file, which gets exe­cuted every­time you start your Ter­mi­nal win­dow (so it's a good place to put your short­cuts and any code you wish to exe­cute when the ter­mi­nal starts, such as paths).

  1. Start the Terminal.
  2. Open the pro­file file for the cur­rent user (you).
  3. pico .profile
  4. Enter a new line for our shortcut.
  5. alias s='ssh -2 -p 22 user@host.com'

Quick expla­na­tion for that com­mand in step 3. The let­ter "s" is the short­cut I make for con­nect­ing to the sniptools.com server. Change it to what you wish. This will mean that when I start Ter­mi­nal, all I need to do is type "s" and it con­nects me via SSH to the sniptools.com server. The "-p" switch is an impor­tant one because some of us with para­noid secu­rity set­tings might have a dif­fer­ent port num­ber than the default port 22 for secure SSH. The rest user/host stuff is self-explanatory. The "-2" is to force SSH2 con­nec­tions instead of older vanilla SSH.

Now. Save the pro­file file and source it to try it out:

source .profile

Sourc­ing is only for this one time, for your cur­rent Ter­mi­nal win­dow, which had already exe­cuted the pro­file file *before* we added this alias. When you start a new Ter­mi­nal ses­sion, these aliases et al will be auto­mat­i­cally set for you.

Done. Now your pro­file has the alias for "s". From now when you type "s" in your Ter­mi­nal, it will con­nect, but it will ask you for a pass­word. To get rid of the nag­ging pass­word, we need to cre­ate pub­lic authen­ti­ca­tion key for the domain. This, in fact is what Secure­CRT does behind the scenes on Win­dows too.

Here are the steps to accom­plish this. Run these one-time com­mands in order from the Ter­mi­nal window.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# generate pub and priv keys, leave the passphrase empty
# (simply press ENTER when asked for it)
ssh-keygen
 
#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
exit
 
#logon to the remote server, without password prompt
ssh -2 -p 22 user@host

That's it. This is a huge time­saver. Now all I need to do to login to the sniptools.com server is type one let­ter, "s" in the Ter­mi­nal, and I'm on! Fol­low these instruc­tions for each host you con­nect to on a reg­u­lar basis and you'll love the con­ve­nience henceforth.

19 comments
  1. […] Credit:Save SSH pass­word for use in “Terminal” (OSX or *Nix) derek-punsalan, domain-industry, explore-recent, favorites, linux, monthly-archives, office, secure­crt, september-2002, september-2003, september-2004, snip­tools, ter­mi­nal, the-unstandard, tools, tuto­ri­als, windows […]

  2. Allen Laudenslager says: Aug 02, 200811:29 pm

    Thanks so much for this. Very, very handy. They should include this sim­ple but very use­ful func­tion­al­ity as a 'Pref­er­ence' set­ting right into Ter­mi­nal. Btw, Secure­CRT rocks on Windows!

  3. J says: Sep 13, 20089:15 am

    This is a great arti­cle. Can you add to it how to cre­ate a sec­ond key on the local machine. Like should I name it id_rsa2? When I enter ssh-keygen:
    Gen­er­at­ing public/private rsa key pair.
    Enter file in which to save the key (/Users/…/.ssh/id_rsa):
    /Users/…/.ssh/id_rsa already exists.
    Over­write (y/n)? y
    Enter passphrase (empty for no passphrase):
    Enter same passphrase again:
    Your iden­ti­fi­ca­tion has been saved in /Users/…/.ssh/id_rsa.
    Your pub­lic key has been saved in /Users/…/.ssh/id_rsa.pub.
    The key fin­ger­print is:
    f3:69:d1…:7e:39:83 j…@…-macbook-pro.local

    I know I shouldn't over­write the old one cause that breaks my old con­nec­tion. So what do I need to name the next id_rsa or does it even mat­ter, as long as it's in the .ssh directory?

    Thanks,
    J

  4. Shanx says: Mar 29, 20099:34 pm

    Hi "J".

    The file name doesn't mat­ter. On the server, we're using the ">>" direc­tive to add to the .ssh known pass­words, so it should work.

    On your own machine, the name can be any­thing, as long as you know that that's the file you're copy­ing to the server.

  5. Tony says: Jul 27, 20092:42 pm

    ha amaz­ing! thank you!

  6. Tony says: Jul 27, 20093:42 pm

    ha amaz­ing! thank you!

  7. Russ Smith says: Sep 03, 200912:19 pm

    Wow, great! Sim­ple, easy to fol­low, and right to the point. Per­fect, thank you!

  8. Hank says: Oct 27, 20092:27 pm

    The pub­lic key you use on your local machine is sup­posed to be uploaded to every remote server you wish to con­nect to, you do not need to gen­er­ate more than one pub­lic key on your local machine. You can keep using the same one and upload them to mul­ti­ple servers.

  9. Jeff Geerling says: Mar 31, 20103:58 am

    WOW! This is exactly what I was look­ing for… I knew I could add a key pair to stop hav­ing to enter my pass­word, but the alias in the Ter­mi­nal is immensely help­ful! No more typ­ing in IP addresses when I tun­nel into my server!

    Thanks for this article!

  10. Ben says: May 05, 20109:49 am

    Great writeup. I noticed that I also have to issue the fol­low­ing com­mand for things to work:
    chmod 700 ~/.ssh; chmod 600 ~/ssh/authorized_keys

  11. David says: Aug 05, 20105:56 pm

    Thank you!
    As web devel­oper, this becomes very handy if you deploy your projects via rsync (over ssh). Once done, it saves a lot of time. Worked great.

  12. puneriashu says: Sep 25, 20107:00 am

    great tip. very handy .. you just increased my productivity.

  13. Slobo says: Nov 12, 20105:48 am

    Thank you!!!! That's what I was look­ing for :)

  14. Kelly says: Mar 15, 20119:21 am

    ssh-copy-id –i user@server

    that is a lot quicker for copy­ing the key to the server.

  15. Kyle Bandy says: Mar 27, 20111:38 am

    Absolutely awe­some guide! Really a great time­saver here. Thanks a billion!

  16. mike says: Apr 03, 201110:13 am

    still ask­ing for the pass­word for me :( I fol­lowed every­thing. Could the secu­rity on my server not allow login via autho­rized keys?

  17. JP says: Apr 22, 20114:00 am

    Thanks Shanx.
    Every­thing works with­out a hitch except for one thing.

    The aliases are not being remem­bered. Every time I close the ter­mi­nal win­dow it for­gets them, and I get this error:

    –bash: s: com­mand not found

    How­ever, when I type "source .pro­file" again it remem­bers. That means for each new win­dow I open have to source the pro­file again. Can you please tell me what I'm doing wrong?

  18. JP says: Apr 22, 20114:24 am

    I fig­ured out a workaround. In the Ter­mi­nal pref­er­ences, I edited the Shell set­tings so that on Startup it runs the com­mand "source .profile".

  19. Vince says: Aug 12, 20112:23 am

    You rock good sir! Well writ­ten, easy-to-follow tuto­r­ial, con­sider this book­marked for future reference!

Submit comment