如何用两个不同的ssh用户连接到GitLab?

时间:2018-11-27 07:04:03

标签: git ssh gitlab

我有两个GitLab帐户-一个用于我的个人项目,一个用于工作-两个都具有生成和添加的SSH密钥。工作帐户有一个id_rsa(因为这是我首先创建的),个人帐户有一个id_rsa_personal。我的~/ssh/config如下:

Host gitlab-personal
    Preferredauthentications publickey
    User personalusername
    HostName gitlab.com
    IdentityFile ~/.ssh/id_rsa_personal
Host gitlab-work
    Preferredauthentications publickey
    User workusername
    HostName gitlab.com
    IdentityFile ~/.ssh/id_rsa

但是,当我运行ssh -vvvT git@personalusername.gitlab.com时,我得到以下信息:

OpenSSH_7.6p1 Ubuntu-4, OpenSSL 1.0.2n  7 Dec 2017
debug1: Reading configuration data /home/personalusername/.ssh/config
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug2: resolving "personalusername.gitlab.com" port 22
ssh: Could not resolve hostname personalusername.gitlab.com: Name or service not known

第四行(Applying options for *)似乎表明ssh忽略了我的两个配置设置,并选择了默认设置,即使用id_rsa。由于这是我的工作帐户,因此无法克隆我的个人存储库,从而导致以下错误:

git clone git@gitlab.com:personalusername/personalproject.git personalprojecttest
Cloning into 'personalprojecttest'...
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.

还有一个类似的问题,答案为(gitlab with two ssh keys not connecting (config updated)),但它涉及在Git配置中指定用户名。我的问题是,我在个人计算机上或在同一台计算机上进行工作时,没有或想要全局Git配置。我更喜欢使用本地Git配置(位于./.git/config中),但是此文件夹尚不存在,因此我没有本地Git配置。

我正在运行Ubuntu 18.04。

3 个答案:

答案 0 :(得分:1)

〜/ .ssh / config:

Host gitlab-personal
    User git
    HostName gitlab.com
    IdentityFile ~/.ssh/id_rsa_personal
    Preferredauthentications publickey

Host gitlab-work
    User git
    HostName gitlab.com
    IdentityFile ~/.ssh/id_rsa
    Preferredauthentications publickey

然后尝试ssh -vvvT gitlab-personalssh -vvvT gitlab-work

运行

git clone gitlab-personal:personalusername/personalproject.git

答案 1 :(得分:-1)

您尝试过ssh-add ~/.ssh/id_rsa_personal吗?展位密钥应添加到ssh-agent。

答案 2 :(得分:-1)

和往常一样,我在写完这个问题后马上就解决了这个问题。我用以下命令修改了~/ssh/config文件:

Host gitlab.com
    Preferredauthentications publickey
    User personalusername@personalemailaddress.com
    HostName gitlab.com
    IdentityFile ~/.ssh/id_rsa_personal
Host gitlab.com
    Preferredauthentications publickey
    User workusername@workemailaddress.com
    HostName gitlab.com
    IdentityFile ~/.ssh/id_rsa

我很简单地将User personalusername更改为使用电子邮件地址代替用户名。之后,我能够成功克隆我的个人项目。

ssh -vvvT git@personalusername.gitlab.com命令也出错。正确的只是ssh -vvvT git@gitlab.com,其输出中提到了两个键。这就是导致我再次给git clone命令发来,只是为了检查的原因。