【问题标题】:Multiple ssh keys are not working多个 ssh 密钥不起作用
【发布时间】:2015-11-19 15:57:12
【问题描述】:
OS Description: Ubuntu 14.04.3 LTS
ssh: OpenSSH_6.6.1p1 Ubuntu-2ubuntu2.3, OpenSSL 1.0.1f 6 Jan 2014

我无法为 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. 

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

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

密钥是在 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)

我的 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;

ssh key的权限

-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

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

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

注意:- 我注意到在我的两个 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)

如果我在 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)

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

谢谢

【问题讨论】:

  • 您尝试连接到 gitlab 的命令是什么?您是通过代理还是通过配置提供密钥?如果您只想使用特定身份,请确保还指定 IdentitiesOnly 选项
  • 我正在尝试使用 git clone、git pull、git push。
  • ssh-agent 不采用第一个,我的正确 ssh-key 在顶部也遇到了同样的问题。
  • Host integrate gitlab.com 楼主可以有空格吗?你用什么名字来推/拉?
  • @Holloway 不,Host 的参数是主机名列表,以空格分隔。我猜第二个gitlab.com 会覆盖第一个,或者反过来。

标签: git ssh gitlab openssh ssh-agent


【解决方案1】:

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

内部文件

# 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

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

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

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

清除缓存

ssh-add -D

添加所有 ssh 密钥

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

同时在 (.git/config) 中相应地设置你的 repos 的 url:

url = git@work.gitlab.com:xxxxxxxxxx.git
url = git@home.gitlab.com:yyyyyyyyy.git 

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

【讨论】:

  • 我已经有了 ~/.ssh/config 文件,正如我在问题中提到的那样。很可能是这个特定版本的 Ubuntu 或 ssh 的一些错误。我在 Windows 和其他基于 linux 的操作系统中使用多个 ssh 密钥没有任何问题。
  • 我已经编辑了我的答案。请看一看。使用不同的主机对我有用。只需在再次添加密钥时清除缓存即可。
  • 使用不同的主机不起作用。即使在使用 ssh-add -D 清除缓存之后,如果我执行 ssh-add -l ,我也可以看到我的密钥。所以可能清除缓存不起作用。
  • 也许你需要在 ".git/config" 中为不同的 repos 设置 url = git@work.gitlab.com:xxxxxxxxxx.git 和 url = git@home.gitlab.com:yyyyyyyyy .git 如果你的 ssh 配置文件符合我的建议。
  • 只是补充一点,当您设置自定义 Host work.gitlab.com 时,ss-add 不是必需的
【解决方案2】:

添加这些行会有所帮助
我遇到了同样的问题https://i.imgur.com/3cj3EWH.png

【讨论】:

    猜你喜欢
    • 2014-09-11
    • 1970-01-01
    • 2018-03-17
    • 1970-01-01
    • 1970-01-01
    • 2019-01-10
    • 2015-08-13
    • 1970-01-01
    • 2019-01-19
    相关资源
    最近更新 更多