小编典典

在 Git 上执行 shell 命令时如何指定要使用的私有 SSH 密钥?

git

可能是一种相当不寻常的情况,但我想指定一个私有 SSH 密钥,以便在git从本地计算机执行 shell ( ) 命令时使用。

基本上是这样的:

git clone git@github.com:TheUser/TheProject.git -key "/home/christoffer/ssh_keys/theuser"

甚至更好(在 Ruby 中):

with_key("/home/christoffer/ssh_keys/theuser") do
  sh("git clone git@github.com:TheUser/TheProject.git")
end

Net::SSH我已经看到使用指定私钥连接到远程服务器的示例,但这是一个本地命令。可能吗?


阅读 212

收藏
2022-02-23

共1个答案

小编典典

这些解决方案都不适合我。

相反,我详细说明了@Martin v. Löwis 提到config为 SSH 设置文件。

SSH 将查找用户的~/.ssh/config文件。我的设置为:

Host gitserv
    Hostname remote.server.com
    IdentityFile ~/.ssh/id_rsa.github
    IdentitiesOnly yes # see NOTES below

我添加了一个远程 git 存储库:

git remote add origin git@gitserv:myrepo.git

然后 git 命令对我来说正常工作。

git push -v origin master

笔记

  • IdentitiesOnly yes防止 SSH 默认行为发送与每个协议的默认文件名匹配的身份文件所必需的。如果您有一个名为的文件,在没有此选项的情况下~/.ssh/id_rsa将在您之前尝试。~/.ssh/id_rsa.github
2022-02-23