git 多个 ssh 密钥不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32212593/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 11:18:32  来源:igfitidea点击:

Multiple ssh keys are not working

gitsshgitlabopensshssh-agent

提问by Yogesh Yadav

OS Description: Ubuntu 14.04.3 LTS
ssh: OpenSSH_6.6.1p1 Ubuntu-2ubuntu2.3, OpenSSL 1.0.1f 6 Jan 2014

I am unable to use multiple ssh key for gitlab. The error I am getting is

我无法为 gitlab 使用多个 ssh 密钥。我得到的错误是

GitLab: The project you were looking for could not be found.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists. 

I am pretty much sure that I did everything right This is my config file

我很确定我做的一切都是正确的 这是我的配置文件

Host work gitlab.com
    Hostname gitlab.com
    IdentityFile ~/.ssh/ida_rsa
    User git

Host integrate gitlab.com
    Hostname gitlab.com
    IdentityFile ~/.ssh/ida_rsa_personal
    User git

The keys are added automatically in ssh-agent, but to make sure I also added the key manually

密钥是在 ssh-agent 中自动添加的,但为了确保我也手动添加了密钥

$ ssh-add -l
2048 e7:08:d6:8c:00:28:31:f9:3f:21:4a:0f:4e:1e:ee:20 id_rsa (RSA)
2048 ff:22:f6:90:2b:7c:9f:ed:45:41:df:79:06:de:fe:81 id_rsa_personal (RSA)

My ssh-agent is also running

我的 ssh-agent 也在运行

$ ssh-agent
SSH_AUTH_SOCK=/tmp/ssh-uTC6tA5HMt1x/agent.4899; export SSH_AUTH_SOCK;
SSH_AGENT_PID=4900; export SSH_AGENT_PID;
echo Agent pid 4900;

Permissions of ssh key

ssh 密钥的权限

-rw-------  1 yogi yogi  1679 jun 25 15:38 id_rsa
-rw-------  1 yogi yogi  1679 ago 25 10:53 id_rsa_personal
-rw-r--r--  1 yogi yogi   404 ago 25 10:53 id_rsa_personal.pub
-rw-r--r--  1 yogi yogi   391 jun 25 15:41 id_rsa.pub

I added my ssh public key on gitlab.

我在 gitlab 上添加了我的 ssh 公钥。

Could any one please tell me what I am missing ?

谁能告诉我我错过了什么?

Note :- I noticed that out of my two ssh keys, the ssh key which is on top( id_rsa - e7:08....) works fine.

注意:- 我注意到在我的两个 ssh 密钥中,位于顶部的 ssh 密钥(id_rsa - e7:08....)工作正常。

$ ssh-add -l
**2048 e7:08:d6:8c:00:28:31:f9:3f:21:4a:0f:4e:1e:ee:20 id_rsa (RSA)**
2048 ff:22:f6:90:2b:7c:9f:ed:45:41:df:79:06:de:fe:81 id_rsa_personal (RSA)

If I add id_rsa_personal(ff:22..) after id_rsa, than id_rsa_personal starts working fine and id_rsa stops working.

如果我在 id_rsa 之后添加 id_rsa_personal(ff:22..),那么 id_rsa_personal 开始工作正常而 id_rsa 停止工作。

$ ssh-add -l
**2048 ff:22:f6:90:2b:7c:9f:ed:45:41:df:79:06:de:fe:81 id_rsa_personal (RSA)**
2048 e7:08:d6:8c:00:28:31:f9:3f:21:4a:0f:4e:1e:ee:20 id_rsa (RSA)

I think , ssh-agent is unable to iterate through keys. It only reads first key and stops. Is this a bug in openssh ? If yes, any workaround ?

我认为,ssh-agent 无法遍历密钥。它只读取第一个键并停止。这是 openssh 中的错误吗?如果是,任何解决方法?

Thanks

谢谢

回答by Zangetsu

You need to have ~/.ssh/configfile

你需要有~/.ssh/config文件

Inside file

内部文件

# Default GitHub user
Host github.com
    HostName github.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/personalid

# Work user account
Host bitbucket.org
    HostName bitbucket.org
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/workid

Details can be found in the link below https://confluence.atlassian.com/bitbucket/configure-multiple-ssh-identities-for-gitbash-mac-osx-linux-271943168.html

详细信息可以在以下链接中找到 https://confluence.atlassian.com/bitbucket/configure-multiple-ssh-identities-for-gitbash-mac-osx-linux-271943168.html

If the domain is same, use different Host

如果域相同,请使用不同的主机

Host work.gitlab.com
    HostName gitlab.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_rsa.work

Host home.gitlab.com
    HostName gitlab.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_rsa.home

Clear the cache

清除缓存

ssh-add -D

Add all ssh keys

添加所有 ssh 密钥

ssh-add ~/.ssh/id_rsa.work
ssh-add ~/.ssh/id_rsa.home

Also set your repos' url in (.git/config) accordingly as given:

还按照给定的方式在 (.git/config) 中相应地设置您的 repos 的 url:

url = [email protected]:xxxxxxxxxx.git
url = [email protected]:yyyyyyyyy.git 

If your ssh config file is as what I suggested.

如果您的 ssh 配置文件符合我的建议。