Cygwin – SSH Without A Password On Every Request

back to tech articles
Windows 7 Ultimate 64-bit, Cygwin

I’ve avoided doing this until now coz I sometimes make mistakes and the SSH password is an additional layer of anti-idiot. Anyway…

Whenever you SSH to a new machine (or SCP a file) on Cygwin, you’re probably used to entering your password for the connection each time. Wouldn’t it be cool if you could enter the password once for each session? Sure it would!

We need to edit our .bash_profile file in the user’s root directory and add the following definition:

File: .bash_profile

1
2
3
4
5
6
SSHAGENT=/usr/bin/ssh-agent
SSHAGENTARGS="-s"
if [ -z "$SSH_AUTH_SOCK" -a -x "$SSHAGENT" ]; then
        eval `$SSHAGENT $SSHAGENTARGS`
        trap "kill $SSH_AGENT_PID" 0
fi

Ok, if we save that and reload Cygwin… nothing happens. However, we could now add our key to the session using ssh-add ~/.ssh/id_rsa. But wait, this is supposed to be automated, right?

In order to have ssh-add run automatically for each session, we should add another snippet to our .bash_profile

File: .bash_profile

1
2
3
4
5
6
7
8
9
10
11
SSHAGENT=/usr/bin/ssh-agent
SSHAGENTARGS="-s"
if [ -z "$SSH_AUTH_SOCK" -a -x "$SSHAGENT" ]; then
        eval `$SSHAGENT $SSHAGENTARGS`
        trap "kill $SSH_AGENT_PID" 0
fi

ssh-add -l >/dev/null 2>&1
if [ $? -eq 1 ]; then
        ssh-add
fi

Save that and reload Cygwin. If all is well, you should now be asked to enter your password for your private key because ssh-add runs for the first time. Do that and your subsequent connections for this session will have the password for your key and you won’t be bothered for it each time.

Use with caution.

Leave a Reply

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