Starting Cygwin ssh-agent at Logon

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.

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:

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

One thought on “Starting Cygwin ssh-agent at Logon

  1. Ireallydon'tknowwhyIamwritingthis

    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”
    PID=$(pgrep ssh-agent)
    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)
    eval $SSH_ENV

    # 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
    echo $SSH_ENV


Leave a Reply

Your email address will not be published. Required fields are marked *