在Git上执行shell命令时,如何指定专用SSH密钥? [英] How to specify the private SSH-key to use when executing shell command on Git?
问题描述
也许是一种非常不寻常的情况,但是我想指定一个从本地计算机执行shell(git)命令时要使用的专用SSH密钥.
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'
如果您喜欢子壳,可以尝试以下操作(尽管它更脆弱):
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,它将通过环境变量查找其代理;这将依次加载密钥.
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屋!