Setting up multiple Gitlab accounts and SSH keys on Linux/MacOS

June 14, 2023
gitlabssh configurationkeys

There are two simple ways to configure multiple Gitlab accounts and SSH keys on Linux and MacOS. The first method is to configure the SSH config file to handle multiple accounts, the second is to alter the SSH command that git uses within each repository.


For both methods you need keys if you don't already have them setup. Gitlab allows an SSH key to be used on only one account so you'll need a separate key for each account. Note which key you add to which account. Gitlab has a guide on how to create keys.

(Best Method) Modify the SSH command in the Git configuration for each repository

Another option to configure SSH for multiple Gitlab accounts is to set a custom SSH command within each repositories’ config file. You can use multiple repositories by specifying the correct key file location in each command. This is easier to setup but requires remembering the correct keyfile path each time you create or clone a new repository. From within the repository, set the ssh command locally with the command below.

  
git config core.sshCommand "ssh -i /path/to/key"
  

This method can be combined with some bash aliases or functions to make pointing the repository a simple command. 

(Lesser Method) Add alternates in the SSH configuration file

Custom configuration is handled by creating a file named config in the ~/.ssh folder. It's possible that this file hasn't been created yet. If it's not present run the following commands to create it.

  
touch ~/.ssh/config;
sudo chmod 600 ~/.ssh/config;
  

Once the file is created add domain entries for each account, append a unique string to them using a hyphen. We'll change the Git configuration to use this domain. Because the HostName parameter is set the requests will go to the right place.


Below is an example configuration for three accounts, each uses the key created and added to that account. When you put the path to the IdentifyFile remember to link the private key, this is typically the key file without the .pub extension. I also left the last Host unaltered, this is my most common key which will be the default.

    
# Secondary                           
Host gitlab.com-SECONDARY_GITLAB
     HostName gitlab.com
     PreferredAuthentications publickey
     IdentityFile ~/.ssh/secondary_key

# Tertiary
Host gitlab.com-TERTIARY_GITLAB
     HostName gitlab.com
     PreferredAuthentications publickey
     IdentityFile ~/.ssh/tertiary_key

# default
Host gitlab.com
     HostName gitlab.com
     PreferredAuthentications publickey
     IdentityFile ~/.ssh/default_key
    

The last step is to configure the git repositories to match the aliases. In each repository edit the .git/config file. Look for the lines setting the urls for the remote repository, it should look something like this.

    
url = git@gitlab.com:YourUsername/Reponame.git
    

So, for example, to modify the URL to use the Secondary account from the example configuration change the domain like below. Don't remove the preceding git@ or the :YourUsername from the URL.

    
url = git@gitlab.com-SECONDARY_GITLAB:YourUsername/Reponame.git