I think this works as a simplification of @sandip-bhattacharya's answer. Put this in your ~/.bashrc
file, and run the export command in any currently running screen sessions.
if [ -S "$SSH_AUTH_SOCK" ] && [ ! -h "$SSH_AUTH_SOCK" ]; then
ln -sf "$SSH_AUTH_SOCK" ~/.ssh/ssh_auth_sock
fi
export SSH_AUTH_SOCK=~/.ssh/ssh_auth_sock
That reads "if $SSH_AUTH_SOCK
is a socket (-S
) and not a symbolic link (! -h
), create a new symbolic link at the known path. In all cases, redefine SSH_AUTH_SOCK
to point to the known path.
The ! -h
avoids creating a circular reference if you run this multiple times.
Also, if you use byobu
, it does this automatically, without needing to edit any config files.
The only bug I've found in this (byobu
has it too) is if you open a second ssh -A
or ForwardAgent
connection it will overwrite the first socket, and if you close the second connection before the first, you will lose your only good socket.
This assumes that you first login, then start screen. Right? – innaM – 2009-03-24T09:13:45.120
1How can it be any other way? How would you start screen without being logged in? – None – 2009-03-24T14:04:30.927
1You're right. The question was phrased in a stupid way. But you do need to login, start up a shell and from there start screen? I often do something like "ssh -t some.machine screen -R". – innaM – 2009-03-24T15:06:57.657
1Ah ok. Well, I just tried this and it doesn't work (ie ssh-agent is not connected). I guess ssh doesn't set up the appropriate sockets when used in this fashion. Maybe some more argument-foo could clean that up? – None – 2009-03-25T13:46:47.360
SSH does set up the sockets, it just never starts up the shell. But this tip is so useful that I think I might just change my habits. – innaM – 2009-03-26T21:57:01.433
Concerning the
ln -v
csrsync
issue, adding a check for interactivity, e.g.$PS1 != ""
, may help. Your answer will of course only work as long as one doesn't establish multiple connections withssh-agent
instances that are different on purpose... – Tobias Kienzler – 2012-10-22T09:01:09.800