Rob Stewart

stewartcircle.com

Setup Multiple GitLab.com Accounts with SSH Authentication

2022-07-09 3 min read Git GitLab How-to

Recently, I created a second GitLab.com account to host the repository for this blog. GitLab.com does not permit you to use one SSH key for multiple user accounts; in their documentation, they state that you must use a different key pair for each account. Unfortunately, even though I generated a second SSH key pair for the new account and uploaded the public key to GitLab.com, I had trouble using SSH to access the repositories associated with each account on Linux Mint 20.3.

I finally got SSH authentication to work with both GitLab accounts by reorganizing my Git repository directories and then modifying my Git configuration files.

Reorganizing Git Repository Directories

I first reorganized my directories so that all of my personal Git repositories were under a directory called personal and all of my project related Git repositories were under another directory called projects. My directories are now set up like this:

.
├── personal
│   └── git-repository-1
│   └── git-repository-2
│   └── . . . 
├── projects
│   └── git-repository-3
│   └── git-repository-4
│   └── . . . 

Modifying Git Configuration Files

I then modified the Git configuration files in my home directory like this:

~/.gitconfig File

The .gitconfig file is the primary configuration file used by Git. I added two includeIf directives to reference the personal and projects directories and then added the path under each directive to reference a new configuration file with settings that will apply to Git repositories under the directory specified in the includeIf directive.

# .gitconfig file
[user]
    name = Rob Stewart
[includeIf "gitdir:~/personal/"]
    path = ~/.gitconfig-personal
[includeIf "gitdir:~/projects/"]
    path = ~/.gitconfig-projects

~/.gitconfig-personal File

I created a new file called .gitconfig-personal. This file has the email address and the SSH command with the path to the private key to use for Git repositories in the personal directory.

# ~/.gitconfig-personal
[user]
    email=personalemail@example.com
[core]
    sshcommand="ssh -i ~/.ssh/personal-id_rsa"

~/.gitconfig-projects File

I created a second new file called .gitconfig-projects. This file has the email address and the SSH command with the path to the private key to use for Git repositories in the projects directory.

# ~/.gitconfig-projects
[user]
    email=projectemailaddress@example.com
[core]
    sshcommand="ssh -i ~/.ssh/projects-id_rsa"

Configuration Validation

After I made these changes, I navigated to one of the Git repository directories underneath the personal directory and ran the following command:

git config --list --show-origin

The output showed the correct email address and SSH command from the ~/.gitconfig-personal configuration file:

file:/home/rob/.gitconfig            user.name=Rob Stewart
file:/home/rob/.gitconfig-personal   user.email=personalemail@example.com
file:/home/rob/.gitconfig-personal   core.sshcommand=ssh -i ~/.ssh/personal-id_rsa
file:/home/rob/.gitconfig            includeif.gitdir:~/personal/.path=~/.gitconfig-personal
 . . . 

I then navigated to one of the Git repository directories underneath the projects directory and ran the same command again:

git config --list --show-origin

The output showed the correct email address and SSH command from the ~/.gitconfig-projects configuration file:

file:/home/rob/.gitconfig            user.name=Rob Stewart
file:/home/rob/.gitconfig-projects   user.email=projectemailaddress@example.com
file:/home/rob/.gitconfig-projects   core.sshcommand=ssh -i ~/.ssh/projects-id_rsa
file:/home/rob/.gitconfig            includeif.gitdir:~/projects/.path=~/.gitconfig-projects
 . . . 

Organizing my Git Repositories into directories and then adjusting my configuration files correctly enabled me to use both sets of SSH keys successfully with GitLab.com. Now all I need to do is make sure that I change to the correct parent folder before I clone a new repository.

Sources