如何用两个不同的ssh用户连接到GitLab? [英] How can I connect to GitLab with two different ssh users?

查看:261
本文介绍了如何用两个不同的ssh用户连接到GitLab?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have two GitLab accounts - one for my personal projects and one for work - and both of them have SSH keys generated and added. There is an id_rsa for the work account (because that's what I created first) and an id_rsa_personal for the personal one. My ~/ssh/config is as follows:

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时,得到以下信息:

Yet, when I run ssh -vvvT git@personalusername.gitlab.com, I get the following:

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.由于这是我的工作帐户,因此无法克隆我的个人存储库,从而导致以下错误:

The fourth line (Applying options for *) seems to indicate that ssh has ignored both of my config settings and picked up the defaults, which is to use id_rsa. Since that is my work account, I am unable to clone my personal repositories, resulting in the following error:

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与两个ssh键未连接(配置已更新)),但它涉及在Git配置中指定用户名.我的问题是我没有或想要一个全局Git配置,因为我在同一台机器上同时进行个人工作和工作.我更喜欢使用本地Git配置(位于./.git/config中),但是此文件夹尚不存在,因此我没有本地Git配置.

There is a similar question with an answer (gitlab with two ssh keys not connecting (config updated)), but that talks about specifying the username in Git config. My problem is that I do not have or want a global Git config as I am doing both personal as well as work work on the same machine. I prefer to have a local Git config (in ./.git/config), however, this folder doesn't exist yet, so I have no local Git config.

我正在运行Ubuntu 18.04.

I am running Ubuntu 18.04.

推荐答案

和往常一样,我在编写此问题后不久便自己解决了该问题.我用以下命令修改了~/ssh/config文件:

As usual, I fixed the issue myself moments after writing up this question. I modified my ~/ssh/config file with this:

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更改为使用电子邮件地址而不是用户名.之后,我能够成功克隆我的个人项目.

I quite simply changed the User personalusername to use the email address instead of the username. I was able to clone my personal projects successfully after this.

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

Also the ssh -vvvT git@personalusername.gitlab.com command was wrong. The correct one is just ssh -vvvT git@gitlab.com, the output of which had mentions of both keys. This is what led me to give the git clone command another shot, just to check.

这篇关于如何用两个不同的ssh用户连接到GitLab?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆