Could not open a connection to your authentication agent

Asked 2023-09-20 20:54:42 View 754,863

I am running into this error of:

$ git push heroku master
Warning: Permanently added the RSA host key for IP address '50.19.85.132' to the list of known hosts.
!  Your key with fingerprint b7:fd:15:25:02:8e:5f:06:4f:1c:af:f3:f0:c3:c2:65 is not authorized to access bitstarter.

I tried to add the keys and I get this error below:

$ ssh-add ~/.ssh/id_rsa.pub
Could not open a connection to your authentication agent.
  • Question: once you've gone through EVERY answer on this page, and none of them work. What can you do next? - anyone
  • @BrandonBertelsen Try this one: $ ssh-agent /bin/sh and $ ssh-add $yourkey - anyone
  • @BrandonBertelsen Next you should use git config --list to see if you have set credential.helper - if you have you should remove this setting, as the helper is being unhelpful. - anyone
  • Also see unix.stackexchange.com/a/48868 - anyone

Answers

Did You Start ssh-agent?

You might need to start ssh-agent before you run the ssh-add command:

eval `ssh-agent -s`
ssh-add

Note that this will start the agent for msysgit Bash on Windows. If you're using a different shell or operating system, you might need to use a variant of the command, such as those listed in the other answers.

See the following answers:

  1. ssh-add complains: Could not open a connection to your authentication agent
  2. Git push requires username and password (contains detailed instructions on how to use ssh-agent)
  3. How to run (git/ssh) authentication agent?.
  4. Could not open a connection to your authentication agent

To automatically start ssh-agent and allow a single instance to work in multiple console windows, see Start ssh-agent on login.

Why do we need to use eval instead of just ssh-agent?

SSH needs two things in order to use ssh-agent: an ssh-agent instance running in the background, and an environment variable set that tells SSH which socket it should use to connect to the agent (SSH_AUTH_SOCK IIRC). If you just run ssh-agent then the agent will start, but SSH will have no idea where to find it.

from this comment.

Public vs Private Keys

Also, whenever I use ssh-add, I always add private keys to it. The file ~/.ssh/id_rsa.pub looks like a public key, I'm not sure if that will work. Do you have a ~/.ssh/id_rsa file? If you open it in a text editor, does it say it's a private key?

Answered   2023-09-20 20:54:42

  • @xtian I'm not sure that I understand the issue. I don't know the exact details, but I'm guessing that the private key is never sent over the network. I think ssh-add merely decrypts an encrypted private key on the host machine, so that it can be used locally...it's never sent to anyone. I'm guessing that only the public keys are ever sent over a network. Is my understanding incorrect? - anyone
  • You're too kind Cupcake. You're absolutely right. My bad. ex here; ssh-add adds the private key for the user's ssh-agent (running process) can act on the client/host's behalf with the server accepting rsa-whatever keys. Geez. I don't know what made me so excited. - anyone
  • I'm curious why eval ssh-agent -s works, but ssh-agent on it's own doesn't. - anyone
  • @DanielM: SSH needs two things in order to use ssh-agent: an ssh-agent instance running in the background, and an environment variable set that tells SSH which socket it should use to connect to the agent (SSH_AUTH_SOCK IIRC). If you just run ssh-agent then the agent will start, but SSH will have no idea where to find it. - anyone
  • Thanks for replying though. I just noticed that you used backquote in the command. I am so confused why the backquote (`) work for eval command but not single/double quote (', ") on my bash shell? - anyone

I tried the other solutions to no avail. I made more research and found that the following command worked. I am using Windows 7 and Git Bash.

eval $(ssh-agent)

More information in: https://coderwall.com/p/rdi_wq (web archive version)

Answered   2023-09-20 20:54:42

  • Worked on Windows 8 too. - anyone
  • Great !! Worked with windows 10 also. - anyone
  • Worked on Manjaro - anyone
  • $ eval "$(ssh-agent -s)" worked for me on Windows 10 - anyone
  • thank you, worked like a charm! depending on which console you are using, it might not know which socket to use to connect to the ssh agent service, eval seems to fix that for you by setting the environment variable for that console, at least how I understand it - anyone

The following command worked for me. I am using CentOS.

exec ssh-agent bash

Answered   2023-09-20 20:54:42

  • Thanks, this worked for me, I ran 'exec ssh-agent zsh' for my shell. - anyone
  • According to this deleted "answer", it works on Amazon Linux AMI too. I guess it's a Unix/Linux/*nix solution. - anyone
  • I ssh'ed into a docker container and ssh-add my.id_rsa would fail on me. But exec ssh-agent zsh gave a environment where I could ssh-add with no problem. And I'm inside my docker container :) - anyone
  • Can anyone please briefly explain what the issue might have been? Thanks :) This worked on my Ubuntu 14.04 too. - anyone
  • I did the same but exec ssh-agent fish. None of the other solutions worked for me. This should be the accepted answer. I could not add my identity on Raspbian buster. - anyone

Could not open a connection to your authentication agent

To resolve this error:

bash:

$ eval `ssh-agent -s`

tcsh:

$ eval `ssh-agent -c`

Then use ssh-add as you normally would.


Hot Tip:

I was always forgetting what to type for the above ssh-agent commands, so I created an alias in my .bashrc file like this:

alias ssh-agent-cyg='eval `ssh-agent -s`'

Now instead of using ssh-agent, I can use ssh-agent-cyg

E.g.

$ ssh-agent-cyg
SSH_AUTH_SOCK=/tmp/ssh-n16KsxjuTMiM/agent.32394; export SSH_AUTH_SOCK;
SSH_AGENT_PID=32395; export SSH_AGENT_PID;
echo Agent pid 32395;
$ ssh-add ~/.ssh/my_pk

Original Source of fix:

http://cygwin.com/ml/cygwin/2011-10/msg00313.html

Answered   2023-09-20 20:54:42

MsysGit or Cygwin

If you're using Msysgit or Cygwin you can find a good tutorial at SSH-Agent in msysgit and cygwin and bash:

  1. Add a file called .bashrc to your home folder.

  2. Open the file and paste in:

    #!/bin/bash
    eval `ssh-agent -s`
    ssh-add
    
  3. This assumes that your key is in the conventional ~/.ssh/id_rsa location. If it isn't, include a full path after the ssh-add command.

  4. Add to or create file ~/.ssh/config with the contents

    ForwardAgent yes
    

    In the original tutorial the ForwardAgent param is Yes, but it's a typo. Use all lowercase or you'll get errors.

  5. Restart Msysgit. It will ask you to enter your passphrase once, and that's it (until you end the session, or your ssh-agent is killed.)

Mac/OS X

If you don't want to start a new ssh-agent every time you open a terminal, check out Keychain. I'm on a Mac now, so I used the tutorial ssh-agent with zsh & keychain on Mac OS X to set it up, but I'm sure a Google search will have plenty of info for Windows.

Update: A better solution on Mac is to add your key to the Mac OS Keychain:

ssh-add -K ~/.ssh/id_rsa

Simple as that.

Answered   2023-09-20 20:54:42

  • FYI: merged from stackoverflow.com/questions/4083079/… - anyone
  • When I do eval `ssh-agent -s` the process does not stop when I exit cygwin. - anyone
  • Is there a way to setup the config for Windows / gitbash so you dont have to do it every time? - anyone
  • It should be ps -u $(whoami) | grep ssh-agent &> /dev/null || eval $(ssh-agent) - otherwise a new ssh-agent is started everytime. Killed my machine every now and then when user had cronjobs. - anyone
  • ForwardAgent yes isn't necessary and is a nontheoretical security risk if it's set for any untrusted server. Local access to your key-agent should function regardless of this setting. - anyone

Run

ssh-agent bash
ssh-add

To get more details you can search

ssh-agent

or run

man ssh-agent

Answered   2023-09-20 20:54:42

  • I would recommend that If you are working with git bash on windows then try this. - anyone
  • @neer17 this allowed me to use ssh-copy-id on ubuntu. Unsure why I couldn't add the key without using ssh-agent to launch bash. - anyone
  • This is the one for Windows! (tested on W10pro build 1809 and git 2.21.0) - anyone
  • I am using ZSH and this works for me: ssh-agent zsh; ssh-add - anyone
  • My Cygwin and Git Bash were working fine, but for some reason the Console opened from the Fork client didn't work, and this fixed it. I'll probably just use Git Bash in the future, but at least I know how to get it to work now :) - anyone

ssh-add and ssh (assuming you are using the openssh implementations) require an environment variable to know how to talk to the ssh agent. If you started the agent in a different command prompt window to the one you're using now, or if you started it incorrectly, neither ssh-add nor ssh will see that environment variable set (because the environment variable is set locally to the command prompt it's set in).

You don't say which version of ssh you're using, but if you're using cygwin's, you can use this recipe from SSH Agent on Cygwin:

# Add to your Bash config file
SSHAGENT=/usr/bin/ssh-agent
SSHAGENTARGS="-s"
if [ -z "$SSH_AUTH_SOCK" -a -x "$SSHAGENT" ]; then
    eval `$SSHAGENT $SSHAGENTARGS`
    trap "kill $SSH_AGENT_PID" 0
fi

This will start an agent automatically for each new command prompt window that you open (which is suboptimal if you open multiple command prompts in one session, but at least it should work).

Answered   2023-09-20 20:54:42

  • FYI: merged from stackoverflow.com/questions/4083079/… - anyone
  • I was linked here from another SO question. Can I ask why it's necessary to add this to the Bash config file? On my machine all I need to do is run eval $(ssh-agent) and I can use password-less ssh for every new terminal window. - anyone
  • That shouldn't be possible, unless you're launching new terminals from that terminal. - anyone
  • Can you outline what this piece of code does? People who don't get the answer by themselves (such as me) won't know what this actually does. - anyone
  • @shuhalo I literally wrote underneath the code what it does. - anyone

I faced the same problem for Linux, and here is what I did:

Basically, the command ssh-agent starts the agent, but it doesn't really set the environment variables for it to run. It just outputs those variables to the shell.

You need to:

eval `ssh-agent`

and then do ssh-add. See Could not open a connection to your authentication agent.

Answered   2023-09-20 20:54:42

Instead of using ssh-agent -s, I used eval `ssh-agent -s` to solve this issue.

Here is what I performed step by step (step 2 onwards on Git Bash):

  1. Cleaned up my .ssh folder at C:\user\<username>\.ssh\
  2. Generated a new SSH key: ssh-keygen -t rsa -b 4096 -C "xyz@abc.com"
  3. Check if any process id(ssh agent) is already running. ps aux | grep ssh
  4. (Optional) If found any in step 3, kill those kill <pids>
  5. Started the SSH agent $ eval `ssh-agent -s`
  6. Added SSH key generated in step 2 to the SSH agent ssh-add ~/.ssh/id_rsa

Answered   2023-09-20 20:54:42

  • 'eval' is not recognized as an internal or external command, operable program or batch file. - anyone
  • You can just restart agent by eval ssh-agent -s and add older key using ssh-add ~/.ssh/id_rsa. If you generate new SSH key then you will need to update that key in all your services such as github, bitbucket, etc. - anyone
  • Is there a way make this survive Windows restarts? After the restart it don't work until I launch "eval ssh-agent -s" and "ssh-add ~/.ssh/id_rsa" again. - anyone
  • after searching for a long time, this is the solution that worked - anyone

Try to do the following steps:

  1. Open Git Bash and run: cd ~/.ssh

  2. Try to run agent: eval $(ssh-agent)

  3. Right now, you can run the following command: ssh-add -l

Answered   2023-09-20 20:54:42

  • This is the only solution here that worked for me (on windows 7). First I used the ps aux | grep ssh and the kill command in Rick's answer to kill the agents. After that ssh-add worked without the -l switch (Using -l gave an error). eval 'ssh-agent' as in Rick's answer did not work, I had to use eval $(ssh-agent) like in Chechoro's answer here. - anyone
  • +1 I had the exact same problem as OP (Windows 7) and this is the only solution that worked for me. - anyone
  • FYI: merged from stackoverflow.com/questions/4083079/… - anyone
  • @Frug It is backticks, not apostrophes; in shell script that means to execute what's contained inside. $(ssh-agent) is equivalent to `ssh-agent` but more readable. - anyone
  • I had the same issue in Ubuntu 21.10 and the above solution worked; when simply running ssh-agent didn't, but why? Why have to use eval? - anyone

In Windows 10 I tried all answers listed here, but none of them seemed to work. In fact, they give a clue. To solve a problem, simply you need three commands. The idea of this problem is that ssh-add needs the SSH_AUTH_SOCK and SSH_AGENT_PID environment variables to be set with the current ssh-agent sock file path and pid number.

ssh-agent -s > temp.txt

This will save the output of ssh-agent in a file. The text file content will be something like this:

SSH_AUTH_SOCK=/tmp/ssh-kjmxRb2764/agent.2764; export SSH_AUTH_SOCK;
SSH_AGENT_PID=3044; export SSH_AGENT_PID;
echo Agent pid 3044;

Copy something like "/tmp/ssh-kjmxRb2764/agent.2764" from the text file and run the following command directly in the console:

set SSH_AUTH_SOCK=/tmp/ssh-kjmxRb2764/agent.2764

Copy something like "3044" from the text file and run the following command directly in the console:

set SSH_AGENT_PID=3044

Now when environment variables (SSH_AUTH_SOCK and SSH_AGENT_PID) are set for the current console session, run your ssh-add command and it will not fail again to connect to ssh agent.

Answered   2023-09-20 20:54:42

  • Thank you! This was exactly what I needed. - anyone
  • These steps are the same as doing 'eval $(ssh-agent)' - anyone
  • Fixed on my Windows 10! Thank you! - anyone
  • Thank you for explaining exactly what's going on, instead of just throwing bash commands at the answer. - anyone
  • This actually helped me to save the issue on Linux...just needed to replace set by export - anyone

One thing I came across was that eval did not work for me using Cygwin, what worked for me was ssh-agent ssh-add id_rsa.

After that I came across an issue that my private key was too open, the solution I managed to find for that (from here):

chgrp Users id_rsa

as well as

chmod 600 id_rsa

finally I was able to use:

ssh-agent ssh-add id_rsa

Answered   2023-09-20 20:54:42

  • Did you use eval `ssh-agent`, with the backticks ` around ssh-agent, as shown in my answer? That worked just fine for me in Cygwin. You seem to be right that ssh-agent ssh-add also works though, at least in the msysgit Bash. However, note that id_rsa is the default key that's used, so you don't need to specify it with ssh-agent ssh-add id_rsa. - anyone
  • I believe I had used the backticks, but for me still no dice - anyone
  • OMG YOU SOLVED MY FEW HOURS FRUSTATION. Thanks! - anyone

For Windows users, I found cmd eval `ssh-agent -s` didn't work, but using Git Bash worked a treat:

eval `ssh-agent -s`; ssh-add KEY_LOCATION

And making sure the Windows service "OpenSSH Key Management" wasn't disabled.

Answered   2023-09-20 20:54:42

  • Could you please tell why do we need eval at all? - anyone
  • @MohammedNoureldin Execute ssh-agent by itself and you'll see why. It produces commands that need to be evaluated, otherwise they aren't executed. - anyone
  • @amzon-ex I tried that, but I did not understand the behavior, could you please elaborate? - anyone
  • @MohammedNoureldin Running just ssh-agent produces some text in a string form which if you typed by yourself in the terminal, they would act as commands or variable assignments. eval does the job of taking the string and converting them to actual runnable commands. - anyone

To amplify on n3o's answer for Windows 7...

My problem was indeed that some required environment variables weren't set, and n3o is correct that ssh-agent tells you how to set those environment variables, but doesn't actually set them.

Since Windows doesn't let you do "eval," here's what to do instead:

Redirect the output of ssh-agent to a batch file with

ssh-agent > temp.bat

Now use a text editor such as Notepad to edit temp.bat. For each of the first two lines:

  • Insert the word "set" and a space at the beginning of the line.
  • Delete the first semicolon and everything that follows.

Now delete the third line. Your temp.bat should look something like this:

set SSH_AUTH_SOCK=/tmp/ssh-EorQv10636/agent.10636
set SSH_AGENT_PID=8608

Run temp.bat. This will set the environment variables that are needed for ssh-add to work.

Answered   2023-09-20 20:54:42

I just got this working. Open your ~/.ssh/config file.

Append the following-

Host github.com
 IdentityFile ~/.ssh/github_rsa

The page that gave me the hint Set up SSH for Git said that the single space indentation is important... though I had a configuration in here from Heroku that did not have that space and works properly.

Answered   2023-09-20 20:54:42

  • This does not help with entering the passphrase for the SSH key. - anyone
  • If you don't want to enter the passphrase, create a key without one. There is no point in having a passphrase if you're just going to store it in your script anyway. - anyone
  • That is the whole point of using ssh-agent. The password to your keys is stored in a secure keychain, handled by ssh-agent. - anyone
  • This is the best answer for me here. Had lots of problems with ssh-agent starting thousands of times in Windows 7 and that kill trap didn't do the trick. I don't mind having no passphrase on my personal computer at home. - anyone

If you follow these instructions, your problem would be solved.

If you’re on a Mac or Linux machine, type:

eval "$(ssh-agent -s)"

If you’re on a Windows machine, type:

ssh-agent -s

Answered   2023-09-20 20:54:42

I had the same problem on Ubuntu and the other solutions didn't help me.

I finally realized what my problem was. I had created my SSH keys in the /root/.ssh folder, so even when I ran ssh-add as root, it couldn't do its work and kept saying:

Could not open a connection to your authentication agent.

I created my SSH public and private keys in /home/myUsername/ folder and I used

ssh-agent /bin/sh

Then I ran

ssh-add /home/myUsername/.ssh/id_rsa

And problem was solved this way.

Note: For accessing your repository in Git, add your Git password when you are creating SSH keys with ssh-keygen -t rsa -C "your Git email here".

Answered   2023-09-20 20:54:42

Let me offer another solution. If you have just installed Git 1.8.2.2 or thereabouts, and you want to enable SSH, follow the well-writen directions.

Everything through to Step 5.6 where you might encounter a slight snag. If an SSH agent is already be running you could get the following error message when you restart bash

Could not open a connection to your authentication agent

If you do, use the following command to see if more than one ssh-agent process is running

ps aux | grep ssh

If you see more than one ssh-agent service, you will need to kill all of these processes. Use the kill command as follows (the PID will be unique on your computer)

kill <PID>

Example:

kill 1074

After you have removed all of the ssh-agent processes, run the px aux | grep ssh command again to be sure they are gone, then restart Bash.

Voila, you should now get something like this:

Initializing new SSH agent...
succeeded
Enter passphrase for /c/Users/username/.ssh/id_rsa:

Now you can continue on Step 5.7 and beyond.

Answered   2023-09-20 20:54:42

This will run the SSH agent and authenticate only the first time you need it, not every time you open your Bash terminal. It can be used for any program using SSH in general, including ssh itself and scp. Just add this to /etc/profile.d/ssh-helper.sh:

ssh-auth() {
    # Start the SSH agent only if not running
    [[ -z $(ps | grep ssh-agent) ]] && echo $(ssh-agent) > /tmp/ssh-agent-data.sh

    # Identify the running SSH agent
    [[ -z $SSH_AGENT_PID ]] && source /tmp/ssh-agent-data.sh > /dev/null

    # Authenticate (change key path or make a symlink if needed)
    [[ -z $(ssh-add -l | grep "/home/$(whoami)/.ssh/id_rsa") ]] && ssh-add
}

# You can repeat this for other commands using SSH
git() { ssh-auth; command git "$@"; }

Note: this is an answer to this question, which has been merged with this one. That question was for Windows 7, meaning my answer was for Cygwin/MSYS/MSYS2. This one seems for some Unix, where I wouldn't expect the SSH agent needing to be managed like this.

Answered   2023-09-20 20:54:42

  • FYI: merged from stackoverflow.com/questions/4083079/… - anyone
  • ssh-agent process do not seems to be associated with the same terminal used to run it. I guess ps -A | grep ssh-agent or ps h -C ssh-agent should be used instead of ps | grep ssh-agent - anyone
  • Using a hyphen in the function name might not be recommended. I don't know why, but read this. For instance, I found that echo ssh-auth | bash will fail. - anyone
  • You can simplyfy this a bit more. Just check whether it is running and assign the env variables. And then add AddKeysToAgent yes (or use prompt) to your ssh config entry (use Host * for all Hosts.) That way you will only be asked for the SSH password if you actually try to connect otherwise you might be asked for a password for a simple git diff or git status. - anyone
  • $HOME/.ssh might be more robust than /home/$(whoami)/.ssh - anyone

The basic solution to run ssh-agent is answered in many answers. However runing ssh-agent many times (per each opened terminal or per remote login) will create a many copies ot ssh-agent running in memory. The scripts which is suggested to avoid that problem is long and need to write and/or copy separated file or need to write too many strings in ~/.profile or ~/.schrc. Let me suggest simple two string solution:

For sh, bash, etc:

# ~/.profile
if ! pgrep -q -U `whoami` -x 'ssh-agent'; then ssh-agent -s > ~/.ssh-agent.sh; fi
. ~/.ssh-agent.sh

For csh, tcsh, etc:

# ~/.schrc
sh -c 'if ! pgrep -q -U `whoami` -x 'ssh-agent'; then ssh-agent -c > ~/.ssh-agent.tcsh; fi'
eval `cat ~/.ssh-agent.tcsh`

What is here:

  • search the process ssh-agent by name and by current user
  • create appropriate shell script file by calling ssh-agent and run ssh-agent itself if no current user ssh-agent process found
  • evaluate created shell script which configure appropriate environment

It is not necessary to protect created shell script ~/.ssh-agent.tcsh or ~/.ssh-agent.sh from another users access because: at-first communication with ssh-agent is processed through protected socket which is not accessible to another users, and at-second another users can found ssh-agent socket simple by enumeration files in /tmp/ directory. As far as about access to ssh-agent process it is the same things.

Answered   2023-09-20 20:54:42

In Windows 10, using the Command Prompt terminal, the following works for me:

ssh-agent cmd 
ssh-add

You should then be asked for a passphrase after this:

Enter passphrase for /c/Users/username/.ssh/id_rsa:

Answered   2023-09-20 20:54:42

  • For non-Windows users: ssh-agent bash or ssh-agent zsh might be the solution. - anyone
  • Thank you, living in Windows is sure frustating. - anyone

Try the following:

ssh-agent sh -c 'ssh-add && git push heroku master'

Answered   2023-09-20 20:54:42

  • Looks like a clean way to do this. - anyone

Use parameter -A when you connect to server, example:

ssh -A root@myhost

from man page :

-A Enables forwarding of the authentication agent connection.  
   This can also be specified on a per-host basis in a configuration file.

   Agent forwarding should be enabled with caution.  Users with the ability to bypass file permissions on the remote host (for the agent's
   UNIX-domain socket) can access the local agent through the forwarded 
   connection.  An attacker cannot obtain key material from the agent,
   however they can perform operations on the keys that enable them to
   authenticate using the identities loaded into the agent.

Answered   2023-09-20 20:54:42

  • What does it do and why? - anyone
  • as per man page ... man ssh ... -A Enables forwarding of the authentication agent connection. This can also be specified on a per-host basis in a configuration file. - anyone

I had this problem, when I started ssh-agent, when it was already running. It seems that the multiple instances conflict with each other.

To see if ssh-agent is already running, check the value of the SSH_AGENT_SOCK environment variable with:

echo $SSH_AGENT_SOCK

If it is set, then the agent is presumably running.

To check if you have more than one ssh-agent running, you can review:

ps -ef | grep ssh

Of course, then you should kill any additional instances that you created.

Answered   2023-09-20 20:54:42

  • I think running eval $(ssh-agent) is supposed to create a new agent with a different PID every time, though I could be wrong. - anyone
  • What do you mean by "Gets confused"? E.g., what or who gets confused? Please respond by editing (changing) your answer, not here in comments (without "Edit:", "Update:", or similar - the answer should appear as if it was written today). - anyone
  • just to add, in order to kill a process use this command kill <process_id> pid - anyone
  • Is this answer really supposed to say SSH_AGENT_SOCK? When I run ssh-agent, I see it setting environment variables named SSH_AUTH_SOCK and SSH_AGENT_PID, but nothing named SSH_AGENT_SOCK. - anyone

Read user456814's answer for explanations. Here I only try to automate the fix.

If you using a Cygwin terminal with Bash, add the following to the $HOME/.bashrc file. This only starts ssh-agent once in the first Bash terminal and adds the keys to ssh-agent. (I am not sure if this is required on Linux.)

###########################
# start ssh-agent for
# ssh authentication with github.com
###########################
SSH_AUTH_SOCK_FILE=/tmp/SSH_AUTH_SOCK.sh
if [ ! -e $SSH_AUTH_SOCK_FILE ]; then
    # need to find SSH_AUTH_SOCK again.
    # restarting is an easy option
    pkill ssh-agent
fi
# check if already running
SSH_AGENT_PID=`pgrep ssh-agent`
if [ "x$SSH_AGENT_PID" == "x" ]; then
#   echo "not running. starting"
    eval $(ssh-agent -s) > /dev/null
    rm -f $SSH_AUTH_SOCK_FILE
    echo "export SSH_AUTH_SOCK=$SSH_AUTH_SOCK" > $SSH_AUTH_SOCK_FILE
    ssh-add $HOME/.ssh/github.com_id_rsa 2>&1 > /dev/null
#else
#   echo "already running"
fi
source $SSH_AUTH_SOCK_FILE

Don’t forget to add your correct keys in the "ssh-add" command.

Answered   2023-09-20 20:54:42

I had a similar problem when I was trying to get this to work on Windows to connect to the stash via SSH.

Here is the solution that worked for me.

  1. Turns out I was running the Pageant ssh agent on my Windows box - I would check what you are running. I suspect it is Pageant as it comes as default with PuTTY and WinSCP.

  2. The ssh-add does not work from command line with this type of agent

  3. You need to add the private key via the Pageant UI window which you can get by double-clicking the Pageant icon in the taskbar (once it is started).

  4. Before you add the key to Pageant you need to convert it to PPK format. Full instructions are available here How to convert SSH key to ppk format

  5. That is it. Once I uploaded my key to stash I was able to use Sourcetree to create a local repository and clone the remote.

Answered   2023-09-20 20:54:42

For Bash built into Windows 10, I added this to file .bash_profile:

if [ -z $SSH_AUTH_SOCK ]; then
    if [ -r ~/.ssh/env ]; then
            source ~/.ssh/env
            if [ `ps -p $SSH_AGENT_PID | wc -l` = 1 ]; then
                    rm ~/.ssh/env
                    unset SSH_AUTH_SOCK
            fi
    fi
fi

if [ -z $SSH_AUTH_SOCK ]; then
    ssh-agent -s | sed 's/^echo/#echo/'> ~/.ssh/env
    chmod 600 ~/.ssh/env
    source ~/.ssh/env > /dev/null 2>&1
fi

Answered   2023-09-20 20:54:42

  • I don't know why you paste so much text if you could've just said to call $(ssh-agent -s) to set the agent's environment. - anyone
  • This worked for me when I used it the first time. But after a system reboot, it actually caused the issue to be worse. With ssh not working at all anymore. Not recommended. - anyone

Using Git Bash on Windows 8.1 E, my resolution was as follows:

eval $(ssh-agent) > /dev/null
ssh-add ~/.ssh/id_rsa

Answered   2023-09-20 20:54:42

  • Why is it necessary to output to /dev/null? Your answer basically does the exact same thing as this one. - anyone
  • Also, I'd like to point out that ~/.ssh/id_rsa is the default key, so you shouldn't have to specify ssh-add ~/.ssh/id_rsa, just ssh-add should work. - anyone
  • I tried your solution in this same environment @Cupcake, it doesn't work. I don't get why this is the answer either, but unless I did it this way it never worked. - anyone
  • @user456814 The > /dev/null is widely used, see for example stackoverflow.com/questions/17846529/…. I guess it is not really necessary. The output redirect just gets rid of the printed "Agent pid blah blah"., see blog.joncairns.com/2013/12/understanding-ssh-agent-and-ssh-add. - anyone
  • What is "E" after Windows 8.1? A particular edition of Windows 8.1? Or something else? - anyone

I resolved the error by force stopping (killed) git processes (ssh agent), then uninstalling Git, and then installing Git again.

Answered   2023-09-20 20:54:42

  • I suspect that all you needed to do was just kill any existing agent processes, then restart one, instead of having to reinstall Git. - anyone

This worked for me.

In the CMD window, type the following command:

cd path-to-Git/bin # (for example,cd C:\Program Files\Git\bin)
bash
exec ssh-agent bash
ssh-add path/to/.ssh/id_rsa

Answered   2023-09-20 20:54:42

  • I've got it working like this in cmd as well but still get the error in git bash.. I don't understand. How could I make it work in git bash as well? - anyone