3

I have created file ssh-start.sh which contains:

eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_rsa

I'm running it and have output:

Agent pid 1234
Identity added: ~/.ssh/id_rsa (~/.ssh/id_rsa)

but when I run

ssh-add -l

I'm getting:

Could not open a connection to your authentication agent.

What am I doing wrong and how to fix that?

(when I run commands from ssh-start.sh file directly in terminal it works fine)

2
  • Is the last command i.e., ssh-add -l part of the script or you're running it after the script runs? Commented Jul 26, 2014 at 14:57
  • I'm running it after script runs. If I add it to script it show my identity, but run 5 seconds later from shell doesn't. Commented Jul 26, 2014 at 15:02

1 Answer 1

5

ssh command needs to know how to talk to the ssh-agent. They know that from the SSH_AUTH_SOCK environment variable.

[gc@slave4 ~]$ ssh-agent -s
SSH_AUTH_SOCK=/tmp/ssh-ln4RuPajkE2A/agent.11091; export SSH_AUTH_SOCK;
SSH_AGENT_PID=11092; export SSH_AGENT_PID;
echo Agent pid 11092;

If you run ssh-add -l after the script, neither ssh nor ssh-add will be able to see that the SSH_AUTH_SOCK environment variable is set. So run all commands as part of a script or all from the command prompt.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for thank hint, that pointed me to right direction, now I know I need to run my script using "source" command: "source ./ssh-start.sh" (or use "." instead of "source" to make it shorter).
Yes, running in the same shell is the key whether you source it or keep everything in the script. I'm glad it helped. :)
@Technext Spent half a day solving this issue) Thanks a lot!
I'm glad it helped you. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.