How to access multiple github accounts from one computer


If you simply copy your ssh key from one github account to another github account, you will find it’s not working. And After you click add the ssh key, it won’t appear under the ssh key list.

So to access multiple git accounts in one computer, you will need to this.

SSH allows you to create multiple SSH keys and specify which SSH key to use for which server or host. This can be achieved by setting the following steps:

  1. Generate a new SSH key for each Git account using the command

    ssh-keygen -t rsa -C "your-email@example.com" 

    Replace “your-email@example.com“ with the email you used for your Git account. When asked for a file to save the key, choose a location that does not already contain an SSH key (for example: ~/.ssh/id_rsa_new).

  2. After creating the keys, you need to add them to ssh-agent. This is done using the ssh-add command, for example:

    ssh-add ~/.ssh/id_rsa_new
  3. Then you will create or modify the config file inside the .ssh directory (Create it if it doesn’t exist)

    nano ~/.ssh/config
  4. Inside this file, you will define each host with its respective SSH key. For example:

    # Account 1
    Host github.com-account1
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa_account1

    # Account 2
    Host github.com-account2
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa_account2
  5. Afterward, we have to do the cloning in a different way to use specific ssh key via git. for example:

    git clone git@github.com-account1:username1/repo.git
    git clone git@github.com-account2:username2/repo.git
  6. Lastly, go to your Git account settings. Find “SSH and GPG keys” and click “New SSH key”. Copy the content of your public ssh key (e.g., id_rsa_new.pub) and paste it here.

By doing this, you can work with multiple Git accounts on the same machine. Remember to replace “account1” and “account2”, “username1” and “username2” with your actual account usernames.


Author: robot learner
Reprint policy: All articles in this blog are used except for special statements CC BY 4.0 reprint policy. If reproduced, please indicate source robot learner !
  TOC