这可能是一种非常不寻常的情况,但我想指定在本地计算机上执行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连接到远程服务器的示例,该服务器使用指定的私钥,但这是一个本地命令。有可能吗?
如果这里的其他解决方案都不适合您,并且您已经创建了多个ssh密钥,但仍然无法执行以下简单操作
git pull
那么假设您有两个ssh密钥文件
id_rsa
id_rsa_other_key
然后在git repo中,尝试:
# Run these commands INSIDE your git directory
eval `ssh-agent -s`
ssh-add ~/.ssh/id_rsa
ssh-add ~/.ssh/id_rsa_other_key
并通过以下方式确保github默认用户名和userid正确:
# Run these commands INSIDE your git directory
git config user.name "Mona Lisa"
git config user.email "mona.lisa@email.com"
看见https://gist.github.com/jexchan/2351996了解更多信息。
这些解决方案都不适合我。
相反,我详细介绍了@Martin v.Löwis提到的为SSH设置配置文件。
SSH将查找用户的~/.SSH/config文件。我的设置如下:
Host gitserv
Hostname remote.server.com
IdentityFile ~/.ssh/id_rsa.github
IdentitiesOnly yes # see NOTES below
我添加了一个远程git存储库:
git remote add origin git@gitserv:myrepo.git
然后git命令对我来说正常工作。
git push -v origin master
笔记
IdentitesOnly yes是必需的,以防止SSH默认行为发送与每个协议的默认文件名匹配的标识文件。如果您有一个名为~/.ssh/id_rsa的文件,则在不使用此选项的情况下,将在~/.sss/id_rsa.gitub之前尝试该文件。
工具书类
在一个客户端上使用多个SSH私钥的最佳方法我怎样才能阻止ssh提供错误的密钥