Configuring Multiple SSH Keys: A Guide for Azure and GitLab Workspaces

NHAILA Achraf
2 min readSep 28, 2023
Configuring Multiple SSH Keys

When you’re a freelancer juggling projects from different companies, it’s a good idea to have a separate SSH key for each one. Think of it as having a different house key for each friend’s place. It keeps things secure and neatly organized!

For example we will configure git ssh for
company1 — AZURE-DEVOPS
company2 — Gitlab

1 — Generate SSH Keys

Firstly, you’ll want to generate distinct SSH keys for each organization or repository.

ssh-keygen -t rsa -b 4096 -C "achraf.nhaila@company1.com" -f ~/.ssh/id_rsa_company1
ssh-keygen -t rsa -b 4096 -C "achraf.nhaila@company2.com" -f ~/.ssh/id_rsa_company2

2 — Add SSH Keys to the SSH Agent

Before you can use these keys with Git, you’ll need to add them to the SSH agent.

eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_rsa_company1
ssh-add ~/.ssh/id_rsa_company2

3 — Upload Public Keys to Git Repositories

For each organization or repository, go to the SSH key settings (on GitHub, it’s in the settings for your profile or organization) and add the corresponding public key
(e.g, id_rsa_company1.pub for azure devops repo).
(e.g, id_rsa_company2.pub for gitlab).

GitLab:

  1. Log in to your GitLab account.
  2. Click on your avatar at the top right, then select Settings.
  3. In the left sidebar, click on SSH Keys.
  4. Copy and paste the content of your SSH public key (e.g., id_rsa_company2.pub) into the "Key" field.
  5. Click Add key.

Azure DevOps:

  1. For a User:
  2. Log in to your Azure DevOps account.
  3. Click on your profile picture at the top right, then select Security.
  4. Under “SSH public keys”, click on + New Key.
  5. Provide a description and then copy and paste the content of your SSH public key (e.g., id_rsa_company1.pub) into the provided field.
  6. Click Save.

Create an SSH Config File

Create or edit the SSH config file in ~/.ssh/config.

and use the private ssh key generate on step 1 :
~/.ssh/id_rsa_company1

~/.ssh/id_rsa_company2

# company  1 on Azure DevOps
Host ssh.dev.azure.com
HostName ssh.dev.azure.com
User git
IdentityFile ~/.ssh/id_rsa_company1
IdentitiesOnly yes



# company 2 on gitlab
Host gitlab-company2
HostName gitlab.com
User git
IdentityFile ~/.ssh/id_rsa_company2

Use the SSH Config in Git

When cloning a repository, use the alias you set in your SSH config file.

For the first organization azure :

git clone git@azure-company:company1/repo.git

For the second organization gitlab:

git clone git@gitlab:company2/repo.git

For existing repositories, you can check and change the remote URL:

git remote -v
#if result of youre url is an HTTP you can use the command bellow to set the Git url

git remote set-url origin git@azure-company:company1/repo.git

By following these steps, you can maintain multiple SSH keys for different organizations or repositories on platforms like GitHub, GitLab, Bitbucket, and others.

i hope this will help ,If you have any further questions or run into issues, don’t hesitate to reach out.

--

--