Sep 17, 2013

SSH - How to configure ssh-agent.

This is the simple script to activate the ssh-agent

If you are using bash, copy the code below and paste at the end of ~/.profile or ~/.bash_profile file. The user profile file defends on the linux distro you use. But as I notice Debian(OpenSuse, Ubunto) use ~/.profile and RHEL(Fedora, CentOS) use ~/.bash_profile. By the way the code below are need to put the end of you user profile.
SSH_ENV=$HOME/.ssh/environment

function start_agent {
     echo "Initialising new SSH agent..."
     /usr/bin/ssh-agent | sed 's/^echo/#echo/' > ${SSH_ENV}
     echo succeeded
     chmod 600 ${SSH_ENV}
     . ${SSH_ENV} > /dev/null
     /usr/bin/ssh-add;
}

# Source SSH settings, if applicable

if [ -f "${SSH_ENV}" ]; then
     . ${SSH_ENV} > /dev/null
     #ps ${SSH_AGENT_PID} doesn’t work under cywgin
     ps -ef | grep ${SSH_AGENT_PID} | grep ssh- agent$ > /dev/null || {
         start_agent;
     }
else
     start_agent;
fi
If you are using tcsh, copy the code below and paste at the end of ~/.login file.
set SSH_AGENT=/usr/bin/ssh-agent
set SSH_AGENT_ARGS="-c"
set TMP_FILE=~/.ssh/ssh-agent-info

#
#  Check for existing ssh-agent process
#
if ( -s $TMP_FILE ) source $TMP_FILE
if ( $?SSH_AGENT_PID ) then
  set this=`ps -elf | grep ${SSH_AGENT_PID} | grep ssh-agent > /dev/null`
  # start ssh-agent if status is nonzero
  if (( $? != 0 ) && ( -x "$SSH_AGENT" )) then
    $SSH_AGENT $SSH_AGENT_ARGS | head -2 > $TMP_FILE
    source $TMP_FILE
    echo "ssh agent started [${SSH_AGENT_PID}]"
    ssh-add
  endif
endif



No comments: