Manage multiple SSH keys for different GitHub accounts

Manage multiple SSH keys for different GitHub accounts

If you have several GitHub accounts, for example, one for your personal projects and one for your work, it's difficult to use SSH for both. You would normally need separate machines to authenticate to different GitHub accounts.

But this can be solved easily by configuring the SSH configuration file.

Here's how:

Create another SSH key pair and add it to your other GitHub account. Remember the name of the file to which you are assigning the new key.

ssh-keygen -t ed25519 -C "email"

Create the SSH configuration file. The configuration file tells the SSH program how it should behave. By default, the configuration file may not exist, so create it inside the .ssh folder:

$ touch ~/.ssh/config

Modify the SSH configuration file. Open the configuration file and paste the code below:

# GitHub account for everyday use

Host github.com
  HostName github.com
  IdentityFile ~/.ssh/key_name
  IdentitiesOnly yes

# Account for work
Host github-work
  HostName github.com
  IdentityFile ~/.ssh/work_key_name
  IdentitiesOnly yes

So, when you need to authenticate via SSH using your work or secondary account, you set the SSH address:

git@github.com:github_account/project.git

to

git@github-work:github_account/project.git

Don't forget to add a reaction if the post has helped.