3
votes

I'm using cygwin as my terminal on Windows 7. I have found several suggestions to run ssh-agent in cygwin so I don't have to enter my password every time I run a git fetch/pull/push. I added the following to my .bash_profile and restarted my cygwin session:

SSH_ENV="$HOME/.ssh/environment"

function start_agent {
    echo "Initialising new SSH agent..."
    /usr/bin/ssh-agent -s | sed 's/^echo/#echo/' > "${SSH_ENV}"
    echo succeeded
    chmod 600 "${SSH_ENV}"
    . "${SSH_ENV}" > /dev/null
    /usr/bin/ssh-add;
}

# Source SSH settings, if applicable

if [ -f "${SSH_ENV}" ]; then
    . "${SSH_ENV}" > /dev/null
    #ps ${SSH_AGENT_PID} doesn't work under cywgin
    ps -ef | grep ${SSH_AGENT_PID} | grep ssh-agent$ > /dev/null || {
        start_agent;
    }
else
    start_agent;
fi

It looks as if the ssh-agent and ssh-add are run successfully, but I am still prompted for my password.

Initialising new SSH agent...
succeeded
Enter passphrase for /cygdrive/c/Users/<username>/.ssh/id_rsa:
Identity added: /cygdrive/c/Users/<username>/.ssh/id_rsa (/cygdrive/c/Users/<username>/.ssh/id_rsa)

$ ssh-add -l
2048 <fingerprint> /cygdrive/c/Users/<username>/.ssh/id_rsa (RSA)

$ git fetch --all
Fetching origin
Enter passphrase for key '/c/Users/<username>/.ssh/id_rsa':

I am in fact using SSH and not HTTPS for my git connection (redacted private info):

$ git remote -v
origin  ssh://git@XXX/XXX.git (fetch)
origin  ssh://git@XXX/XXX.git (push)

The closest problem I've found for this issue is the following question:

ssh-agent doesn't work / save me from typing passphrase for git

However, I didn't rename my ssh under /git/bin.

Any suggestions on how to diagnose this issue? Thanks!

2
You can see different paths in the logs. How does your ~/.ssh/config looks like?Jakuje
I do not have one.Jadenity
@Jakuje, I noticed the paths are different - the ssh-add one is /cygdrive/c/Users/<username>/.ssh/id_rsa, but the git one is /c/Users/<username>/.ssh/id_rsa. How do I make these match?Jadenity
having the same problem. I suspect, that the problem is related to the different paths. Any luck so far?X.X_Mass_Developer
2021: See Randy Groom's answer for Windows 10 with Git for Windows installed.cb4

2 Answers

2
votes

Here is an easier solution to this than the one above:

  1. Launch Services (can be found by typing Services in the search box on the Taskbar)
  2. Edit the settings of the "OpenSSH Authentication Agent"
  3. Set the Startup type to Automatic and Start the Service
  4. Launch the Edit the System Environment Variables (type Environment in the search box on the Taskbar)
  5. Add GIT_SSH variable with the value set to "C:\Windows\System32\OpenSSH\ssh.exe"

Now when an SSH key is added, you will not need to continue to type the passphrase in a Windows command prompt or in a Cygwin Bash shell.

0
votes

The problem is still remain. Seems it is related to the different paths and hasn't been fixed yet. You may consider to use expect as an alternatif to ssh-agent for login with passphrase interaction.

In Linux it use to be installed like this:

$ sudo apt-get update
$ apt-get install --assume-yes --no-install-recommends apt-utils expect 

In cygwin you may find it under Tcl:

enter image description here

Here is a simple step on how to use it.

Create a file, lets locate it and name ~/.expect_push

#!/usr/bin/expect -f
spawn git push origin master
expect "Username for 'https://github.com':"
send "<username>\n";
expect "Password for 'https://<username>@github.com':"
send "<password>\n";
interact

Change the above <username> and <password> to your login id with following notes:

  • Modify the contents follow precisely to your git login interaction.
  • If your <password> contain the char $ put it as \$ otherwise the authentication will be failed.
  • For example if your password mypa$$word then put <password> as mypa\$\$word.

Create another file ~/.git_push

#!/usr/bin/bash
git remote set-url origin [email protected]:<username>/<repo>.git
git add .
git commit -m "my test commit using expect"
expect ~/.ssh/.expect_push

Make them executable and create symlink in /bin

$ chmod +x ~/.expect_push
$ chmod +x ~/.git_push
$ cd /bin
$ ln -s ~/.git_push push

Then you can push to the remote without need to fill in username and password or passphrase

$ cd /path/to/your/git/folder
$ push