210

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;
SSH_AGENT_PID=2807; export SSH_AGENT_PID;
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?

1
  • 9
    Try eval $(ssh-agent) Sep 22, 2012 at 15:28

9 Answers 9

312

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.

7
  • Thanks, I didn't quite understand how it works. Thats very muchly appreciated. Sep 22, 2012 at 17:30
  • 4
    Or you can enter ssh-agent bash and after the ssh-add will work
    – RaduM
    May 15, 2013 at 13:56
  • @Stephane, do I get it correctly that ssh-agent command just prints the output of it's execution while eval version actually runs the command?
    – Denys S.
    Oct 7, 2013 at 8:50
  • 5
    @DenysS. Well no, it does its setup and then tells your shell how to update its environment to be able to contact it. It cannot do that by itself. It can start a new shell with the updated environment (that's what ssh-agent bash does), but it cannot update the memory of a separate process running a foreign command (your already running shell). Oct 7, 2013 at 9:03
  • @StephaneChazelas, if my ssh-agent is started on boot but I still have to eval it in my shell does it mean that it is not automatically evaluated for every shell and has to be configured somehow by the root user?
    – Denys S.
    Oct 7, 2013 at 9:10
42

Try this one:

$ ssh-agent /bin/sh
$ ssh-add $yourkey
8
  • 6
    This actually worked for me where none of the other answers worked. Nov 12, 2014 at 18:06
  • 6
    If you are using the fishshell, this answer works. ssh-agent /usr/bin/fish.
    – slackmart
    Mar 29, 2016 at 18:17
  • 3
    I get an error: "Could not open a connection to your authentication agent." Aug 12, 2016 at 17:29
  • @slackmart If you installed fish in BSD, its location is /usr/local/bin/fish (alternatively /usr/ports/shells/fish). Thank the whereis command for helping me figure that out.
    – cjm
    Aug 26, 2016 at 4:28
  • Great @cjm, I use Slackware Linux, and that's the path that works for me.
    – slackmart
    Aug 27, 2016 at 11:35
17

This question has been also very well covered on Stackoverflow.

eval `ssh-agent -s`

ssh-add
1
  • 1
    You forgot the quotes around `...`. Without them, the split+glob operator is invoked on the output. Dec 9, 2015 at 9:56
6

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
1
  • 4
    I get an error: "Could not open a connection to your authentication agent." Aug 12, 2016 at 18:41
4

You may also use the following syntax:

ssh-agent sh -c 'ssh-add && echo Do some stuff here.'
3
  • This isn't working for me, if you go ssh-agent sh -c "ssh-add -l" it will tell you you have no identities.
    – Ain Tohvri
    Jul 1, 2015 at 13:43
  • Because you've to add it first, for example: ssh-agent sh -c "ssh-add && ssh-add -l" will print your identity.
    – kenorb
    Jul 3, 2015 at 17:25
  • RUN ssh-agent sh -c 'ssh-add /root/.ssh/id_rsa' worked for me in a dockerfile
    – Fl_ori4n
    Jul 8, 2021 at 14:31
2

Try this: go to C:\$Installation_Folder$\Git\cmd and execute:

start-ssh-agent 

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

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

1
  • "bash: start-ssh-agent: command not found" Aug 12, 2016 at 18:42
2

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$\Git\cmd 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$\Git\cmd"

Step 2: you just need to double-click

start-ssh-agent

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.

1

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)"
ssh-add
sudo SSH_AUTH_SOCK="$SSH_AUTH_SOCK" npm install git+ssh://git@PROJECT-URL -g && sudo npm link PROJECT-NAME
8
  • 1
    export and eval work just as well as root provided root's login shell is Bourne like. If root's shell is csh or tcsh, use eval `ssh-agent -c` Dec 8, 2015 at 22:50
  • Note that you can also give root access to your ssh-agent by doing sudo SSH_AUTH_SOCK="$SSH_AUTH_SOCK" npm install... Dec 8, 2015 at 22:51
  • Tried both also bash is bourne again shell and neither worked properly. all users on my debian jessie instance including root are set to use bash by default.
    – Belldandu
    Dec 8, 2015 at 23:51
  • What did you try? eval "$(ssh-agent)" at the prompt of a bash shell running as root will work. You'll obviously need to run ssh from that shell or its descendant so they inherit the SSH_AUTH_SOCK env var. eval "$(sudo ssh-agent)"; sudo ssh... won't work because sudo by default doesn't pass the SSH_AUTH_SOCK env var. Dec 9, 2015 at 9:50
  • i tried eval "$(ssh-agent)" ill be looking later to see if there is some sort of configuration error. Although the current way i have given this guide allows free control over which user you wish to be since your already root and can access any users id_rsa thats on the server. So once i get it working appropriately ill edit my answer.
    – Belldandu
    Dec 9, 2015 at 19:51
0

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 https://stackoverflow.com/a/48290333/11154841.

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.