How can I connect to GitLab with two different ssh users?

隐身守侯 提交于 2019-12-13 05:24:44

问题


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

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

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.

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.

I am running Ubuntu 18.04.


回答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

Then try ssh -vvvT gitlab-personal, ssh -vvvT gitlab-work.

Run

git clone gitlab-personal:personalusername/personalproject.git



回答2:


Did you tried ssh-add ~/.ssh/id_rsa_personal? Booth keys should be added to ssh-agent.




回答3:


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

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.

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.



来源:https://stackoverflow.com/questions/53494350/how-can-i-connect-to-gitlab-with-two-different-ssh-users

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!