How to Use Multiple Github Accounts With SSH Keys on the Same Machine

There might be a chance that you need to create and use a separate github(or bitbucket) account other than your personal one in your workplace. And Github(Bitbucket or Gitlab will do the same I believe) doesn't allow to share a ssh key between different accounts for authentication.

That's the case we are going to figure out below.

First of all, create a new ssh key pair on your machine.

1ssh-keygen -t ed25519 -C "[email protected]"
2# Note: If you are using a legacy system that doesn't support the Ed25519 algorithm, use:
3ssh-keygen -t rsa -b 4096 -C "[email protected]"

Next, edit/create the ssh config file.

1vim ~/.ssh/config
 1# Default github account: crazyoptimist
 3   HostName
 4   IdentityFile ~/.ssh/id_rsa
 5   IdentitiesOnly yes
 7# Workplace github account: extremeoptimist
 8Host github-workplace   # <-- This is the hostname that you will actually add by `git remote add`
 9   HostName
10   IdentityFile ~/.ssh/id_ed25519
11   IdentitiesOnly yes

Test the ssh authentication.

1ssh -T [email protected]
2ssh -T git@github-workplace

Now you should be able to clone repos in your workplace org.

1git clone git@github-workplace:your-org/some-cool-project.git

You got an idea to do this in a better way? Then you got it!

Happy coding!