在bash脚本中运行ssh-agent [英] Running ssh-agent in bash script
问题描述
我创建了ssh-start.sh文件,其中包含:
I have created file ssh-start.sh which contains:
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_rsa
我正在运行它并输出:
Agent pid 1234
Identity added: ~/.ssh/id_rsa (~/.ssh/id_rsa)
但是我跑步时
ssh-add -l
我得到了:
Could not open a connection to your authentication agent.
我在做什么错以及如何解决?
What am I doing wrong and how to fix that?
(当我直接在终端中运行ssh-start.sh文件中的命令时,它工作正常)
(when I run commands from ssh-start.sh file directly in terminal it works fine)
推荐答案
ssh
命令需要知道如何与 ssh-agent
对话.他们从 SSH_AUTH_SOCK
环境变量知道这一点.
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;
如果在脚本之后运行 ssh-add -l </code> ,则
ssh
和 ssh-add
都不会能够看到已设置 SSH_AUTH_SOCK
环境变量.因此,将所有命令作为脚本的一部分运行,或者在命令提示符下运行所有命令.
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.
这篇关于在bash脚本中运行ssh-agent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!