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

Written by Shanx July 19th, 2008

How to set up time-saving shortcuts for SSH in Mac OSX or Linux, a bit like SecureCRT on Windows.

Continue reading →
Close

Anyone who runs hosted remote servers and has to log into remote terminals for regular use, it is vital to have shortcuts that allow for quick login. SSH2 is the recommended way.

On Windows, there is the fantastic SSH2 tool SecureCRT. Or if you’re cash crunched, a combination of Putty and Putty Connection Manager works for many.

On Mac OSX and Unix/Linux systems, one doesn’t truly need an SSH client at all, because the “Terminal” application is inbuilt. People talk of iTerm and such, but I have still to see a value add for such tools.

But one does miss the convenience of SecureCRT on OSX, because I have still to find a true SecureCRT alternative for the Mac platform. Something that allows me to make pre-determined connections so I can just click on them to connect (which tools like JellyfiSSH do) and then logs me in directly without prompting for a password (which JellyfiSHH does not do).

So I have simply made aliases in my [code].profile[/code] file, which gets executed everytime you start your Terminal window (so it’s a good place to put your shortcuts and any code you wish to execute when the terminal starts, such as paths).

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

Quick explanation for that command in step 3. The letter “s” is the shortcut I make for connecting to the sniptools.com server. Change it to what you wish. This will mean that when I start Terminal, all I need to do is type “s” and it connects me via SSH to the sniptools.com server. The “-p” switch is an important one because some of us with paranoid security settings might have a different port number than the default port 22 for secure SSH. The rest user/host stuff is self-explanatory. The “-2″ is to force SSH2 connections instead of older vanilla SSH.

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

source .profile

Sourcing is only for this one time, for your current Terminal window, which had already executed the profile file *before* we added this alias. When you start a new Terminal session, these aliases et al will be automatically set for you.

Done. Now your profile has the alias for “s”. From now when you type “s” in your Terminal, it will connect, but it will ask you for a password. To get rid of the nagging password, we need to create public authentication key for the domain. This, in fact is what SecureCRT does behind the scenes on Windows too.

Here are the steps to accomplish this. Run these one-time commands in order from the Terminal window.

# 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 timesaver. Now all I need to do to login to the sniptools.com server is type one letter, “s” in the Terminal, and I’m on! Follow these instructions for each host you connect to on a regular basis and you’ll love the convenience henceforth.

11 Comments

  1. [...] Credit:Save SSH password for use in “Terminal” (OSX or *Nix) derek-punsalan, domain-industry, explore-recent, favorites, linux, monthly-archives, office, securecrt, september-2002, september-2003, september-2004, sniptools, terminal, the-unstandard, tools, tutorials, windows [...]

  2. Allen Laudenslager says:

    Thanks so much for this. Very, very handy. They should include this simple but very useful functionality as a ‘Preference’ setting right into Terminal. Btw, SecureCRT rocks on Windows!

  3. J says:

    This is a great article. Can you add to it how to create a second key on the local machine. Like should I name it id_rsa2? When I enter ssh-keygen:
    Generating public/private rsa key pair.
    Enter file in which to save the key (/Users/…/.ssh/id_rsa):
    /Users/…/.ssh/id_rsa already exists.
    Overwrite (y/n)? y
    Enter passphrase (empty for no passphrase):
    Enter same passphrase again:
    Your identification has been saved in /Users/…/.ssh/id_rsa.
    Your public key has been saved in /Users/…/.ssh/id_rsa.pub.
    The key fingerprint is:
    f3:69:d1…:7e:39:83 j…@…-macbook-pro.local

    I know I shouldn’t overwrite the old one cause that breaks my old connection. So what do I need to name the next id_rsa or does it even matter, as long as it’s in the .ssh directory?

    Thanks,
    J

  4. Shanx says:

    Hi “J”.

    The file name doesn’t matter. On the server, we’re using the “>>” directive to add to the .ssh known passwords, so it should work.

    On your own machine, the name can be anything, as long as you know that that’s the file you’re copying to the server.

  5. Tony says:

    ha amazing! thank you!

  6. Tony says:

    ha amazing! thank you!

  7. Russ Smith says:

    Wow, great! Simple, easy to follow, and right to the point. Perfect, thank you!

  8. Hank says:

    The public key you use on your local machine is supposed to be uploaded to every remote server you wish to connect to, you do not need to generate more than one public key on your local machine. You can keep using the same one and upload them to multiple servers.

  9. WOW! This is exactly what I was looking for… I knew I could add a key pair to stop having to enter my password, but the alias in the Terminal is immensely helpful! No more typing in IP addresses when I tunnel into my server!

    Thanks for this article!

  10. Ben says:

    Great writeup. I noticed that I also have to issue the following command for things to work:
    chmod 700 ~/.ssh; chmod 600 ~/ssh/authorized_keys

  11. David says:

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

Leave a Reply

Miscellaneous

I use the Nokia e61i as my mobile. Instead of my telco’s data plan (which offers me a meagre 1GB per month) I simply prefer to use my home wireless [...]

Continue reading →

View all

Web Tools

If you use Firefox (and if not, what are you waiting for?) you are familiar with useful extensions such as Video Downloader, which allow you to save local copies of [...]

Continue reading →

View all

Databases

This regexp worked for me. SELECT * FROM table WHERE NOT column ~ ( ‘^(‘|| $$[\09\0A\0D\x20-\x7E]|$$|| — ASCII $$[\xC2-\xDF][\x80-\xBF]|$$|| — non-overlong 2-byte $$\xE0[\xA0-\xBF][\x80-\xBF]|$$|| — excluding overlongs $$[\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}|$$|| — straight 3-byte [...]

Continue reading →

View all

Windows

So you’ve been visited by the much dreaded CRC — Cyclical Redundancy Check error, most likely encountered while copying files between hard disks. On Mac OSX, this will usually appear [...]

Continue reading →

View all

Mac OSX

A simple app ought to do it. Download iRinger. It’s a Windows app. If you’re on Mac, you’ll want to use it within a virtual machine, like Parallels or VMWare [...]

Continue reading →

View all

System Maintenance

I use the Nokia e61i as my mobile. Instead of my telco’s data plan (which offers me a meagre 1GB per month) I simply prefer to use my home wireless [...]

Continue reading →

View all

Wordpress

Among many new exciting features, WordPress 2.6 released the ability to store each and every revision of your posts, like an elaborate update history. Now this can be a pretty [...]

Continue reading →

View all

Audio/Video

Panic, the makers of some fantastic software such as Transmit or Panic, also have the most light-weight audio converter for the Mac OSX platform. It’s called Audion: get it here. [...]

Continue reading →

View all

iPhone

A simple app ought to do it. Download iRinger. It’s a Windows app. If you’re on Mac, you’ll want to use it within a virtual machine, like Parallels or VMWare [...]

Continue reading →

View all