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…)
Very useful, thanks. Not being a Python disciple (yet), I turned this into a shell script. If you source it (source script or . script), it even sets the required environment variables in the current shell:
# set up ssh-agent if it isn’t running yet
NAGENTS=$(pgrep ssh-agent|wc -l)
if [ “$NAGENTS” -eq 1 ]
echo “ssh-agent already running”
if [ “$SSH_AGENT_PID” -ne “$PID” ]
echo SSH_AGENT_PID is $SSH_AGENT_PID, but agent PID is “$PID”
echo Please fix this manually
elif [ “$NAGENTS” -gt 1 ]
echo “more than one ssh-agent already running!! Manual fix required”
SSH_ENV=$(ssh-agent -s | grep -v Agent)
# setx sets environment variable for Windows
setx SSH_AUTH_SOCK $SSH_AUTH_SOCK 2>&1 >/dev/null
setx SSH_AGENT_PID $SSH_AGENT_PID 2>&1 >/dev/null