SSH config with multiple keys for multiple gitlab user accounts

时光毁灭记忆、已成空白 提交于 2020-06-27 21:00:29

问题


I run Gitlab CE on my server and use several different user accounts to group my repos by interest. The problem is with SSH.

I found the following solution for github:

https://gist.github.com/jexchan/2351996

In this guide one just uses different hosts with the same hostname in the config. Which is little effort to achieve what I'd like to achieve. But this solution does not work with Gitlab or at least not for me.

This solution is all over the web. One that is less used but is working for me, is this one:

https://gist.github.com/gubatron/d96594d982c5043be6d4

In the second, one assigns subdomain names as hosts in the ssh config with the same hostnames and uses the same subdomains in the git config. Little example:

SSH config:

Host user1.git.mydomain.at
  HostName git.mydomain.at
  IdentityFile ~/.ssh/id_rsa_user1

Host user2.git.mydomain.at
  HostName git.mydomain.at
  IdentityFile ~/.ssh/id_rsa_user2

git:

git remote set-url origin admin@user1.git.mydomain.at:user1/foo.git
git remote set-url origin admin@user2.git.mydomain.at:user2/foo.git

One can see, that I have to change every repo url manually. I would like to avoid this, and would prefer the first solution.

Am I missing something important?


回答1:


You need both.

I Assuming that:

  1. usernames (not email) are user1 and user2 respectively

  2. you have uploaded adequate public (i.e. .pub) keys, for which the respective private keys are user1_rsa and user2_rsa

  3. the server url is in the form server.com (i.e. substitute server.com by bitbucket.org, github.com, etc.) then ~/.ssh/config should contain:

    Host server.com-user1
      HostName server.com
      User git
      IdentityFile ~/.ssh/user1_rsa
    
    Host server.com-user2
      HostName server.com
      User git
      IdentityFile ~/.ssh/user2_rsa
    

II. In your git directory, assuming that you have added an origin named origin and are in a domain named domain and a project named project, then git remote -v should be configured to return something like this:

origin  git@server.com-user1:domain/project.git (fetch)
origin  git@server.com-user1:domain/project.git (push)

  • You could have obtained this output in the first place by adding the remote directory using git remote add origin git@bitbucket.org-user1:domain/project.git. The key is the extra -user1, with a dash, matching in both files, and matching your username.
  • You can also edit .git/config after the fact and add -user1 by hand.


来源:https://stackoverflow.com/questions/29775626/ssh-config-with-multiple-keys-for-multiple-gitlab-user-accounts

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