O'Reilly logo

Linux Server Hacks by Rob Flickenger

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Hack #69. Running the ssh-Agent in a GUI

Running ssh-agent in windowing environments

This ssh -agent method described in [Hack #68] works fine as long as that initial eval ssh-agent can be run before your window environment starts. One place to do this is at login, in your ~/.bash_login (or ~/.login if running tcsh). But this can be a less than optimal solution if you're using pass phrases on your keys (which you probably should be). Your window system won't start until you enter your key passwords, after logging in. Another side effect to this approach is that if your ssh-agent ever gets killed, you'll have to quit X, start a new agent, and log in again. And in some environments (such as OS X) there is never even an opportunity to run commands before the graphical environment starts.

Rather than wastefully running an agent for every window you have open (or copy-and-pasting the environment settings between them), try this code in your ~/.profile :

if [ -f ~/.agent.env ]; then
. ~/.agent.env > /dev/null

if ! kill -0 $SSH_AGENT_PID > /dev/null 2>&1 then
echo "Stale agent file found. Spawning new agent..."
eval `ssh-agent | tee ~/.agent.env`
ssh-add
fi
else
echo "Starting ssh-agent..."
eval `ssh-agent | tee ~/.agent.env`
ssh-add
fi

This will maintain a ~/.agent.env file for you, with an environment pointing at your currently running ssh-agent. If the agent dies, opening a new terminal window will spawn one automatically (and add your keys for you), which is shared between all subsequent ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required