Config SSH GitHub with multiple accounts

When working with GitHub, you might need to switch between different accounts, such as a personal account and a company account. If not configured properly, this can become messy. So, how do we manage it efficiently? Let's get started. Note: This instruction is for Linux OS. 1. Generate SSH key based on profiles Using this command ssh-keygen -t rsa -C "example@gmail.com" to generate the account based on the email you used. The result of the command is: Generating public/private rsa key pair. Enter file in which to save the key (/home/khoanguyen/.ssh/id_rsa): Usually, we store our SSH key in the .ssh folder, so let's keep the folder location and only change the file name. For example, if I want to name the key example, I will enter the path as /home/khoanguyen/.ssh/example. Then, you will be asked to input the passphrase: Enter passphrase (empty for no passphrase): Usually, I leave it empty and skip the passphrase since I don’t want to enter it repeatedly when working with GitHub. However, if security is your priority, adding a passphrase will provide an extra layer of protection. That’s it for the first profile. Now, we’ll do the same for the second profile, and for this one, I will store the file in /home/khoanguyen/.ssh/example2. 2. Add SSH key to GitHub config file. Now, we have two SSH keys stored in the .ssh folder. Next, let's link these keys to our GitHub config file. First, create a file called config inside ~/.ssh/ using the following command: cd ~/.ssh/ touch config Then, add these lines of codes to the config file: #example account Host github.com-example HostName github.com User example IdentityFile ~/.ssh/example #example 2 account Host github.com-example2 HostName github.com User example2 IdentityFile ~/.ssh/example2 With: Host: This is the identifier for each profile. The github.com part remains the same, but you can add an identifier after it. In my case, I named them example and example2. HostName: This should always be github.com for every profile. User: Your GitHub username. IdentityFile: The path to the private SSH key file. Since we already stored them in the .ssh folder, we just need to reference the correct file. 3. Cloning repo with different accounts. Prequesite, we need the 2 SSH keys added to GitHub account. To do this, you can follow step by step in GitHub official docs https://docs.github.com/en/authentication/connecting-to-github-with-ssh/adding-a-new-ssh-key-to-your-github-account. Next, for cloning private repo from account example. I will enter this command in the terminal: git clone git@github.com-example:khoanguyn1411/Next-MyPortfolio.git Note: We have added the suffix -example after the host, as configured in the config file. This tells your local GitHub setup to use the example profile with the correct SSH key path when cloning the repository. And just like that—boom!

Mar 24, 2025 - 10:46
 0
Config SSH GitHub with multiple accounts

When working with GitHub, you might need to switch between different accounts, such as a personal account and a company account. If not configured properly, this can become messy. So, how do we manage it efficiently? Let's get started.

Note: This instruction is for Linux OS.

1. Generate SSH key based on profiles

Using this command ssh-keygen -t rsa -C "example@gmail.com" to generate the account based on the email you used.

The result of the command is:

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

Usually, we store our SSH key in the .ssh folder, so let's keep the folder location and only change the file name. For example, if I want to name the key example, I will enter the path as /home/khoanguyen/.ssh/example.

Then, you will be asked to input the passphrase:

Enter passphrase (empty for no passphrase):

Usually, I leave it empty and skip the passphrase since I don’t want to enter it repeatedly when working with GitHub. However, if security is your priority, adding a passphrase will provide an extra layer of protection.

That’s it for the first profile. Now, we’ll do the same for the second profile, and for this one, I will store the file in /home/khoanguyen/.ssh/example2.

2. Add SSH key to GitHub config file.

Now, we have two SSH keys stored in the .ssh folder. Next, let's link these keys to our GitHub config file.

First, create a file called config inside ~/.ssh/ using the following command:

cd ~/.ssh/
touch config

Then, add these lines of codes to the config file:

#example account
    Host github.com-example
        HostName github.com
        User example
        IdentityFile ~/.ssh/example

#example 2 account
    Host github.com-example2
        HostName github.com
        User example2
        IdentityFile ~/.ssh/example2

With:

  • Host: This is the identifier for each profile. The github.com part remains the same, but you can add an identifier after it. In my case, I named them example and example2.
  • HostName: This should always be github.com for every profile.
  • User: Your GitHub username.
  • IdentityFile: The path to the private SSH key file. Since we already stored them in the .ssh folder, we just need to reference the correct file.

3. Cloning repo with different accounts.

Prequesite, we need the 2 SSH keys added to GitHub account. To do this, you can follow step by step in GitHub official docs https://docs.github.com/en/authentication/connecting-to-github-with-ssh/adding-a-new-ssh-key-to-your-github-account.

Next, for cloning private repo from account example. I will enter this command in the terminal:

git clone git@github.com-example:khoanguyn1411/Next-MyPortfolio.git

Note: We have added the suffix -example after the host, as configured in the config file. This tells your local GitHub setup to use the example profile with the correct SSH key path when cloning the repository. And just like that—boom!