AddThis

Wednesday, August 31, 2011

Easy SSH and Cygwin

SSH-ing into a remote machine (that's running an SSH server) is super easy, but can be a major pain having to input the password over and over.  This is especially true on a Windows machine.  You can use putty, but I prefer using Cygwin because I get all my favorite linux utilities :)  Here's how to make all that work:

Setup
  1. Download and install cygwin with the openssh and the keychain package
  2. Run cygwin
  3. Enter in the terminal:
    1. ssh-keygen -t rsa
    2. choose some password, and enter it again to confirm
  4. vi ~/.bashrc and put this at the button of the file:
keychain ~/.ssh/id_rsa.pub
source ~/.keychain/${HOSTNAME}-sh

Usage
To get be able to log in to the remote machine, simply append the contents of your local ~/.ssh/id_rsa.pub to ~/.ssh/authorized_keys on the remote machine (create the file on the remote machine if it does not exist). *If the authorized_keys file doesn't exist, create it and make sure that the permission is at least 700!

Config
If you typically ssh using another user, or want to setup an alias to be able to quickly ssh somewhere, you can configure ssh to do this.  Simply vi ~/.ssh/config (create the file if it doesn't exist) and model your entries after the following:

Host server
  Hostname server.fully.qualified.name
  User admin

Now to be able to ssh into server.fully.qualified.name as the admin user, simply type in:
ssh server
whereas before you had to type in
ssh admin@server.fully.qualified.name

3 comments:

JonKeam said...

Typically you want the .ssh directory permissions to be 700 (drwx------) and the public key (.pub file) to be 644 (-rw-r--r--). Your private key (id_rsa) should be 600 (-rw-------).

JonKeam said...

On the newest version of cygwin that I'm using 1.7.16, I had to instead include:

#notice no .pub after id_rsa
keychain ~/.ssh/id_rsa
source ~/.keychain/${HOSTNAME}-sh

JonKeam said...

I also got ~/.ssh/authorized_keys to work with permissions of 644 as well.