Keychain on Windows with Cygwin

I might be repeating myself, just deleting this from our knowledgebase here and didn’t want to lose it.

Running Keychain on Windows

Running svn+ssh means we can use the users that already exist on the Linux server, and we’re tunneling our revision control over a secure connection. The downside is it requires you to login every time you connect to the server.

There’s a way to get around that. First off, make sure you have installed the OpenSSH and keychain packages in Cygwin. Then, run ssh-keygen. Don’t make the mistake of not entering a passphrase. You will now have a private and public key pair generated in ~/.ssh. Use scp to copy your public key to the server you want to access, like this:

scp id_rsa.pub username@server:.ssh

Then, ssh to the server and append your new public key to the authorized_keys file in ~/.ssh:

cat id_rsa.pub >> ~/.ssh/authorized_keys

Now, logout of server and try ssh’ing back in. It should ask you for your passphrase. This means ssh on the server side found your public key in the authorized_keys file and wants to make sure it’s you. Let’s make it even easier.

Edit (or create) a new .bash_profile in your home Cygwin directory. It should look something like this:

# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi

# User specific environment and startup programs
eval `keychain –eval id_rsa`

That last line will run keychain, which is a wrapper for ssh-agent, with your private key. ssh-agent caches your ssh keys and will provide them to any clients (ssh, scp, etc.) who are connecting to a host that has your private key in the authorized_keys file.

Now try closing your Cygwin window and opening a new one. If this is the first time you’re running keychain since you logged into Windows, it will ask you for your passphrase. This is the password for the key you created with ssh-keygen. Enter it, then try to ssh to the server. You shouldn’t have to enter a password.

The final step: create a file named ssh.cmd in your Cygwin directory (c:\cygwin, for example). It should contain one line:

d:\cygwin\bin\bash.exe –login -c “/usr/bin/ssh %*”

Right-click on a folder in Windows Explorer, then click on “TortoiseSVN” and “Settings…” Click “Network” and put the path to ssh.cmd in the space for “SSH client.”

Now try an update. You’ll see a console window for a second as it authenticates, then your update should continue without prompting you for a password.

Leave a Reply