20

So, I use git and github with MFA, so, to avoid annoying password asking, for more than a year I am using the git@github.com:user/repo.git URL style.

A couple of days ago, I ran a brew update, and now, every time try to sync with github servers, git asks me the key password.

What I did so far:

  • Checked my configs, seems ok to me, but here it is (the relevant part):

    [user]
       name = Carlos Alexandro Becker
       email = caarlos0@gmail.com
       helper = osxkeychain
    
  • Tried to update git and osxkeychain, current versions are:

    git 2.4.1
    OpenSSH_6.2p2, OSSLShim 0.9.8r 8 Dec 2011
    
  • Cleaned osxkeychain for github.com

  • checked that ssh-agent is running, re-added my identity

The one weird thing that I saw is this:

$ ssh-add ~/.ssh/id_rsa.pub
Could not open a connection to your authentication agent.
$ ssh-agent sh -c 'ssh-add ~/.ssh/id_rsa.pub'
Enter passphrase for /Users/carlos/.ssh/id_rsa.pub:

So, my guess is that somehow ssh-agent is not working properly, but I don't have any idea why nor how to fix it.

caarlos0
  • 20,020
  • 27
  • 85
  • 160

4 Answers4

39

Since you mentioned, brew, I assume you're running on a Mac. This has also happened to me and the solution was to ensure that I added the passphrase to the keychain (the Mac version of ssh-agent, automatically launched on a Mac, includes keychain support):

$ ssh-add -K

And to store the passphrase for a different key:

$ ssh-add -K /path/to/private/key/file

Specifically in my case (since I use a separate key for GitHub):

$ ssh-add -K ~/.ssh/github_rsa

To automatically load keys into the ssh-agent and store passphrases in your keychain, you need to modify your ~/.ssh/config:

Host *
 AddKeysToAgent yes
 UseKeychain yes
 IdentityFile ~/.ssh/id_rsa

I obtained this information from here:

The above addresses the OP issue for ssh keys. The following is also useful for Mac users if you want to cache your HTTPS credentials as well. You can do this by using a credential helper. To tell git to use the osxkeychain helper, ensure this is added to your ~/.gitconfig (or ~/.config/git/config).

[credential]
    helper = osxkeychain

Instead of editing the file directly, you can set this entry from the command line:

$ git config --global credential.helper osxkeychain

See these links for more detail (including how to verify that your system has the osxkeychain helper installed):

Subfuzion
  • 1,789
  • 20
  • 18
  • 3
    ssh-add -K did the trick for me. Note that I started experiencing this after updating to macOS 10.12.3. – Fostah Jan 30 '17 at 14:10
  • 1
    This just works up to the next restart of my macbook. Then it asks for the passphrase again.. – Andru Apr 20 '17 at 11:34
  • @Andru I added more information about the osx keychain helper, which will take care of restarts. – Subfuzion Jul 09 '17 at 05:53
  • @Subfuzion Thanks, but what do you mean by "Finally, ensure this is added to your ~/.gitconfig to"? `[credential] helper = osxkeychain` was already part of my `~/.gitconfig` file.. – Andru Aug 21 '17 at 14:48
  • @Andru I edited the answer to make it clearer this is just extra information related to caching HTTPS credentials. The OP question related to ssh. The part about updating your ~/.ssh/config pertains to ensuring the passphrase is automatically added to the keychain. – Subfuzion Aug 23 '17 at 17:57
  • @Fostah, apple changed the behavior in 10.12.2 https://developer.apple.com/library/archive/technotes/tn2449/_index.html – wisbucky Sep 18 '19 at 00:33
2

Seems like something went wrong with my ssh-agent.

I tried to stop it (with kill -9) and start it again (with ssh-agent), but it wouldn't fix the problem.

After I ran eval 'ssh-agent -s' it all started to work as expected again.

Still I have no idea why this happened..

caarlos0
  • 20,020
  • 27
  • 85
  • 160
0

In my case it happened because I changed the ssh key I use for Github and forgot to change the entry in ~/.ssh/config.

I should have changed the line IdentityFile ~/.ssh/id_rsa to the location of the new SSH key.

Andru
  • 5,954
  • 3
  • 39
  • 56
0

In my case, on PopOs (Ubuntu) all I had to do was

ssh-add ~/.ssh/{ssh_key}

I'm not sure what caused it to go missing in the first place though.

dantheman
  • 123
  • 5