Common problem: you want to use ssh-agent with Cygwin, but because Windows is special, you can’t inherit the environment in the way you naturally would on a Unix or Linux system.
Now, before I proceed, understand that this is a HorribleHack™; it is the equivalent of modifying your .bashrc every time you login.
For this to work, you need to have the setx utility installed, which you need to download if you’re still using XP.
The first step is to save the following script; I put it in ~/bin/, but you could safely put it into /usr/bin as it only modifes the environment of the person who runs it. I used Python 3 because I could and because everyone should.
# Start an SSH Agent process and export the values to the System, so all
# Cygwin shells can inherit it.
nohup /usr/bin/ssh-agent -c | /usr/bin/python3 -c "
ssh_agent_pid = None
input = sys.stdin.read().split()
# Validate the input
input != 'setenv'
input != 'setenv'
input != 'SSH_AUTH_SOCK'
input != 'SSH_AGENT_PID'
raise Exception('bad input')
# Need to chomp the semi-colon at the end of each line.
ssh_agent_socket = input[:-1]
ssh_agent_pid = input[:-1]
# Set the variables.
subprocess.check_call(['setx', 'SSH_AUTH_SOCK', ssh_agent_socket])
subprocess.check_call(['setx', 'SSH_AGENT_PID', ssh_agent_pid])
except Exception as ex:
if ssh_agent_pid is not None:
if __name__ == '__main__':
Next, create a shortcut in your Startup Folder. I called it simply “SSH Agent”.
The command line you will need (the Target field in the shortcut), is:
C:\cygwin\bin\run.exe -p /bin bash ~/bin/start-ssh-agent
There seems to be an undocumented feature of run, that requires one to set the -p option to something or it will fail. Obviously, if you’ve installed the 64-bit version of Cygwin or put the start-ssh-agent script somewhere else, you’ll need to change your paths accordingly.
I haven’t written the symmetrical stop-ssh-agent, because I’m lazy and Windows doesn’t provide a convenient location to run scripts at log-off (you mess with group policies if you want, but I have more interesting things to be doing…)