2

Everytime I login into our CentOS 5, MediaTemple (dv) server, I have to do:

ssh-agent $BASH
ssh-add .ssh/my_id

once I've managed to login. This is used for our server to participate in our git workflow, and as we iterate quite a bit while in development, I'm growing tired of doing the same over and over again.

Adding this to the server's .bash_profile does not quite work, and my very limited understanding of ssh tells me it's because doing ssh_agent $BASH pretty much just spawns a new login window (and thus, it stops executing the next lines of the file...).

How do I get the server to have it's key working every time I remotely login into it?

2 Answers 2

2

The easiest thing to do is to enable agent forwarding in SSH so that it uses the agent on your local system instead of on the server. Failing that, you can do as it suggests in the man page and run eval $(ssh-agent -s).

3
  • Great, so it would use the ssh-agent on my local machine, but present the key stored in the server when git asks for them? I'm not very familiar at all with SSH, so if you could tell me about some resources I could look into, I'd really appreciate it!
    – Roberto
    Jul 15, 2010 at 10:29
  • The key would be stored on the agent in your local machine once it is added. ibm.com/developerworks/linux/library/l-keyc3 Jul 15, 2010 at 10:38
  • Yes, agent forwarding is the answer I was looking for. Thanks a lot!
    – Roberto
    Jul 26, 2010 at 19:21
2

This is copied from a script I made a few months ago for teams I work with and use in my .bashrc. It was compiled from a collection of ideas and tweaked to work on multiple operating systems/environments as we found incompatibilities. It checks for a running agent, and if necessary starts one (saving the data for other shells). It then checks if that agent has keys added, and if not adds them for 10hrs (designed for a workday) with usage confirmation. If desired, confirmation can be removed by removing the -c options to ssh-add.

SSH_ENV="$HOME/.bash_sshagent"
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
}
# 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 -q ssh-agent$ || {
         start_agent;
     }
else
     start_agent;
fi
/usr/bin/ssh-add -l > /dev/null || {
        echo No ssh identities detected. Running "ssh-add -c -t 36000"...
        /usr/bin/ssh-add -c -t 36000;
}
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .