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

基本上是这样的:

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连接到远程服务器的示例,该服务器使用指定的私钥,但这是一个本地命令。有可能吗?


当前回答

您可以尝试sshmulti-npm包来维护多个ssh密钥。

其他回答

如果您的路径上有要使用给定标识文件签名的目录,则可以通过设置ControlPath,通过.ssh/config文件指定使用特定标识文件,例如:

host github.com
  ControlPath ~/Projects/work/**
  HostName github.com
  IdentityFile ~/.ssh/id_work
  User git

然后,ssh将在给定的工作路径下执行git命令时使用指定的标识文件。

这样的方法应该有效(orip建议):

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

如果您更喜欢子壳,可以尝试以下方法(尽管它更脆弱):

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

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

或者,如果您愿意将仅包含.ssh目录的目录设置为HOME,那么设置HOME也可以达到目的;这可能包含identity.pub或配置文件设置IdentityFile。

如果SSH端口号不是22(默认值),请在~/.SSH/config中添加端口xx

在我的情况下(同义词),

Host my_synology
    Hostname xxxx.synology.me
    IdentityFile ~/.ssh/id_rsa_xxxx
    User myname
    Port xx

然后使用配置中的主机标题进行克隆。(“my_synology”。避免@chopstik的“*”)

git clone my_synology:path/to/repo.git

此命令克隆回购并配置SSH密钥以永久使用:

git clone -c "core.sshCommand=ssh -i ~/.ssh/<key>" git@github.com:<user>/<repo>.git

现在,如果运行git fetch、git pull或git push,它将使用core.shCommand中配置的SSH密钥(保存在.git/config中)。

最快、最简单的方法是:

使用ssh克隆您的repo:

git-c core.sshCommand=“ssh-i~/.ssh/<your_key>”克隆git@github.com:<user>/<repo>.git

然后cd到克隆的repo中,然后:

git-config-core.shCommand'ssh-i~/.ssh/<your_key>'

要测试它是否工作:

git--git-dir=/path/to/repo/.git pull

所以你可能会想:为什么我在github中安装了.pub,而private在默认目录中之后,我创建的ssh密钥就不起作用了?

该文档为我们提供了一个澄清问题的命令:ssh-vTgit@github.com

输出显示了git查找的ssh密钥列表。因此,您可能希望使用这些名称之一创建密钥,或者使用上述过程来包含所需的密钥。