Running ssh-agent in bash script

2019-08-11 07:06发布

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)

标签: bash ssh
1条回答
神经病院院长
2楼-- · 2019-08-11 07:52

ssh commands need 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 it 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 from the script or all from the command prompt.

查看更多
登录 后发表回答