What's the best way to start ssh-agent with Cygwin?

0

I'd like ssh-agent to be started when I start Cygwin, since I use ssh passthrough often.

I tried adding:

eval `ssh-agent.exe`

to my .bashrc, but any time I use Alt-F2 to open a new terminal window, the .bashrc is run and hence new ssh-agents are created which is undesirable and messes with the passthrough.

I then tried moving the eval command to .bash_profile, but this too results in the same problem - I guess every new terminal window is considered a new login shell.

A third option is to use cygrunsrv, but I just found about it and am not clear on how to use it for this specific purpose, and perhaps more importantly, how to pass on the SSH_AUTH_SOCK and SSH_AGENT_PID information from where the service is created to every new shell created.

Any suggestions on the best way to achieve this outcome - where a single ssh-agent is executed and waiting, and its information is immediately available to be used in any newly opened shell - is welcome.

sundar - Reinstate Monica

Posted 2016-09-19T06:15:08.597

Reputation: 1 289

Answers

1

You can put it in your .bashrc, but before starting it again you should check if it is already running. For example, you can do

if [ -z "$SSH_AUTH_SOCK" ]; then
    (umask 077; ssh-agent > "$HOME/.ssh/environment")
    ssh-add
fi

as part of the process.

Also, if the agent is not running, you could first test if that environment file exists and source it like

environ="$HOME/.ssh/environment"
if [ -f "$environ" ]; then
    . "$environ" >/dev/null
fi

if [ -z "$SSH_AUTH_SOCK" ] || ! ps -p "$SSH_AGENT_PID" | grep -q ssh-agent; then
    (umask 077; ssh-agent > "$environ")
    ssh-add
fi

There's a lengthy answer detailing much of this here on the U&L site.

I do something like this, but have the environ file include the current hostname, since my home directory is on a network share and I want to run the agent on different hosts simultaneously.

And to call out explicitly how this answers your question about how to get the SSH_AUTH_SOCK variable passed between processes, you store the output of ssh-agent in a file and source it again in each shell

Eric Renouf

Posted 2016-09-19T06:15:08.597

Reputation: 1 548

Thanks for the answer and for the link. After looking at the various options, I went with the keychain solution, which turns out to be an awesome little program. – sundar - Reinstate Monica – 2016-09-25T10:09:01.733