kuba
kuba

Reputation: 3870

Running ssh-agent in bash script

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)

Upvotes: 3

Views: 5931

Answers (1)

Technext
Technext

Reputation: 8107

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.

Upvotes: 5

Related Questions