Mastering Multiple GitHub Accounts: A Developer's Guide to SSH Configuration

Learn how to efficiently manage multiple GitHub accounts on a single machine using SSH keys for authentication. This guide walks you through setting up your SSH config, verifying connections, and ensuring secure practices for juggling work, personal, and educational projects.

As developers, we often find ourselves juggling multiple GitHub accounts.

Perhaps one for work, another for personal projects, and maybe even a third for educational purposes. Managing these accounts efficiently on a single machine can be tricky. With the right SSH configuration, it becomes a LAZY!

In this tutorial, we’ll walk through the process of setting up and managing multiple GitHub users pushing and pulling code using SSH keys.

Security Tip: We are going to be playing around with SSH keys in this tut. Never share your private SSH keys. If you believe a key has been compromised, generate a new pair and update your GitHub settings accordingly. If you need a refresher on how to generate new keys check out the Offical Lazy Dev Guide titled “Push and Clone Github Repos with ed25519 SSH Authentication for Lazy Devs”

Prerequisites

If you need help with getting github setup with ssh keys, check out the official Lazy Dev School tutorial titled “Push and Clone GitHub Repos with Ed25519 SSH Authentication for Lazy Devs

Setting Up Your SSH Config File

The key to using different SSH keys for different GitHub accounts lies in your SSH config file.

This file is typically located at ~/.ssh/config on Unix-based systems (including macOS) and at C:\Users\YourUsername\.ssh\config on Windows.

# Open the SSH config file
nano ~/.ssh/config

Let’s reformat the config file to include our two accounts.

This is the format for the SSH config file. You will want to update the text with your own information. Think of the Host line as an alias. Inside the Host block we can specify the HostName, User, and IdentityFile (the path to your private key).

~/.ssh/config
# Tutorial User Account
Host github-tutorial
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_ed25519_tutorial

# Personal Account
Host github-personal
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_ed25519_personal

Verify SSH Key Permissions

# check the permissions of the key
ls -l ~/.ssh/id_ed25519

All the private keys should have 600 permissions.

-rw-------

If need be, update the permissions.

chmod 600 ~/.ssh/id_ed25519
chmod 700 ~/.ssh

Here is a guide created by Linode.com to help you learn the basics of linux file permissions.

Verify Git Global Settings

Before we get started let’s just verify that our global git config so we know what we are working with.

git config --global user.name
git config --global user.email

Verify SSH Connection

Let’s also test our default SSH connection to github. Not only are you looking for a success message but the name returned in the success message should match the name of the account you are using.

ssh -T git@github.com
# success message
Hi lazydevschooltut! You've successfully authenticated, but GitHub does not provide shell access.
ssh -T github-personal

Troubleshooting

On some systems you may need to restart the ssh service after making changes to the SSH config file.

# reload the SSH agent
sudo systemctl restart sshd

If you get the wrong user returned you might need to take a look at the SSH Agent.

# list out keys in the agent
ssh-add -l

If the key is not found, add it.

# add a key to the agent
ssh-add ~/.ssh/id_ed25519

You may need to remove one or more keys from the agent.

ssh-add -d ~/.ssh/id_ed25519

A last resort clear the agent cache.

ssh-add -D
ssh-add ~/.ssh/id_ed25519

Clone a GitHub Repo

Let’s user our default connection to clone a repo.

# clone the repo
git clone git@github.com:lazydevschool/python-cli.git

Create a Repo From Scratch

Here we will create a new repo from scratch. The intent for this repo is that we push it to our personal account.

mkdir delete-me-repo && cd delete-me-repo
echo "# delete-me-repo" >> README.md

Set Local Git Config

# initialize the repo
git init
# set the local git config
git config user.name "cosa"
git config user.email "cosa@aol.com"
# stage and commit the changes (local only)
git add .
git commit -m "initial commit"

Verify Git Config

Ensure the git repo user.name and user.email match the account you are trying to use.

git config user.name
git config user.email

Push Your Changes

Now let’s create a new repo and push our changes to a new branch. Can you see the slight change we made inthe origin url? That’s right we are using the SSH config for our personal account.

git remote add origin github-personal:bfmcneill/delete-me-repo.git
git branch -M main
git push -u origin main

-u is short for --set-upstream. This will push the current branch to the remote repo and set the upstream branch. You only need to do this once. Every subsequent push can be done with git push.

Final Remarks

Congratulations! You have successfully set up and verified multiple GitHub accounts using SSH keys.

Managing multiple GitHub accounts on a single machine may have seemed daunting at first, but with the right SSH configuration, there is a LAZY way to do it.

Stay lazy and keep coding!