Discover Cloud Solutions with HostingerGet a special discount.

hostingerLearn More
Published on

How to Set Up Multiple GitHub Accounts with SSH Keys

Authors
  • avatar
    Name
    Luis Carbonel
    Twitter

Overview

In this article, I'll guide you through setting up multiple GitHub accounts using SSH keys on macOS. This is particularly useful if you need to manage both a personal and a professional profile. We'll cover everything from generating SSH keys to configuring your ~/.ssh/config file and cloning repositories with the correct account.

Step 1: Generate SSH Keys for Each Account

First, open the Terminal and generate an SSH key for your personal account:

ssh-keygen -t ed25519 -C "your_personal_email@example.com" -f ~/.ssh/id_rsa_personal

Then, generate an SSH key for your professional account:

ssh-keygen -t ed25519 -C "your_professional_email@example.com" -f ~/.ssh/id_rsa_professional

Step 2: Add SSH Keys to the SSH Agent

Start the SSH agent:

eval "$(ssh-agent -s)"

Add your SSH keys to the agent:

For Mac OS:

ssh-add --apple-use-keychain ~/.ssh/id_rsa_personal
ssh-add --apple-use-keychain ~/.ssh/id_rsa_professional

For Linux:

ssh-add ~/.ssh/id_rsa_personal
ssh-add ~/.ssh/id_rsa_professional

Step 3: Configure the ~/.ssh/config

Configure your ~/.ssh/config file to use the appropriate keys based on the host. Open or create the file with the following command:

nano ~/.ssh/config

Add the configuration for your personal account:

Host github.com-personal
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa_personal
  AddKeysToAgent yes
  UseKeychain yes

Add the configuration for your professional account:

Host github.com-professional
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa_professional
  AddKeysToAgent yes
  UseKeychain yes

Copy the SSH public key to your clipboard.

To copy the public key to your clipboard, run the following command:

pbcopy < ~/.ssh/id_rsa_personal.pub

Do the same for the professional account:

pbcopy < ~/.ssh/id_rsa_professional.pub

Add the SSH public keys to your GitHub accounts.

  1. Go to your GitHub account settings.

  2. Click on New SSH key.

  3. Paste the public key for your personal account and give it a title.

Step 4: Test the SSH Connection

Verify that you can authenticate correctly with each account:

For the personal account:

ssh -T git@github.com-personal

For the professional account:

ssh -T git@github.com-professional

You should see a success message similar to:

Hi [your-username]! You've successfully authenticated, but GitHub does not provide shell access.

Troubleshooting

In linux, if you encounter the error

/root/.ssh/config: line 6: Bad configuration option: usekeychain
/root/.ssh/config: terminating, 1 bad configuration options

you can fix it by running the following command:

sed -i 's/UseKeychain/IgnoreUnknown/' ~/.ssh/config

Step 5: Clone Repositories Using the Correct Account

Now you can clone repositories by specifying the configured host.

To clone a repository using your personal account:

git clone git@github.com-personal:user/personal-repo.git

To clone a repository using your professional account:

git clone git@github.com-professional:organization/professional-repo.git

Conclusion

Setting up multiple GitHub accounts with SSH keys allows you to efficiently manage different profiles without conflicts. By following these steps, you can ensure that you're always using the correct SSH key for each account, simplifying your workflow and enhancing the security of your GitHub operations.

References