在 Windows 上的 Git 中在多个 ssh 密钥之间切换
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9672975/
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
Switching between multiple ssh keys in Git on Windows
提问by Tobias Ekblom
I need to have multiple keys in my client to access two different accounts on Repository hosting. See https://redefy.repositoryhosting.com/support-> How do I manage multiple accounts with multiple keypairs on my client?
我需要在我的客户端中有多个密钥才能访问存储库托管上的两个不同帐户。请参阅https://redefy.repositoryhosting.com/support-> 如何在我的客户端上管理具有多个密钥对的多个帐户?
I do not understand how to change between different ssh keys in Git on Windows, can anybody help me?
我不明白如何在 Windows 上的 Git 中的不同 ssh 密钥之间进行更改,有人可以帮助我吗?
回答by fajran
I assume you use git bash and openssh.
我假设您使用 git bash 和 openssh。
Like what it's written in the article, you can make a configuration file for ssh client that lists all of your accounts. You can write the following configuration in your own ssh client configuration file in ~/.ssh/config
就像文章中写的那样,您可以为 ssh 客户端制作一个配置文件,其中列出了您的所有帐户。可以在自己的ssh客户端配置文件中写如下配置~/.ssh/config
Host account-one
HostName server.example.com
User user-one
IdentityFile ~/.ssh/key-one
Host account-two
HostName server.example.com
User user-two
IdentityFile ~/.ssh/key-two
What it says is you define two, kind of, "host aliases" named account-one
and account-two
. If you use them, when making connection, the ssh client will use the corresponding HostName
, User
, and IdentityFile
for the server address, username, and ssh key file. With this you can use them to access your accounts and keys at even the same server.
它说的是您定义了两个名为account-one
和 的“主机别名” account-two
。如果使用它们,在进行连接时,ssh 客户端将使用相应的HostName
、User
、 和IdentityFile
作为服务器地址、用户名和 ssh 密钥文件。有了这个,您甚至可以使用它们在同一台服务器上访问您的帐户和密钥。
In git, you can define two remotes using them
在 git 中,您可以使用它们定义两个遥控器
$ git remote add one account-one:repository.git
$ git remote add two account-two:repository.git
then you can push to those remotes
然后你可以推送到那些遥控器
$ git push one master
$ git push two master
回答by mamapitufo
Which key is used for which server is handled by the SSH program that git is using to connect. In the default setup this should be the command line SSH client (openSSH?).
哪个密钥用于哪个服务器由 git 用于连接的 SSH 程序处理。在默认设置中,这应该是命令行 SSH 客户端(openSSH?)。
Using openSSH you can configure particular keyfiles for particular hosts in the ~/.ssh/config
file:
使用 openSSH,您可以为~/.ssh/config
文件中的特定主机配置特定的密钥文件:
Host foo.example.com
IdentityFile ~/.ssh/foo.example.com-id_rsa
Host bar.example.com
IdentityFile ~/.ssh/bar.example.com-id_rsa
Where ~/.ssh/*.example.com-id_rsa
are the private key files for each server.
~/.ssh/*.example.com-id_rsa
每个服务器的私钥文件在哪里。
I hope this helps.
我希望这有帮助。
回答by Daniel S.
On Windows you should try Pageant an SSH authentication agent for PuTTY, PSCP, PSFTP, and Plink. This tool can manage yout ssh keys and its pass-phrases. To use it together with Git you have to install Putty and link to the plink.exe setting the GIT_SSH variable.
在 Windows 上,您应该尝试 Pageant 一个适用于 PuTTY、PSCP、PSFTP 和 Plink 的 SSH 身份验证代理。该工具可以管理您的 ssh 密钥及其密码短语。要将它与 Git 一起使用,您必须安装 Putty 并链接到设置 GIT_SSH 变量的 plink.exe。
- Install Putty and friends (http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html)
Set GIT_SSH
set GIT_SSH=<path-to-plink.exe>
- Start Pageant and add you keys
- Run Git
- 安装 Putty 和朋友 (http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html)
设置 GIT_SSH
set GIT_SSH=<path-to-plink.exe>
- 启动 Pageant 并添加密钥
- 运行 Git
hth Daniel
丹尼尔
回答by sethcall
I'll answer this a little indirectly. I have previously used git bash and I've found that when I'm using git via the git bash shell, that it behaves just like a mac or linux bash shell. Meaning, when using git bash, that I can answer your question like:
我会间接回答这个问题。我以前使用过 git bash,我发现当我通过 git bash shell 使用 git 时,它的行为就像 mac 或 linux bash shell。意思是,在使用 git bash 时,我可以回答您的问题,例如:
'If you use git bash, you can manage multiple accounts just as you would if you were on linux or mac, using ssh-agent/ssh-add and friends'
“如果你使用 git bash,你可以像在 linux 或 mac 上一样管理多个帐户,使用 ssh-agent/ssh-add 和朋友”