ssh-add complains: Could not open a connection to your authentication agent

I’ve been trying to get ssh-add working on a RaspberryPi running Raspbian.

I can start ssh-agent, when I do it gives the following output into the terminal:

SSH_AUTH_SOCK=/tmp/ssh-06TcpPflMg58/agent.2806; export SSH_AUTH_SOCK;
echo Agent pid 2807;

If I run ps aux | grep ssh I can see it is running.

Then I try to run ssh-add in order to add my key passphrase, and I get the following:

Could not open a connection to your authentication agent.

Any ideas?

Asked By: Daniel Groves


Your shell is meant to evaluate that shell code output by ssh-agent. Run this instead:

eval "$(ssh-agent)"

Or if you’ve started ssh-agent already, copy paste it to your shell prompt (assuming you’re running a Bourne-like shell).

ssh commands need to know how to talk to the ssh-agent, they know that from the SSH_AUTH_SOCK environment variable.

Answered By: Stéphane Chazelas

Try this one:

$ ssh-agent /bin/sh
$ ssh-add $yourkey
Answered By: user48656

If using csh as a shell (FreeBSD PI) this could work:

eval `ssh-agent -c`

next you only need to do something like:

ssh-add ~/.ssh/id_rsa
Answered By: nbari

This question has been also very well covered on Stackoverflow.

eval `ssh-agent -s`

Answered By: xaa

You may also use the following syntax:

ssh-agent sh -c 'ssh-add && echo Do some stuff here.'
Answered By: kenorb

Try this:
go to C:$Installation_Folder$Gitcmd
and execute:


It will open a cmd command and run the ssh-agent the right way.

ssh-add ~/.ssh/id_rsa should then work.

Answered By: sab125

This was confusing, and has 2 possible good answers, depending on whether the user is attempting to add a ssh key on a Linux or on Windows (as I am). This probably doesn’t answer the OP, but is an expansion for git-bash. I run both Windows and Ubuntu for development, and my git installation is slightly different in each.

Try this: go to C:$Installation_Folder$Gitcmd and execute:

start-ssh-agent It will open a cmd command and run the ssh-agent the
right way.

.. was a good Windows answer, but failed to specify that you were expected to go through Windows Explorer to find the Git installation folder, and run the the Windows shell would open on completion of step 1.
“go to C:$Installation_Folder$Gitcmd”

Step 2: you just need to double-click


On step 3, you go back to git-bash or whichever *nix terminal emulator you are running and run ssh-add. If you used the default name for the ssh public key, you don’t have to add the name as ssh-add uses that automatically.

Answered By: Wolf Halton

There’s a couple of ways if you wish to use an identity with sudo,
e.g. sudo npm install.

Long way, but also gives you root access so you won’t need to use sudo a lot.

  1. Run
    sudo ssh-agent bash
    This will ask for your password and after will run ssh-agent with the superuser’s privileges,
  2. Navigate to your users’ .ssh folder
    cd /home/user/.ssh
  3. From there you can run

    ssh-add id_rsa
    npm install git+ssh://git@YOUR-PROJECT-URL -g && npm link PROJECT-NAME

Short way, only your user (unless you want to sudo a lot)

eval "$(ssh-agent)"
sudo SSH_AUTH_SOCK="$SSH_AUTH_SOCK" npm install git+ssh://git@PROJECT-URL -g && sudo npm link PROJECT-NAME
Answered By: Belldandu

The easiest solution does not have to be bad.

You need neither ssh-agent nor ssh-add when you use a passwordless private key, see

By this, I got rid of the error Could not open a connection to your authentication agent. ssh-add exit code 2.

That does not need to be insecure: you simply need to delete both keys of the key pair right after their usage. That means, you must delete the public key on the server and delete the private key on the client. It goes without saying: never use them again, do not even keep a backup somewhere.

Mind that you can create the public key from a private key, but not the other way round. Normally, it should suffice to simply delete the public key from the registered keys on your Git portal, but they should better be both deleted, so that the same public key can never ever be used again. Even if someone had stolen your private key, it would be of no use if you simply never use its public key again.

With a passwordless private key, you can even use it in Docker to get around any password entries. You can clone a git repo without any password, the passwordless private key is all you need. See Dockerfile: clone repo with passwordless private key. Errors: “authentication agent” or “read_passphrase: can’t open /dev/tty” as an example.

Categories: Answers Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.