指定执行shell命令时使用的私有SSH密钥? [英] Specify private SSH-key to use when executing shell command?

查看:108
本文介绍了指定执行shell命令时使用的私有SSH密钥?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许是一种非常不寻常的情况,但我想在本地计算机上执行shell(git)命令时指定一个私有SSH密钥。



基本上像这样:

  git clone git@github.com:TheUser / TheProject.git -key/ home / christoffer / ssh_keys /用户

甚至更好(在Ruby中):

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

我见过使用Net :: SSH连接远程服务器的例子,一个指定的私钥,但这是一个本地命令。是否可能?

解决方案

类似这样的东西应该可以工作(orip):

  ssh-agent bash -c'ssh-add / somewhere / yourkey; git clone git@github.com:user / project.git'

如果你喜欢subhell,你可以尝试以下操作(虽然它更脆弱):

pre $ s $ s $ ssh-agent $(ssh-add / somewhere / yourkey; git clone git@github.com:user / project.git)

Git会调用SSH来寻找其代理由环境变量;反过来,这将会加载密钥。



另外,设置 HOME 也可以做到这一点,你愿意设置一个只包含 .ssh 目录的目录作为 HOME ;这可能包含identity.pub或设置IdentityFile的配置文件


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.

Basically like this:

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

Or even better (in Ruby):

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

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?

解决方案

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 will invoke SSH which will find its agent by environment variable; this will, in turn, have the key loaded.

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.

这篇关于指定执行shell命令时使用的私有SSH密钥?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆