Introduction:

When managing multiple GitHub accounts on a single machine, SSH authentication can present challenges. I'm writing this article immediately after spending { too many } hours battling with this. I tried following guidance from GitHub documentation and Stack Overflow, none of which actually worked for me, I'm not sure why, but I'm hoping this might help you out. I found the seed of my solution from an AWS user data script we have that pulls a git repo down onto an EC2 instance when it starts up, the rest I just adapted for my repo.

I'm running Linux Mint, I'm a serial tinkerer with Linux distributions, but when it comes to low hassle Linux, this one has stuck around more than any other, ultimately I like things that work and don't cause too many headaches, Mint fits the bill on that front for me. I have two GitHub accounts, one for my website code and another for code repositories I want to make public. If I run git config --global --list it shows me the config for my website repo, which is how I'd expect it to look. I just wanted to have a repo in another GitHub account that I could edit, pull, push etc.

Prerequisites:

  • Linux, I'm using Mint, but I'm sure this will either work or be easily adaptable to other distributions.
  • Basic understanding of terminal commands

Step-by-Step Guide:

  1. Generate ed25519 SSH Keys and add them to you secondary github account:

Ed25519 is a public-key cryptography algorithm based on the EdDSA signature scheme. It is secure and efficient and perfectly sufficient for this purpose. The "arryw.git" string can be anything you like, it's just a comment to help you remember what the key is for.

ssh-keygen -t ed25519 -C "arryw.git"

# You can choose something else other than the default file name if you already have id_ed25519 files in ~/.ssh
# You would do it like this:
ssh-keygen -t ed25519 -C "arryw.git" -f ~/.ssh/id_ed25519_arryw

# You can just do the following and let it output to ~/.ssh/id_ed25519 but for the remainder of this article, I'm going to assume we want distinctly named keys
ssh-keygen -t ed25519 -C "arryw.git"

This should output 2 files to your ~/.ssh directory, id_ed25519_arryw and id_ed25519_arryw.pub. The .pub file is the one you want to add to your secondary GitHub account. Do this by going to your GitHub account, click your profile picture & go to Settings > SSH and GPG keys > New SSH key. Copy the contents of the id_ed25519.pub file into the key field and give it a name.

  1. Global Git Configuration:

Check your global Git configuration with:

git config --global --list

If you have a global Git configuration, it should be reflected in the output. This is not required but it is good practise to record this information prior to making changes.

  1. SSH Configuration:

Create or edit your ~/.ssh/config file to define a different Host for your secondary GitHub account:

Host github-arryw
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_arryw
  1. Add SSH Keys to SSH-Agent:

Ensure your SSH keys are added to the ssh-agent:

eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519_arryw

# Check to see if your key has been added
ssh-add -l
  1. GitHub Authentication Check:

Test your connection to GitHub with:

ssh -T git@github-arryw

You should receive a message confirming your connection to GitHub.

  1. Cloning Repositories:

Use the following SSH syntax for cloning:

GIT_SSH_COMMAND="ssh -i ~/.ssh/id_ed25519_arryw -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no" git clone git@github.com:arryw/ArryWalkerCodebase.git
  1. Git Configuration:

When you now run a git remote -v, you will see what looks like a correct URL for your secondary GitHub account, but this needs a slight adjustment:

origin	git@github.com:arryw/ArryWalkerCodebase.git (fetch)
origin	git@github.com:arryw/ArryWalkerCodebase.git (push)

To make the adjustment, run:

git remote set-url origin git@github-arryw:arryw/ArryWalkerCodebase.git

Now git remote -v should show the correct configuration:

origin	git@github-arryw:arryw/ArryWalkerCodebase.git (fetch)
origin	git@github-arryw:arryw/ArryWalkerCodebase.git (push)

Conclusion:

You should now be able to use straight-up git commands like git add, git commit, git push etc. without any issues. This setup should enable seamless switching between multiple GitHub accounts on a single Linux machine.

Troubleshooting:

  • Verify repository URLs are case-sensitive and correct.
  • Confirm SSH keys are added to GitHub accounts.
  • Ensure the SSH key has read/write access to the repository.