这可能是一种非常不寻常的情况,但我想指定在本地计算机上执行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连接到远程服务器的示例,该服务器使用指定的私钥,但这是一个本地命令。有可能吗?
总结一下答案和评论,设置git以使用不同的密钥文件,然后忘记它的最佳方法是编辑~/.ssh/config(或c:\users\<youruser>\.ssh\config)并指定多个身份,这也支持同一主机的不同用户(例如,个人GitHub帐户和工作用户),这也适用于Windows:
Host github.com
HostName github.com
IdentityFile /path/to/your/personal/github/private/key
User dandv
Host github-work
HostName github.com
IdentityFile /path/to/your/work/github/private/key
User workuser
然后,要将项目克隆为您的个人用户,只需运行常规gitclone命令即可。
要将repo克隆为工作用户,请运行gitclonegit@github-work:公司/项目.git。
您需要创建一个~/.ssh/config,如下所示
Host <Your bitbucket server>
User <userid>
Hostname <Your bitbucket server as above>
IdentitiesOnly yes
IdentityFile ~/.ssh/id_rsa<file> This is your private key file
许可如下
-rw------- $HOME/.ssh/config
将公钥添加到git中(cat~/.ssh/id_rsa_b[或类似名称])
然后是git clone,如下所示
git clone ssh://blahblah@blah.com/userid/test.git
最快、最简单的方法是:
使用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密钥列表。因此,您可能希望使用这些名称之一创建密钥,或者使用上述过程来包含所需的密钥。