Which ssh-agent to connect to when setting SSH_AUTH_SOCK inside screen/tmux?

1

I am using a variation of the following line:

export SSH_AUTH_SOCK=$(find /tmp/ssh-* -name agent.\* -uid $(id -u)|head -n 1)

found over at Github. This way I should be able to use my forwarded agent connection on the remote system even from inside screen or tmux. However, what if I establish two separate SSH connections to the remote machine and both of them run a separate instance of the ssh-agent?

The question boils down to:

  1. should I avoid starting another instance of ssh-agent in the first place?
  2. if the answer is no on the preceding point, how do I choose to which existing socket to connect, given there are multiple running at the moment?
  3. bonus question: can there be something like a stale socket? I.e. when I look for the socket using the find command above, can I end up with two alternatives one of which has gone bad/stale? Or are sockets cleaned up automatically when the process owning them dies? Turns out there can be stale sockets. Just had the issue.

0xC0000022L

Posted 2013-01-30T15:51:44.837

Reputation: 5 091

Answers

1

You should avoid starting multiple ssh-agent processes in the first place. You can use a command like pgrep -u <your_username> ssh-agent to detect the PID of your ssh-agent process (or lack thereof).

jjlin

Posted 2013-01-30T15:51:44.837

Reputation: 12 964