Some suggest setting up so-called "passwordless" authentication by generating ssh keys and specifying empty passphrase for the private key. This will result in passwordless authentication, but will also decreased security. Should anyone get hold of your private key, (s)he'll get access to all of your remote systems.
ssh-agentcan help a lot in keeping the security level high and minimizing the number of times you need to type in the password. However, if you use a terminal with tabs or use both local and remote terminals on your workstation, you'll end up running many ssh-agent processes and having to authenticate every time you start such a process, which diminishes most of the conveniences of using
Frustrated with this situation and with a bit of help from Martin, I created a shell script, which I added to my
.bash_profilestartup script. All I have to do now is to authenticate when my first terminal session starts and I'm good until the next time I restart my OS. sweeeet...
Here is how you could set it up on a workstation and a remote-server:
First, if you haven't generated your private/public ssh key pair, do that now:
workstation $ ssh-keygen Generating public/private rsa key pair. Enter file in which to save the key (/Users/user/.ssh/id_rsa): ********************** Enter same passphrase again: ********************** Your identification has been saved in id_rsa. Your public key has been saved in id_rsa.pub. The key fingerprint is: 01:a6:95:23:1c:74:53:c7:f4:87:07:a2:50:ef:99:16 user@Computer.localNow make sure that the file system permissions are set up correctly:
workstation $ cd ~/.ssh/ workstation $ ls -l total 56 -rw------- 1 user staff 1743 Aug 31 00:13 id_rsa -rw-r--r-- 1 user staff 398 Aug 31 00:13 id_rsa.pubThe
id_rsafile must be readable only by owner, if this is not true, the key will be ignored.
On the remote server you need to authorize your newly generated key pair by appending it's public key to the
~/.ssh/authorized_keysfile under your remote home directory:
workstation $ cat ~/.ssh/id_rsa.pub |ssh user@remote-server 'sh -c "cat - >>~/.ssh/authorized_keys"'Now you can try to log in:
workstation $ ssh user@remote-server Enter passphrase for /Users/user/.ssh/id_rsa: ********************** Identity added: /Users/user/.ssh/id_rsa (/Users/user/.ssh/id_rsa) Last login: Thu Sep 11 20:19:19 2008 remote-server $
If you open a new tab in your terminal and try to log in again, you'll be asked to enter the passphrase yet again. This is where my script becomes useful. First download the script from Mediacast: ssh-agent-init.sh and store it somewhere in your home directory
workstation $ mkdir ~/bin workstation $ cd ~/bin workstation $ wget http://mediacast.sun.com/users/IgorMinar/media/ssh-agent-init.sh workstation $ chmod o+x ~/bin/ssh-agent-init.sh
The next (last) step is optional if you want to start the script manually you can skip it.
I wanted to have this script automatically invoked when I start my terminal for the first time in the interactive mode. All I needed to modify were my
.bash_profile(used for interactive sessions) and
.bashrc(used for non-interactive sessions) startup scripts for my bash shell (modifications are in italics):
workstation $ cat ~/.bash_profile ... ... . ~/bin/ssh-agent-init.sh workstation $ cat ~/.bashrc export NONINTERACTIVE=1 . ~/.bash_profile(Note: I source the
~/.bash_profilescript from the
~/.bashrcscript to enable code reuse between the two scripts.)
That's it! If you now try to open a terminal tab for the first time, you'll be asked for passphrase. Once that is done, any new tab or any other shell session created under the same account will reuse the same ssh-agent process.
I've been using this script on my MacOS X laptop as well as OpenSolaris workstation for a few weeks now and it's been working like charm.