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.


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")

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

if [ -f "$environ" ]; then
    . "$environ" >/dev/null

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

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

  • 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 - Remember Monica Sep 25 '16 at 10:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.