我有 2 个 Git 服务器需要 2 个不同的 SSH key 。
git clone user1@server1:blahblahblah
使用 ~/.ssh/id_rsa
,但我需要根据要连接的服务器指定要使用的 key 。
这个工作的 Git 命令行参数是什么? (我正在运行 Linux。)
最佳答案
还有一种可能。即设置core.sshCommand
,例如
git config --local core.sshCommand "/usr/bin/ssh -i /home/me/.ssh/id_rsa_foo"
在这种策略特别有用的特定场景下:当您在 Github 上有多个帐户时,因为所有帐户都以 ssh
到 Github 为 git@github.com
并且它使用 ssh
key 来确定您是哪个 Github 用户。在这种情况下,.ssh/config
和 ssh-agent
都不会执行您想要的操作。
更新 — 在拥有本地存储库之前无法运行上述操作,因此如果您尝试克隆远程存储库,则需要根据 drewbie18 的回答手动指定 key :
git clone -c core.sshCommand="/usr/bin/ssh -i /home/me/.ssh/id_rsa_foo" git@github.com:me/repo.git
克隆存储库后,您可以使用 git config
命令永久设置它。
关于git - 选择要与 Git 一起使用的私钥,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6688655/