Skip to content

Instantly share code, notes, and snippets.

@juandm
Last active February 22, 2024 02:50
Show Gist options
  • Save juandm/d7eb0af74c60f8d957979bc6299eb248 to your computer and use it in GitHub Desktop.
Save juandm/d7eb0af74c60f8d957979bc6299eb248 to your computer and use it in GitHub Desktop.
Set up multiple git accounts (Gitlab, Github, Bitbucket) with SSH in the same machine

Multiple GIT accounts setup

This short tutorial shows how to set up multiple different accounts of many git repository sites using SSH, all this tutorial and its commands were aplied in Ubuntu.

Generate SSH keys

Generate SSH keys for each one of the sites you wanna connect:

  1. Open your terminal and type the following command to generate a RSA key:

    ssh-keygen -o -t rsa -b 4096 -C "email@example.com"

    When prompted for the path of the key, put a specific name to identify the key, for example id_rsa_gitlab.

    Generating public/private rsa key pair.
    Enter file in which to save the key (/home/username/.ssh/id_rsa): /home/username/.ssh/id_rsa_gitlab
    

    After that when asked for password set-up one if you want or press enter to leave it blank.

  2. List the new created SSH keys using the following command and validate that the created key is here :

    ls -al ~/.ssh
    
  3. In order to use the generated keys, you’ll need to save them to a configuration file. For OpenSSH clients this is configured in the ~/.ssh/config file. In this file you can set up configurations for multiple hosts, like GitLab, GitHub, Bitbucket, etc.

    If there is not any config file create it executing touch config and edit it (via nano or any text editor) following the example below.

    # GitLab.com
    Host gitlab.com
      Preferredauthentications publickey
      IdentityFile ~/.ssh/id_rsa_gitlab
    
    # Github.com
    Host github.com
      Preferredauthentications publickey
      IdentityFile ~/.ssh/id_rsa_github
    
  4. Add the SSH keys in the respective sites, for example, for gitlab follow: https://docs.gitlab.com/ee/gitlab-basics/create-your-ssh-keys.html

  5. To test the connection (in this case to gitlab) use the following command:

    ssh -T git@gitlab.com
    

    If all things are OK the server returns the following message:

    Welcome to GitLab, @username! message.

    If there is a problem authenticating try to run the last command with the verbose argument to check out the details of the process:

    ssh -vT git@gitlab.com
    

    Also try to start the ssh-agent using the following command

    eval $(ssh-agent -s)
    ssh-add ~/.ssh/other_id_rsa
    
@soniacjallah
Copy link

Thank you! (However, the link on #4 does not work)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment