← Back to home

Managing Multiple GitHub Accounts with SSH Keys

I maintain multiple GitHub accounts on the same machine:

The cleanest and most reliable way to work with multiple GitHub accounts on a single computer is to use separate SSH keys and map each one to a custom SSH host. This avoids constantly switching credentials or accidentally pushing to the wrong account.

The setup steps involve:

1. Create an SSH key for each GitHub account

Each GitHub account should have its own SSH key pair (public + private). Follow GitHub’s official documentation for each GitHub account, making sure to give custom-names for each SSH key. You can name these hosts and key files however you like—just keep them consistent:

After this step, you should have multiple private and public keys on your machine, one per GitHub account. For example, when I look at the files in the ~/.ssh folder, ear_ed25519 and ear_ed25519.pub are the private and public keys associated with the eric-rosen GitHub account, whereas cl_ed25519 and cl_ed25519.pub are the private and public keys associated with the continuallylearning account:


eric@ericbot:~$ ls -lh ~/.ssh
    total 52K
    -rw------- 1 eric eric  432 Dec 27 17:25 cl_ed25519
    -rw-r--r-- 1 eric eric  118 Dec 27 17:25 cl_ed25519.pub
    -rw-rw-r-- 1 eric eric  250 Dec 27 17:31 config
    -rw------- 1 eric eric  419 Dec 27 17:30 ear_ed25519
    -rw-r--r-- 1 eric eric  109 Dec 27 17:30 ear_ed25519.pub
    

The GitHub instructions tell you to add the ssh keys to the SSH agent via ssh-add /PATH/TO/PRIVATE_KEY, make sure you've done this by checking that all the keys are loaded. For example:

eric@ericbot:~$ ssh-add -l
256 SHA256:rVtbltX9gB1gA+oJ1tdsftbdnXduNdwzedqDKeIMlNE eric.andrew.rosen@gmail.com (ED25519)
256 SHA256:s19ObVbe6dheJtIPeuhRKJceoi6kV3oJzpdU6Rv/GVk continuallylearninggithub@gmail.com (ED25519)

2. Assign a hostname to each SSH key

Next, edit your SSH config file:


~/.ssh/config
      

Define a separate host entry for each GitHub account:


# GitHub account alias for eric-rosen
Host github-eric
    HostName github.com
    User git
    IdentityFile ~/.ssh/ear_ed25519
    IdentitiesOnly yes

# GitHub account alias for continuallylearning
Host github-cl
    HostName github.com
    User git
    IdentityFile ~/.ssh/cl_ed25519
    IdentitiesOnly yes
      

Here:

3. Verify SSH access for each account

Test that each SSH key is correctly associated with its GitHub account:


ssh -T git@github-eric
ssh -T git@github-cl
      

Each command should authenticate successfully and indicate which GitHub account you’re logged in as.

4. Configure Git remotes to use the correct host

To push and pull using the correct account, your repository must:

Go into directory of the repository, and check your current remote:


git remote -v
      

Example output:


origin  git@github.com:eric-rosen/eric-rosen.github.io.git (fetch)
origin  git@github.com:eric-rosen/eric-rosen.github.io.git (push)
      

If the host or username is incorrect, update it using the following format:


git@{host-name}:{repo_username}/{repo_name}.git
      

For example:


git remote set-url origin git@github-eric:eric-rosen/eric-rosen.github.io.git
      

5. Final checklist

If both the SSH test and the remote configuration are correct, you’re all set.