Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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
add comment

2 Answers

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. ibm.com/developerworks/linux/library/l-keyc3 –  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
add comment

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

Your Answer

 
discard

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.