GIT 选择要使用的私钥
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6688655/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
GIT select private key to use
提问by user349302
I have 2 Git servers that require 2 different SSH keys.
我有 2 个需要 2 个不同 SSH 密钥的 Git 服务器。
git clone user1@server1:blahblahblah
uses ~/.ssh/id_rsa
, but I need to specify which key to use depending on the server I am connecting to.
git clone user1@server1:blahblahblah
uses ~/.ssh/id_rsa
,但我需要根据我连接的服务器指定要使用的密钥。
What Git command-line parameter does this job? (I am running Linux)
什么 Git 命令行参数可以完成这项工作?(我正在运行 Linux)
采纳答案by Cameron Skinner
If you are connecting via SSH then the key will be controlled by an SSH parameter, not a git parameter.
如果您通过 SSH 连接,那么密钥将由 SSH 参数控制,而不是 git 参数。
SSH looks in the ~/.ssh/config
file for configuration parameters. Modify that file and add IdentityFile entries for the two Git servers like this:
SSH 在~/.ssh/config
文件中查找配置参数。修改该文件并为两个 Git 服务器添加 IdentityFile 条目,如下所示:
Host server1.whatever.com
IdentityFile /path/to/key_1
Host server2.whatever.com
IdentityFile /path/to/key_2
This articlehas some more details.
这篇文章有更多细节。
回答by Richard Smith
There is another possibility. That's to set core.sshCommand
, e.g.
还有另一种可能。那是设置core.sshCommand
,例如
git config --local core.sshCommand "/usr/bin/ssh -i /home/me/.ssh/id_rsa_foo"
There's one particular scenario when this strategy is particularly useful: that's when you have multiple accounts on Github, as all accounts ssh
to Github as [email protected]
and it uses the ssh
key to determine which Github user you are. In this case neither .ssh/config
nor ssh-agent
will do what you want.
这种策略在一种特殊情况下特别有用:那就是当您在 Github 上有多个帐户时,所有帐户都ssh
与 Github 相同,[email protected]
并且它使用ssh
密钥来确定您是哪个 Github 用户。在这种情况下,既.ssh/config
不会也ssh-agent
不会做你想做的。
Update— You cannot run the above until you have a local repository, so if you're trying to clone a remote repository, you'll need to specify the key manually as per drewbie18's answer:
更新— 在您拥有本地存储库之前,您无法运行上述内容,因此,如果您尝试克隆远程存储库,则需要按照 drawbie18 的回答手动指定密钥:
git clone -c core.sshCommand="/usr/bin/ssh -i /home/me/.ssh/id_rsa_foo" [email protected]:me/repo.git
Once you've cloned the repository you can use the git config
command to set this permanently.
克隆存储库后,您可以使用该git config
命令永久设置它。
回答by Zaz
Generally, you want to use ~/.ssh/config
for this. Simply pair server addresses with the keys you want to use for them as follows:
通常,您要为此使用~/.ssh/config
。只需将服务器地址与您要用于它们的密钥配对,如下所示:
Host github.com
IdentityFile ~/.ssh/id_rsa.github
Host heroku.com
IdentityFile ~/.ssh/id_rsa.heroku
Host *
IdentityFile ~/.ssh/id_rsa
Host *
denotes any server, so I use it to set ~/.ssh/id_rsa
as the default key to use.
Host *
表示任何服务器,所以我用它来设置~/.ssh/id_rsa
为要使用的默认密钥。
回答by Guss
In my scenario, similar to @Richard Smith scenario (whose solution, BTW, didn't work for me), I need to use different keys for the same server under different repositories.
在我的场景中,类似于@Richard Smith 场景(其解决方案,顺便说一句,对我不起作用),我需要对不同存储库下的同一服务器使用不同的密钥。
The workaround for me was to set up the session correctly with the environment variable GIT_SSH_COMMAND
, like so:
我的解决方法是使用环境变量正确设置会话GIT_SSH_COMMAND
,如下所示:
export GIT_SSH_COMMAND="ssh -o IdentitiesOnly=yes -i ~/.ssh/my-secret-identitiy"
Update:
更新:
Another thing to note here is that setting the environment variable correctly can be a hustle, so I'm using the command prompt modification facilities provided by things like Liquid Promptor Fish Shellto hook into the shell and keep updating the environment variables according to the current directory and some rules. For example, all my personal projects that need to my personal SSH key with Gitlab are under ~/Documents/Projects/personal
so when the shell hook runs pwd
and finds that the current directory is under that path, it automatically sets the GIT_SSH_COMMAND
variables as needed.
这里要注意的另一件事是,正确设置环境变量可能会很麻烦,所以我使用Liquid Prompt或Fish Shell等提供的命令提示符修改工具来挂钩 shell 并根据当前目录和一些规则。比如我的所有需要我的个人SSH密钥和Gitlab的个人项目都在,~/Documents/Projects/personal
所以当shell钩子运行pwd
并发现当前目录在那个路径下时,它会GIT_SSH_COMMAND
根据需要自动设置变量。
回答by khelll
Use ssh-add path-to-private-key
it works out of the box.
使用ssh-add path-to-private-key
它开箱即用。
回答by Maoz Zadok
You can set --global or --local configuration, global will update ~/.gitconfig
file, local will update configuration in the repository .git/config
and override the global (~/.gitconfig) configuration
您可以设置 --global 或 --local 配置,全局将更新~/.gitconfig
文件,本地将更新存储库中的配置.git/config
并覆盖全局(~/.gitconfig)配置
git config --local --add core.sshCommand 'ssh -i ~/.ssh/my_key'
git config --local --add core.sshCommand 'ssh -i ~/.ssh/my_key'
回答by drewbie18
Windows user here, I just ran into this issue and have a slightly different solution then I have read here so far. The problem I faced is that I simply wanted to clone a repo using a specific private ssh key and not have to globally configure my git config or add specific git bash settings, as I do my work in PowerShell. Essentially I just want to have some private keys sitting in my .ssh folder and I reference them in specific repos as required.
这里的 Windows 用户,我刚刚遇到了这个问题并且有一个稍微不同的解决方案,然后我在这里阅读到目前为止。我面临的问题是,我只是想使用特定的私有 ssh 密钥克隆一个存储库,而不必像在 PowerShell 中工作那样全局配置我的 git 配置或添加特定的 git bash 设置。本质上,我只想在我的 .ssh 文件夹中放置一些私钥,并根据需要在特定的存储库中引用它们。
The following command works for this:
以下命令适用于此:
git clone -c core.sshCommand="ssh -i ~/.ssh/<PRIVATE KEY NAME>" <CLONE URL>
Essentially what this does is upon the initialization of the git repo it sets the core.sshCommand option before running the clone. So the specific ssh key you wish to use for this repo is set ONLY for this repo. This may not be an ideal solution for all cases but for what I want it is.
本质上这是在 git repo 的初始化时它在运行克隆之前设置 core.sshCommand 选项。因此,您希望用于此存储库的特定 ssh 密钥仅为此存储库设置。这可能不是所有情况下的理想解决方案,但对于我想要的。
回答by Eike
The other answers inspired me to write a little script that chooses the ssh key depending on command line options or (if present) the values of git remote -v. Hope it helps!
其他答案启发我编写一个小脚本,根据命令行选项或(如果存在)git remote -v 的值来选择 ssh 密钥。希望能帮助到你!
To actually answer the question: Use gat.
实际回答这个问题:使用 gat。
See also https://bitbucket.org/eikerobert/gat/src/master/
另见https://bitbucket.org/eikerobert/gat/src/master/
#!/bin/bash
usegat=false
for VAR in "$@"
do
if [ "$VAR" != "${VAR/[email protected]:myaccount/}" ]; then
usegat=true
fi
done
if [ $usegat=false ]; then
/usr/bin/git rev-parse --is-inside-work-tree >/dev/null 2>&1
isinsidegitrepo=$?
#echo $isinsidegitrepo
if [ $isinsidegitrepo = 0 ]; then
remote=`/usr/bin/git remote -v`
if [ "$remote" != "${remote/[email protected]:myaccount/}" ]; then
usegat=true
fi
fi
fi
if [ $usegat = true ]; then
# echo "TRUE"
/usr/bin/git -c core.sshCommand="/usr/bin/ssh -i /home/myaccount/.ssh/mykey" "$@"
else
#echo "FALSE"
/usr/bin/git "$@"
fi