代码之家  ›  专栏  ›  技术社区  ›  aalaap

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

  •  0
  • aalaap  · 技术社区  · 6 年前

    我有两个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)

    我正在运行Ubuntu 18.04。

    3 回复  |  直到 6 年前
        1
  •  3
  •   phd    6 年前

    ~/.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-personal ssh -vvvT gitlab-work .

    git clone gitlab-personal:personalusername/personalproject.git
    
        2
  •  -1
  •   Dzintars    6 年前

    ssh-add ~/.ssh/id_rsa_personal ? 应将Booth密钥添加到ssh代理。

        3
  •  -1
  •   aalaap    6 年前

    ~/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 命令再开枪,只是为了检查。