使用的一种可能性~/.ssh/config
是使用Match
限制而不是Host
限制。特别是Match Exec
调用 shell 命令来决定是否应用声明。在 bash 中,您可以使用以下命令:
[ git@git.company.com:gitolite-admin = $(git config --get remote.origin.url)'' ]
这使用 bash[
命令来验证两个字符串是否相等。在这种情况下,它正在测试字符串是否git@git.company.com:gitolite-admin
与从$(git config --get remote.origin.url)''
命令获得的输出匹配。
您可以使用任何其他命令来标识 shell 所在的存储库。为此,在我的情况下,将变量定义到您的 shell 中很重要。完整的示例如下:$SHELL
/bin/bash
~/.ssh/config
Match Exec "[ git@git.company.com:gitolite-admin = $(git config --get remote.origin.url)'' ]"
IdentityFile ~/.ssh/gitolite-admin
IdentitiesOnly yes
ForwardAgent no
ForwardX11 no
ForwardX11Trusted no
Match Exec "[ git@git.company.com:some_repo = $(git config --get remote.origin.url)'' ]"
IdentityFile ~/.ssh/yourOwnPrivateKey
IdentitiesOnly yes
ForwardAgent no
ForwardX11 no
ForwardX11Trusted no
在此示例中,我假设~/.ssh/yourOwnPrivateKey
包含您自己的私钥并且~/.ssh/gitolite-admin
包含用户的私钥gitolite-admin
。我包含了IdentitiesOnly yes
声明,以确保只向 git 服务器提供一个密钥,正如 Mark Longair 所提到的。其他声明只是 git 的标准 ssh 选项。
如果您有多个some_repo
要与不同键一起使用的配置,则可以添加此配置。如果您有多个存储库git@git.company.com
并且其中大多数都使用~/.ssh/yourOwnPrivateKey
它,那么将此密钥作为主机的默认值包含在内更有意义。在这种情况下,~/.ssh/config
将是:
Match Exec "[ git@git.company.com:gitolite-admin = $(git config --get remote.origin.url)'' ]"
IdentityFile ~/.ssh/gitolite-admin
IdentitiesOnly yes
Host git.company.com
IdentityFile ~/.ssh/yourOwnPrivateKey
IdentitiesOnly yes
ForwardAgent no
ForwardX11 no
ForwardX11Trusted no
请注意,顺序很重要,Host git.company.com
限制应出现在Match Exec
一个或一个之后。