通过 shell 脚本克隆 git repo 时权限被拒绝

时间:2021-03-02 20:03:43

标签: linux bash git shell

我从服务器 (A) 启动一个名为 test.sh 的脚本,其中包含以下代码:

#!/bin/bash

ssh login@server-b.com -p 22 'bash $HOME/tmp/git.sh'

exit 0

因此从服务器 (B) 启动另一个脚本(称为 git.sh)并包含:

#!/bin/bash

cd $HOME/tmp
git clone ssh://repo_login@my-repo.com:22/home/scripts

exit 0

但是 git clone 不起作用,我收到此错误消息:

Cloning into 'scripts'...
Permission denied (publickey,gssapi-keyex,gssapi-with-mic,keyboard-interactive).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

但是从服务器 (B) 如果我手动启动 git.sh 脚本,它可以工作。

你知道为什么吗?

谢谢 L.

1 个答案:

答案 0 :(得分:0)

通过 ssh 运行脚本时,您需要启动 ssh-agent,并添加您的密钥,然后才能连接到另一台机器。

脚本 git.sh 应修改如下(假设您的密钥在文件 ~/.ssh/id_rsa 中):

#!/bin/bash

cd $HOME/tmp
eval $(ssh-agent)
ssh-add ~/.ssh/id_rsa
git clone ssh://repo_login@my-repo.com:22/home/scripts
exit 0
相关问题