09.15.09

Reconnect ssh-agent in screen

Posted in Intermediate, Linux at 9:58 am by Techie

I like to use ssh-agent to make logins simpler, but had a problem that whenever I detached a screen, I’d lose my agent. This post and others provide information on how to do reconnect, but it didn’t quite work for me. In particular, what these posts suggest is editing your .bashrc or similar to create a symlink to your actual $SSH_AUTH_SOCK, then set your .screenrc to set the environmental variable $SSH_AUTH_SOCK to point to your symlink. This is all fine and good, until you log into screen the first time, and it sources your .bashrc and tries to symlink to your symlink. Here’s how to fix it.

Add the following to the end of .bashrc:


if test $SSH_AUTH_SOCK && [ $SSH_AUTH_SOCK != "/tmp/ssh-agent-$USER-screen" ]
then
rm -f /tmp/ssh-agent-$USER-screen
ln -sf "$SSH_AUTH_SOCK" "/tmp/ssh-agent-$USER-screen"
fi

This creates a symlink to your actual $SSH_AUTH_SOCK, but only if it isn’t set to your symlink.

Add the following to your .screenrc:


setenv SSH_AUTH_SOCK "/tmp/ssh-agent-$USER-screen"

That should do the trick.

Leave a Comment

Bad Behavior has blocked 280 access attempts in the last 7 days.