I can't figure out why everytime I ssh back into my server I always have to start my ssh agent with the command eval 'ssh-agent -s' followed by ssh-add <my_key.pem> in order to successfully ssh from my server. I have placed this in my .bashrc but obviously I'm missing something and don't want to always have to initialize that each time I log in.

  • 1
    Maybe a ssh-reagent script may help you?
    – jofel
    Jan 8, 2016 at 11:44
  • 1
    thanks alot but this didn't help. perhaps I'm not using it correctly. I added it to my .bashrc but still get Could not open a connection to your authentication agent. after running ssh-add -L after logging out. Jan 8, 2016 at 12:08
  • 1
    You need to call the ssh-reagent() function (the gist only define it). There need also already a ssh-agent running.
    – jofel
    Jan 8, 2016 at 12:10
  • 1
    Thanks again for this. I called it from my bashrc and seems to work now but i noticed a ton of ssh-agent processes running after that. Is there a way to just re-use same process? Jan 8, 2016 at 13:27
  • Are you sure, that the the ssh-agent did not run previously? The ssh-reagent script does not start new ones.
    – jofel
    Jan 8, 2016 at 13:42

3 Answers 3


As @Jakuje said, using Forward Agent and using a key on your local, secure machine is better.

However, if you need to do what you say, use a tool such as keychain (github.com/funtoo/keychain) or ssh-find-agent (github.com/wwalker/ssh-find-agent ( I wrote the latter, and many still use it, but I have since switched to using the former ). Both of these tools look for running gpg or ssh agents and connectyour environment up to them for you.


I believe much more secure and useful is to ForwardAgent from your workstation, than having some keys on some server and fiddle during each login with agent reinitialization.

You can simply add to your ~/.ssh/config for appropriate host (or use -A switch to ssh command):

ForwardAgent yes

Add the local key to your local agent and you don't have to care if you use it locally or remotely.

You also didn't mention if it is password protected key, but I assume no, if you put it into .bashrc. Much easier way would be also to specify the path to key in your ~/.ssh/config on server (if you want to have it on server) like this:

IdentityFile <my_key.pem>

again for specific host or globally.

  • I wonder what I did.... I had this issue when I set up ssh for my github account. Then I set up ssh for my digital ocean droplet and after doing ssh-copy-id, ssh-add, and then connecting to my webserver, the issue went away. I just restarted my computer and miraculously I don't have to start the agent any more. WEIRD Sep 15, 2017 at 20:12
  • Not my choice cause I have hundreds of repos and do not put them to my ssh config ~/.ssh/config
    – Nam G VU
    Nov 8, 2020 at 5:55

if you are encountering this with git pushes,setting git remote to match the settings in your .ssh/config should do the trick..

git remote add alice git@gitolite-as-alice:whatever.git


Host gitolite-as-alice <-- matches git remote above
HostName git.company.com
User git
IdentityFile /home/whoever/.ssh/id_rsa.alice
IdentitiesOnly yes
  • Not my choice cause I have hundreds of repos and do not put them to my ssh config ~/.ssh/config
    – Nam G VU
    Nov 8, 2020 at 5:54

You must log in to answer this question.

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