108

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

one noa
  • 345
  • 1
  • 3
  • 10
leipzy
  • 11,676
  • 6
  • 19
  • 24

7 Answers7

212

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

eval `ssh-agent -s`

That question was answered in this topic: Could not open a connection to your authentication agent

Community
  • 1
  • 1
Kritana
  • 2,236
  • 1
  • 14
  • 7
53

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.

Derek Foulk
  • 1,892
  • 1
  • 19
  • 37
  • 1
    I am not sure about using `"C:\Path\To"` in Git-Bash under windows. I remember a while back I received errors when using typical windows paths. You can also use `ssh-add /c/Users/YourName/.ssh/id_rsa`. - I thank you for this answer though. Following the Git docs resulted in many ssh agents running. After killing them, and using the alternate eval method, this worked perfect. – Wade Jul 18 '15 at 20:44
  • I didn't realize that there was another way to specify a path in Windows. Good to know. You are welcome, just hope it saves everyone the headache I endured. – Derek Foulk Jul 19 '15 at 05:20
  • Ctrl+Alt+Del and hit End Process to stop each 'ssh-agent.exe' step was all what I needed! – Ferit Buyukkececi Jan 14 '16 at 14:13
  • @FeritBuyukkececi - Glad to hear you were able to get up and running! – Derek Foulk Jan 14 '16 at 20:59
  • This worked for me, but only temporary. Is there a trick that it will always work? This is kinda annoying :/ – Isengo Jan 15 '17 at 15:13
  • 1
    @Isengo this worked permanently for me. I am unsure why it wouldn't for you. Maybe something is preventing the SSH Agent from running after you restart your machine? – Derek Foulk Mar 17 '17 at 23:23
  • 1
    this needs to be moved to best answer – ALisboa Feb 09 '18 at 18:23
  • @Isengo - There may be something else wrong if this didn't resolve your issue permanently... – Derek Foulk Feb 10 '18 at 22:57
  • @ASignor - Thank you for the props! Glad this helped you... – Derek Foulk Feb 10 '18 at 22:58
  • @derekmx271 thanks for checking. I am using Ubuntu now - so I am good :) – Isengo Feb 12 '18 at 06:32
  • Awesome, it made the trick forme – Gregory Apr 09 '18 at 13:09
  • `eval $(ssh-agent -s)` worked for me - Ubuntu 22.04 LTS – irahorecka May 10 '23 at 15:05
23

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"

Jhourlad Estrella
  • 3,545
  • 4
  • 37
  • 66
16

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 git@bitbucket.org:Guiness/DoubleIrish.git
$ git remote -v
origin  git@bitbucket.org:Guiness/DoubleIrish.git (fetch)
origin  git@bitbucket.org:Guiness/DoubleIrish.git (push)
Mike@DUBLIN MINGW64 ~/source/repos/DoubleIrish (master)
$ git push -u origin master
[...]
git@bitbucket.org: 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

Varus Septimus
  • 1,510
  • 20
  • 13
3

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.

Avi
  • 31
  • 1
3

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.

Mihail Ignatiev
  • 843
  • 6
  • 16
-2

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"xxx@xx.com", 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.

Zhang Qun
  • 347
  • 2
  • 5
  • your advise is conflicting the OP question with irrelevant topics ... how you generate the ssh key pair is orthogonal to the issue at hand – Scott Stensland Apr 13 '17 at 17:23