satanik
satanik

Reputation: 602

SSH config with multiple keys for multiple gitlab user accounts

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 [email protected]:user1/foo.git
git remote set-url origin [email protected]: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?

Upvotes: 3

Views: 2726

Answers (1)

SwiftArchitect
SwiftArchitect

Reputation: 48514

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  [email protected]:domain/project.git (fetch)
origin  [email protected]:domain/project.git (push)

  • You could have obtained this output in the first place by adding the remote directory using git remote add origin [email protected]: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.

Upvotes: 2

Related Questions