By peter on February 23, 2006 | Permalink
I started using Screen on a daily basis, and quickly desired an ssh-agent tied to a long-lived screen session.
The obvious solution (running "ssh-agent screen") doesn't work—as soon as you log out, screen drops into the background and you lose the agent.
People have solved this problem in various ways—a quick search shows some of them. I didn't like them. They all rely on scripts external to screen for running the agent and maintaining its environment variables.
I spent a little time last night on a solution. It's completely contained in your .screenrc, and produces an ssh-agent tied to screen's life cycle.
It's just two lines:
setenv SSH_AUTH_SOCK $HOME/.screen-ssh-agent screen 10 ssh-agent -a $SSH_AUTH_SOCK $SHELL
This creates a screen (number 10) that holds your ssh agent. If you don't like 10, you can start the agent on any screen. Things still work properly. The agent will be available to all the screens in that session. It also persists if you close the screen it was started on.
Make sure you run the setenv line early enough in your .screenrc that it will take effect before you open new screens.
The only problem I have with this setup is that all my screen sessions share the same agent—the one listening on $HOME/.screen-ssh-agent. I'd like to make that one agent per screen session, using something like $HOME/.screen-ssh-agent.$$. Unfortunately $$ is empty from within .screenrc—I can't find a real environment variable that uniquely identifies the current process.