Saturday 17 January 2009

setting up an ssh-agent to run scp or ssh cronjobs

Here is the procedure that should be followed in linux to create or load an existing ssh-agent running and making it available across all the user shells with out invoking a new agent again.

Step 1) Edit user's .bash_profile file to include the following lines.

# If ssh-agent is not running for this user, then start it and add a key.
# If ssh-agent is running, then instantiate the environment vars saved
# the last time ssh-agent was run. /proc is your friend.
ssh_agent_pid_1=`ps -C ssh-agent -o "pid,user" --no-headers|awk '$2=="'$LOGNAME'" {print $1; exit}'`

if [ -z $ssh_agent_pid_1 ]; then
eval `/usr/bin/ssh-agent|tee $HOME/.ssh/$HOSTNAME.ssh-agent`
# Add here keys you want loaded every time you log in after a reboot. Use your public key file names.


/usr/bin/ssh-add $HOME/.ssh/identity ##(user's private key for ssh-authentication )


else

source $HOME/.ssh/$HOSTNAME.ssh-agent
fi

Step 2) This will create an agent automatically if you don't have one running. If you do have one running it will load it. Also it will load the default key automatically. The default key must be named identity and not be world readable or writable.

Step 3) Kill your agent if there is already one running to test this. To see if there is one running type: ps -aef | grep ssh-agent. Then kill the pid if there is one.

Step 4) Logout/Login or source logger's .bash_profile file. The .bash_profile will be automatically loaded with the default key. You will be prompted for a passphrase if the key has one.

No comments:

Post a Comment

Tweets by @sriramperumalla