Ssh-agent with passwords without spawning too many processes

I am using ssh-agent with password protected keys on Linux. Every time I enter a certain car, I do this:

eval `ssh-agent` && ssh-add 

This works pretty well, but every time I log in and do this, I create another ssh-agent. From time to time I will do killall ssh-agent to reap them. Is there an easy way to reuse the same ssh-agent process in different sessions?

+6
linux ssh
source share
4 answers

take a look at the keychain. It was written by people in a similar situation with themselves. Keychain

+5
source share

How much control do you have on this machine? One answer would be to run ssh-agent as a daemon process. Other options are explained on this web page , basically checking to see if there is an agent and then running it if it is 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 
+3
source share

You can do:

 ssh-agent $SHELL 

This will cause ssh-agent to exit when you exit the shell. They still will not be shared between sessions, but at least they will leave when you do this.

+1
source share

Depending on the shell you are using, you can set different profiles for login shells and simple regular new shells. In general, you want to run ssh-agent for login systems, but not for each subshell. In bash, these files will be .bashrc and .bash_login , for example.

Most desktop linuxs these days run ssh-agent for you. You simply add your key using ssh-add, and then forward the keys to remote ssh sessions by running

  ssh -A
0
source share

All Articles