DEV Community

Cover image for Config SSH GitHub with multiple accounts
Khoa Nguyen
Khoa Nguyen

Posted on

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): 
Enter fullscreen mode Exit fullscreen mode

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):
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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! 🎉 You're now able to clone the repo successfully.

4. Changing GitHub profile when pushing commits

Sometimes, your push to a GitHub repository might be rejected. This usually happens because you're using the wrong GitHub profile. To fix this, simply switch to the correct profile before pushing your code.

git config user.name "example"
git config user.email "example@gmail.com"
Enter fullscreen mode Exit fullscreen mode

And that's all for managing multiple GitHub profiles! Thanks for reading.

Heroku

Amplify your impact where it matters most — building exceptional apps.

Leave the infrastructure headaches to us, while you focus on pushing boundaries, realizing your vision, and making a lasting impression on your users.

Get Started

Top comments (0)

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay