cron git push 与 ssh 密钥

xfx*_*xfx 7 git ssh cron

我为github账号设置了ssh key,所以不用每次都输入密码,效果很好。这是我使用的脚本:

#!/bin/bash
git push origin master
Run Code Online (Sandbox Code Playgroud)

但是当我使用 cron 运行它时,它不会使用我的 ssh 密钥。这是输出:

Permission denied (publickey)
fatal: The remote end hung up unexpectedly
Run Code Online (Sandbox Code Playgroud)

我四处搜索并找到了一些文章,但没有一篇能解决我的问题。以下是我找到的文章(以及更多):

https://askubuntu.com/questions/110565/why-is-git-not-using-my-pubkey-with-crontab

https://unix.stackexchange.com/questions/35466/how-to-perform-git-push-using-crontab

通过 cron git push

任何人都可以给我一步一步的说明来解决这个问题吗?

Sto*_*ica 5

根据您的评论,我真的不明白为什么您的脚本在 cron 中不起作用。我们可以尝试一些事情来解决问题。

~/.ssh/config为运行 cron 作业的用户添加配置,如下所示:

Host github-project1
User git
HostName github.com
IdentityFile /path/to/github.project1.key
#or like this:
#IdentityFile ~/.ssh/github.project1.key
Run Code Online (Sandbox Code Playgroud)

然后在你的 git 工作树中添加一个新的遥控器,如下所示:

git remote add github-project1 github-project1:USER/PROJECT.git
Run Code Online (Sandbox Code Playgroud)

在 url 中,github-project1:user/project.git仅将USER和更改PROJECT为适合您的项目的正确值,但保持原样:它必须与我们刚刚添加的配置中的设置github-project1值匹配。Hostssh

最后,更改脚本以使用这个新的遥控器:

#!/bin/bash
git push github-project1 master
Run Code Online (Sandbox Code Playgroud)

首先在 shell 中测试脚本,然后在 cron 中测试。


Von*_*onC 4

正如您的一个线程中提到的,您需要将执行 cron 脚本的 root 用户指向右侧HOME(包含$HOME/.ssh/id_rsa(.pub)您的公钥和私钥的用户)。

#!/bin/bash
HOME=/home/yourAccount git push origin master
Run Code Online (Sandbox Code Playgroud)

如果这不起作用,请开始调试 ssh 命令

#!/bin/bash
HOME=/home/yourAccount ssh -Tvvv yourGitServer
Run Code Online (Sandbox Code Playgroud)

并首先使用简单的私钥(不受密码保护)进行检查。
然后,如果您需要密码,请确保您的ssh 代理正在运行,以便缓存所述密码(或使用钥匙串,如我之前提到的)。

根据您的日志,建议使用公共 ssh 密钥,但被拒绝。

debug1: Trying private key: /home/jack/.ssh/id_rsa
debug3: no such identity: /home/jack/.ssh/id_rsa
Run Code Online (Sandbox Code Playgroud)

仔细检查“ BitBucket Set up SSH for Git ”,并确保您的id_rsaid_rsa.pub都在那里,并受到正确的保护

另请检查您id_rsa.pub是否已添加到您的 BitBucket 帐户(作为一行)。

https://confluence.atlassian.com/download/attachments/304578655/ssh_key.png?version=2&modificationDate=1379979345091&api=v2&effects=drop-shadow