Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 2 down vote accepted

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).

share|improve this answer
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! – Rob Jul 15 '10 at 10:29
The key would be stored on the agent in your local machine once it is added. – Ignacio Vazquez-Abrams Jul 15 '10 at 10:38
Yes, agent forwarding is the answer I was looking for. Thanks a lot! – Rob Jul 26 '10 at 19:21
Thanks! Just what I was looking for! – Daniel Elliott Apr 27 '11 at 9:01

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.

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$ || {
/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;
share|improve this answer
I will try it out myself and report back. Thanks! – Rob Jul 15 '10 at 10:31

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.