Monday, August 13, 2012

How to use SSH-Agent in msysgit?

UPDATE 08/14/12: changed .bashrc file

I assume that You've generated and added SSH key as was described here: Generating SSH Keys.
It means you've got something like this in Yours user folder:

[C:\Users\<user_name>\.ssh]
$ dir /b
id_rsa
id_rsa.pub
known_hosts
But, when You communicate with GitHub remote (via calling push/pull/fetch) You are getting the next message every time:
Enter passphrase for key '/c/Users/<user_name>/.ssh/id_rsa':
There is a simple solution:
  1. Add a file called .bashrc to my home folder (C:\Users\<user_name>\)
  2. Add the next content to this file:
  3. #! /bin/bash
    eval `ssh-agent -s`
    ssh-add
    
  4. Approach used in case #2 it dangerous because it create ssh-agent instance every time you run msysgit shell. I've tested the next (similar) solution from GitHub again, very carefully and  it DOES work. So, you have to create .bashrc (or .profile) file with the next content:
  5. #! /bin/bash
    SSH_ENV="$HOME/.ssh/environment"
    
    # start the ssh-agent
    function start_agent {
        echo "Initializing new SSH agent..."
        # spawn ssh-agent
        ssh-agent | sed 's/^echo/#echo/' > "$SSH_ENV"
        echo succeeded
        chmod 600 "$SSH_ENV"
        . "$SSH_ENV" > /dev/null
        ssh-add
    }
    
    # test for identities
    function test_identities {
        # test whether standard identities have been added to the agent already
        ssh-add -l | grep "The agent has no identities" > /dev/null
        if [ $? -eq 0 ]; then
            ssh-add
            # $SSH_AUTH_SOCK broken so we start a new proper agent
            if [ $? -eq 2 ];then
                start_agent
            fi
        fi
    }
    
    # check for running ssh-agent with proper $SSH_AGENT_PID
    if [ -n "$SSH_AGENT_PID" ]; then
        ps -ef | grep "$SSH_AGENT_PID" | grep ssh-agent > /dev/null
        if [ $? -eq 0 ]; then
      test_identities
        fi
    # if $SSH_AGENT_PID is not properly set, we might be able to load one from
    # $SSH_ENV
    else
        if [ -f "$SSH_ENV" ]; then
      . "$SSH_ENV" > /dev/null
        fi
        ps -ef | grep "$SSH_AGENT_PID" | grep ssh-agent > /dev/null
        if [ $? -eq 0 ]; then
            test_identities
        else
            start_agent
        fi
    fi
    

From now on You have to enter passphrase only once when you start msysgit shell:
Welcome to Git (version 1.7.11-preview20120710)


Run 'git help git' to display the help index.
Run 'git help <command>' to display help for specific commands.
Initializing new SSH agent...
succeeded
Enter passphrase for /c/Users/<user_name>/.ssh/id_rsa:
Identity added: /c/Users/<user_name>/.ssh/id_rsa (/c/Users/<user_name>/.ssh/id_rsa)

Please note: I've tested the next (similar) solution from GitHub and it doesn't work. It works.

3 comments:

  1. Does this work if you have multiple files. In my bashrc file I currently have:

    #! /bin/bash
    eval `ssh-agent -s`
    ssh-add ~/.ssh/id_rsa
    ssh-add ~/.ssh/gitlab_rsa

    As you say this creates a new ssh-agent every time a terminal opens. Just wanted to check the above will work!

    Thanks

    ReplyDelete
  2. Hi, I've never tried this before. But, you can check it by yourself, here is nice short article https://gist.github.com/jexchan/2351996

    ReplyDelete
  3. Also have a look here https://confluence.atlassian.com/pages/viewpage.action?pageId=271943168#ConfiguringMultipleSSHIdentitiesforGitBash,MacOSX,&Linux-Ensurethessh-agentisrunningandloadedwithyourkeys

    ReplyDelete