Git Bash: Could not open a connection to your authentication agent

GitGithubSsh

Git Problem Overview


I'm new to Github and Generating SSH Keys look a neccessity. And was informed by my boss about this, so I need to comply.

I successfully created SSH Key but when I'm going to add it to the ssh-agent

this is what happens

What seems to be the problem?

enter image description here

Git Solutions


Solution 1 - Git

It seems you need to run ssh-agent before using it:

eval `ssh-agent -s`

That question was answered in this topic: https://stackoverflow.com/questions/17846529/could-not-open-a-connection-to-your-authentication-agent

Solution 2 - Git

I checked all the solutions on this post and the post that @kenorb referenced above, and I did not find any solution that worked for me.

I am using Git 1.9.5 Preview on Windows 7 with the following configuration:

  • Run Git from the Windows Command Prompt
  • Checkout Windows-style, commit Unix-style line endings

I used the 'Git Bash' console for everything... And all was well until I tried to install the SSH keys. GitHub's documentation says to do the following (don't run these commands until you finish reading the post):

> Ensure ssh-agent is enabled: > > ####If you are using Git Bash, turn on ssh-agent: > > # start the ssh-agent in the background > ssh-agent -s > # Agent pid 59566 > > ####If you are using another terminal prompt, such as msysgit, turn on ssh-agent: > > # start the ssh-agent in the background > eval $(ssh-agent -s) > # Agent pid 59566

Now of course I missed the fact that you were supposed to do one or the other. So, I ran these commands multiple times because the later ssh-add command was failing, so I returned to this step, and continued to retry over and over.

This results in 1 Windows 'ssh-agent' process being created every single time you run these commands (notice the new PID every time you enter those commands?)

So, Ctrl+Alt+Del and hit End Process to stop each 'ssh-agent.exe' process.

Now that all the messed up stuff from the failed attempts is cleaned up, I will tell you how to get it working...

In 'Git Bash':

Start the 'ssh-agent.exe' process

eval $(ssh-agent -s)

And install the SSH keys

ssh-add "C:\Users\MyName\.ssh\id_rsa"

*** Adjust the path above with your username, and make sure that the location of the*** /.ssh directory is in the correct place. I think you choose this location during the Git installation? Maybe not...

The part I was doing wrong before I figured this out was I was not using quotes around the 'ssh-add' location. The above is how it needs to be entered on Windows.

Solution 3 - Git

On Windows, you can use Run with one of the below commands.

For 32-Bit: >"C:\Program Files (x86)\Git\cmd\start-ssh-agent.cmd"

For-64 Bit: >"C:\Program Files\Git\cmd\start-ssh-agent.cmd"

Solution 4 - Git

Situation: MVS2017 App - Using 'Git Bash' on Windows 10 - Trying to connect to a BitBucket repository.

To be clear, when you install Git for Windows (https://git-scm.com/download/win), it comes with an utility called Git Bash.

enter image description here

So, I am in 'Git Bash', as follows:

Mike@DUBLIN MINGW64 ~/source/repos/DoubleIrish (master)
$ git remote add origin [email protected]:Guiness/DoubleIrish.git
$ git remote -v
origin  [email protected]:Guiness/DoubleIrish.git (fetch)
origin  [email protected]:Guiness/DoubleIrish.git (push)
Mike@DUBLIN MINGW64 ~/source/repos/DoubleIrish (master)
$ git push -u origin master
[...]
[email protected]: Permission denied (publickey).
fatal: Could not read from remote repository.

Is the private key set up?

$ ssh -V
OpenSSH_7.7p1, OpenSSL 1.0.2p  14 Aug 2018
$ ls -a ~/.ssh
./  ../  known_hosts

I can see that, at this point, my private key file (id_rsa) is missing. So I add it: (note: generating a pair of private-public keys is out of scope of my reply, but I can say that in Linux, you can use ssh-keygen for that.)

$ ls -a ~/.ssh
./  ../  id_rsa  known_hosts

OK, let's proceed:

$ ssh-agent
SSH_AUTH_SOCK=/tmp/ssh-KhQwFLAgWGYC/agent.18320; export SSH_AUTH_SOCK;
SSH_AGENT_PID=17996; export SSH_AGENT_PID;
echo Agent pid 17996;

$ ssh-add ~/.ssh/id_rsa
Could not open a connection to your authentication agent.

To solve this, I run:

$ ssh-agent bash

And then, again:

$ ssh-add ~/.ssh/id_rsa
Identity added: /c/Users/Mike.CORP/.ssh/id_rsa (/c/Users/Mike.CORP/.ssh/id_rsa)

It worked for me. Hope this helps

Solution 5 - Git

I was struggling with the problem as well.

After I typed $ eval 'ssh-agent -s' followed by $ssh-add ~/.ssh/id_rsa

I got the same complain: "Could not open a connection to your authentication agent". Then I realize there are two different kind of quotation on my computer's keyboard. So I tried the one at the same position as "~": $ eval ssh-agent -s $ ssh-add ~/.ssh/id_rsa

And bang it worked.

Solution 6 - Git

I would like to improve on the accepted answer

Downsides of using .bashrc with eval ssh-agent -s:

  1. Every git-bash will have it's own ssh-agent.exe process
  2. SSH key should be manually added every time you open git-bash

Here is my .bashrc that will eradicate above downsides

Put this .bashrc into your home directory (Windows 10: C:\Users\[username]\.bashrc) and it will be executed every time a new git-bash is opened and ssh-add will be working as a first class citizen

Read #comments to understand how it works

# Env vars used
# SSH_AUTH_SOCK - ssh-agent socket, should be set for ssh-add or git to be able to connect
# SSH_AGENT_PID - ssh-agent process id, should be set in order to check that it is running
# SSH_AGENT_ENV - env file path to share variable between instances of git-bash
SSH_AGENT_ENV=~/ssh-agent.env
# import env file and supress error message if it does not exist
. $SSH_AGENT_ENV 2> /dev/null

# if we know that ssh-agent was launched before ($SSH_AGENT_PID is set) and process with that pid is running 
if [ -n "$SSH_AGENT_PID" ] && ps -p $SSH_AGENT_PID > /dev/null 
then
  # we don't need to do anything, ssh-add and git will properly connect since we've imported env variables required
  echo "Connected to ssh-agent"
else   
  # start ssh-agent and save required vars for sharing in $SSH_AGENT_ENV file
  eval $(ssh-agent) > /dev/null
  echo export SSH_AUTH_SOCK=\"$SSH_AUTH_SOCK\" > $SSH_AGENT_ENV
  echo export SSH_AGENT_PID=$SSH_AGENT_PID >> $SSH_AGENT_ENV
  echo "Started ssh-agent"
fi

Also this script uses a little trick to ensure that provided environment variables are shared between git-bash instances by saving them into an .env file.

Solution 7 - Git

above solution doesn't work for me for unknown reason. below is my workaround which was worked successfully.

  1. DO NOT generate a new ssh key by using command ssh-keygen -t rsa -C"[email protected]", you can delete existing SSH keys.

  2. but use Git GUI, -> "Help" -> "Show ssh key" -> "Generate key", the key will saved to ssh automatically and no need to use ssh-add anymore.

Solution 8 - Git

Try using cygwin instead of bash. that worked for me

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionleipzyView Question on Stackoverflow
Solution 1 - GitKritanaView Answer on Stackoverflow
Solution 2 - GitDerek FoulkView Answer on Stackoverflow
Solution 3 - GitJhourlad EstrellaView Answer on Stackoverflow
Solution 4 - GitVarus SeptimusView Answer on Stackoverflow
Solution 5 - GitAviView Answer on Stackoverflow
Solution 6 - GitMihail IgnatievView Answer on Stackoverflow
Solution 7 - GitZhang QunView Answer on Stackoverflow
Solution 8 - Gituser11767754View Answer on Stackoverflow