0

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

1 Answers1

3

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
else
    # ... and when ssh-agent is running, we do this instead.
    ssh-add
fi
telcoM
  • 96,466