Jotting #16: SSH for Windows+Cygwin

2009-02-02

I have struggled with this a bit but I think it is a common scenario. So for my (future and maybe your) benefit here is the current way I have been setting up SSH under Windows (XP) with cygwin.

Windows+Cygwin Preliminaries

  1. Place the following windows script in the start-up folder, e.g., C:\Documents and Settings\All Users\Start Menu\Programs\Startup:

    @echo off
    C:
    chdir c:\cygwin\bin
    set HOME=/home/%USERNAME%
    bash --login c:\cygwin\sshagentrc

  2. Edit cygwin.bat file so that it looks like this:
    @echo off
    REM Script to start ssh-agent each time user logs into machine
    C:
    chdir C:\cygwin\bin
    
    set HOME=/home/%USERNAME%
    bash --login -i
  3. Place the following code in a file .sshagentrc in the c:\cygwin directory
    #!/bin/bash
    # Creates an ssh-agent,
    # writes ssh agent info to the file '~/.ssh-agent-info-`hostname`'
    # and then prompts user for keys.
    # Then any shell can use the agent by sourcing the contents of ~/.ssh-agent-info-`hostname`:
    #  . ~/ssh-agent-info-`hostname`
    HOME=/home/$USERNAME
    SSH_INFO_FILE=$HOME/.ssh-agent-info-`hostname`
    
    if test -e $HOME/.ssh/identity; then
       /usr/bin/echo "ssh_info:   $SSH_INFO_FILE"
       /usr/bin/ssh-agent > $SSH_INFO_FILE
       /usr/bin/chmod 600 $SSH_INFO_FILE
       . $SSH_INFO_FILE
       /usr/bin/ssh-add $HOME/.ssh/identity
    else
       /usr/bin/echo ""
       /usr/bin/echo "ERROR: No private key defined in $HOME/.ssh"
    fi

Individual User Setups

  1. Modify your .bashrc file to include the following lines:
    # Hook into SSH agent session
    SSH_INFO_FILE=~/.ssh-agent-info-`hostname`
    if test -e $SSH_INFO_FILE; then
       . $SSH_INFO_FILE > /dev/null
    fi
    export SVN_SSH=ssh

The SVN_SSH is useful when using subversion with ssh (svn+ssh protocol); you can also create a  Windows environment variable.

  1. Goto HOME directory
  2. Run
    ssh-keygen [-t rsa]
  3. Enter the required passphrase (and remember it!)
  4. This should create a file named identity (or similar) in folder ~/.ssh.
  5. Email the public key, in files *.pub, to your administrator so that he can add the public key to the required servers.

PuTTY

If you want to run PuTTY (Pageant) as well because you like apps like TortoiseSVN, then

  1. Import the OpenSSH key into PuTTYgen and create a PuTTY-compatible key (I store it with the OpenSSH keys in my cygwin home directory: c:\cygwin\home\<userid>\.ssh\identity.ppk)
  2. Place the pageant.exe shortcut into your startup directory
  3. Modify the short-cut property to “c:\PUTTY_HOME\pageant.exe” “HOME\.ssh\identity.ppk”

This will start up pageant when you log in, asks for your SSH key passphrase and you’re set for the day.

Results

The setup initialises ssh-agent when you log into windows and adds your SSH key to the agent’s session. When starting a cygwin terminal, the .bashrc script ensures that the terminal shares the agent’s session.

Overall, you only have to type in your SSH key’s passphrase once and the rest is easy sailing 😉


Jotting #15: Eclipse Tips and Moans

2009-02-02

Some Eclipse tips and moans that I’ve experienced on and off.

I like to start Eclipse with the option -showlocation; it helps to identify which workspace I am working on, especially when I need to work on two version (trunk and branch).

Workspaces

Just upgraded to Eclipse 3.4 (Ganymede) and had to get rid of a wrongly created workspace. While it is easy to move between workspaces many have commented that is difficult to get rid of workspaces.

In Eclipse 3.4, go to file <Eclipse_HOME>/configuration/.settings/org.eclipse.ui.ide.prefs and remove any workspace in the key-value RECENT_WORKSPACES. Voila. Done. Thanks.

Working Sets

I sometimes like to group projects into working sets, especially in workspaces with many Eclipse projects; but in Eclipse these sets are somewhat second class objects:

  • many menu options don’t work on sets
  • can’t export/import sets

I would like to export sets because when I branch my code it would be nice to carry things over to the branch.

Bookmarks

Similarly, you cannot export/import bookmarks. What a shame/hazzle/waste-of-time. My current workaround is to take a snapshot with Faststone Capture (version 4.8, great little helper app!) and keep the picture.