Lorin Hochstein
Lorin Hochstein

Reputation: 59192

ssh-agent with passwords without spawning too many processes

I use ssh-agent with password-protected keys on Linux. Every time I log into a certain machine, I do this:

eval `ssh-agent` && ssh-add

This works well enough, but every time I log in and do this, I create another ssh-agent. Once in a while, I will do a killall ssh-agent to reap them. Is there a simple way to reuse the same ssh-agent process across different sessions?

Upvotes: 6

Views: 5623

Answers (4)

jfm3
jfm3

Reputation: 37774

Depending on which shell you use, you can set different profiles for login shells and mere regular new shells. In general you want to start ssh-agent for login shells, but not for every subshell. In bash these files would be .bashrc and .bash_login, for example.

Most desktop linuxes these days run ssh-agent for you. You just add your key with ssh-add, and then forward the keys over to remote ssh sessions by running

ssh -A

Upvotes: 0

Greg Hewgill
Greg Hewgill

Reputation: 992717

You can do:

ssh-agent $SHELL

This will cause ssh-agent to exit when the shell exits. They still won't be shared across sessions, but at least they will go away when you do.

Upvotes: 2

Aaron Arbery
Aaron Arbery

Reputation: 124

have a look at Keychain. It was written b people in a similar situation to yourself. Keychain

Upvotes: 5

Blair Conrad
Blair Conrad

Reputation: 241714

How much control do you have over this machine? One answer would be to run ssh-agent as a daemon process. Other options are explained on this web page, basically testing to see if the agent is around and then running it if it's not.

To reproduce one of the ideas here:

SSH_ENV="$HOME/.ssh/environment"

function start_agent {
     echo "Initialising new SSH agent..."
     /usr/bin/ssh-agent | sed 's/^echo/#echo/' > "${SSH_ENV}"
     echo succeeded
     chmod 600 "${SSH_ENV}"
     . "${SSH_ENV}" > /dev/null
     /usr/bin/ssh-add;
}

# Source SSH settings, if applicable

if [ -f "${SSH_ENV}" ]; then
     . "${SSH_ENV}" > /dev/null
     #ps ${SSH_AGENT_PID} doesn’t work under cywgin
     ps -ef | grep ${SSH_AGENT_PID} | grep ssh-agent$ > /dev/null || {
         start_agent;
     }
else
     start_agent;
fi 

Upvotes: 3

Related Questions