22

I type in ssh-agent and get the following back:

SSH_AUTH_SOCK=/tmp/ssh-GqdeT074HLRJ/agent.4670; export SSH_AUTH_SOCK;
SSH_AGENT_ID=4671; export SSH_AGENT_PID;
echo Agent pid 4671;

I then type in ssh-add privkey.pem and get the following:

Could not open a connection to your authentication agent

Any ideas as to what I'm doing wrong? I tried adding sudo before each of those commands, as well, but got near identical output.

Giacomo1968
  • 53,069
  • 19
  • 162
  • 212
neubert
  • 6,863
  • 35
  • 76
  • 138

3 Answers3

30

You’re supposed to execute the output that you get back from ssh-agent, like this: eval $(ssh-agent)

Then your environment is set up for ssh-add to connect to the agent.

Leon Weber
  • 463
  • 4
  • 7
  • I tried `sudo eval $(ssh-agent)` and it said `sudo: eval: command not found`. I tried `eval $(sudo ssh-agent)` and it said `Agent pid 4735` and then, after both of those, `ssh-add` still doesn't work. Any ideas? – neubert Jan 28 '14 at 05:27
  • 1
    never mind - doing `sudo bash` and then doing all that got the job done. thanks! – neubert Jan 28 '14 at 05:42
  • 1
    "eval `ssh-agent`" also works. I didn't need any sudo. – labyrinth Mar 12 '14 at 17:08
  • doing eval and then ssh-add still did not work for me, any ideas why? – user3226932 Apr 28 '17 at 08:26
3

You may also use the following syntax:

ssh-agent sh -c 'ssh-add && echo Do some stuff here.'
kenorb
  • 24,736
  • 27
  • 129
  • 199
-1

I found solution:

run below command OR add to .bashrc file

exec ssh-agent bash

ssh-add [key path]

  • 1
    (1) Two solutions have already been posted, and one has been accepted as correct.  Does yours have any advantage over those earlier ones?  Please do not respond in comments; [edit] your answer to make it clearer and more complete. (2) Do you really believe that it’s a good idea to `exec bash` from within `.bashrc`?  Have you tried it?  Before you do, think hard about what will happen.  (Hint: what does a new bash process do when it starts?)  If you do dare to try it, you might want to make sure that you have another shell window open. – Scott - Слава Україні Feb 15 '17 at 08:19