This question already has an answer here:

I have a scenario where, whenever a user logs into a CENTOS machine via SSH, I need to have the following commands run:

ssh-agent bash ssh-add

[This other thread explains why I am using those commands:

ssh-add complains: Could not open a connection to your authentication agent ]

I have tried putting those into .bashrc, but then when I log in via Putty, it seems like the log in hangs.

I have also tried putting the two commands in a shell script, and then running the shell script manually after logging in, but that is not working (it looks like only the "ssh-agent bash" is run).

So is it possible to get those 2 commands to be run when the user logs in? And if so, how can I do that?

Thanks, Jim

marked as duplicate by user88036, roaima, Thomas, RalfFriedl, Romeo Ninov Sep 22 '18 at 16:37

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Is your keyfile protected by a passphrase? – Panki Sep 20 '18 at 13:55
  • ssh-agent and ssh-add are run locally not on the remote machine. What OS is running on the local machines? ... If it's Windows and you are using putty, maybe this Q/A will be helpful.... – RubberStamp Sep 20 '18 at 13:59

If you put

ssh-agent bash ssh-add

in your .bashrc, you'll get an infinite recursion: the shell executing .bashrc starts ssh-agent, which starts another copy of bash... which will again execute .bashrc, and the process will repeat.

You'll want something like this instead:

if [[ "$SSH_AUTH_SOCK" = "" ]]; then
    # on the first round, we do this...
    exec ssh-agent bash
    # ... and when ssh-agent is running, we do this instead.

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