Using SSH Keys on Windows
Monday, January 28th, 2008Install 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 Linux server and append your new public key to the authorized_keys file in ~/.ssh:
cat id_rsa.pub >> ~/.ssh/authorized_keys
Now, logout from the 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 your Linux server. You shouldn’t have to enter a password.