在 Git 上执行 shell 命令时如何指定要使用的私有 SSH 密钥? [英] How to specify the private SSH-key to use when executing shell command on Git?
问题描述
可能是一个相当不寻常的情况,但我想指定一个私有 SSH 密钥,以便在从本地计算机执行 shell (git
) 命令时使用.
A rather unusual situation perhaps, but I want to specify a private SSH-key to use when executing a shell (git
) command from the local computer.
基本上是这样的:
git clone git@github.com:TheUser/TheProject.git -key "/home/christoffer/ssh_keys/theuser"
甚至更好(在 Ruby 中):
Or even better (in Ruby):
with_key("/home/christoffer/ssh_keys/theuser") do
sh("git clone git@github.com:TheUser/TheProject.git")
end
我见过使用指定私钥的 Net::SSH
连接到远程服务器的示例,但这是一个本地命令.可能吗?
I have seen examples of connecting to a remote server with Net::SSH
that uses a specified private key, but this is a local command. Is it possible?
推荐答案
这样的事情应该可行(orip 建议):
Something like this should work (suggested by orip):
ssh-agent bash -c 'ssh-add /somewhere/yourkey; git clone git@github.com:user/project.git'
如果你更喜欢子shell,你可以尝试以下(虽然它更脆弱):
if you prefer subshells, you could try the following (though it is more fragile):
ssh-agent $(ssh-add /somewhere/yourkey; git clone git@github.com:user/project.git)
Git 会调用 SSH,SSH 会通过环境变量找到它的代理;反过来,这将加载密钥.
Git will invoke SSH which will find its agent by environment variable; this will, in turn, have the key loaded.
或者,设置 HOME
也可以解决问题,前提是您愿意将一个仅包含 .ssh
目录的目录设置为 HOME
>;这可能包含一个 identity.pub,或一个 配置文件 设置 IdentityFile.
Alternatively, setting HOME
may also do the trick, provided you are willing to setup a directory that contains only a .ssh
directory as HOME
; this may either contain an identity.pub, or a config file setting IdentityFile.
这篇关于在 Git 上执行 shell 命令时如何指定要使用的私有 SSH 密钥?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!