jonsully1.dev

Managing Multiple GitHub Accounts with SSH

Cover Image for Managing Multiple GitHub Accounts with SSH
Photo by Yancy Min  on Unsplash
John O'Sullivan
John O'Sullivan
Senior Full Stack Engineer
& DevOps Practitioner

Managing multiple GitHub accounts on a single machine can be streamlined by configuring SSH keys for each account. Here's a step-by-step guide tailored for my setup.

Steps:

  1. Create SSH keys for all accounts
  2. Add SSH keys to the SSH Agent
  3. Add SSH public keys to GitHub
  4. Create a Config File and Make Host Entries
  5. Clone GitHub repositories using different accounts

Step 1: Create SSH keys for all accounts

First, navigate to your .ssh directory:

cd ~/.ssh

Generate unique SSH keys for each account using the following syntax:

ssh-keygen -t rsa -C "your-email-address" -f "github-username"

Here:

  • -C adds a comment to help identify your SSH key.
  • -f specifies the filename where your SSH key will be saved.

For my two accounts, I generated the keys as follows:

ssh-keygen -t rsa -C "john.osullivan@company.com" -f "github-jonsully1dev"
ssh-keygen -t rsa -C "jon@jonsully1.dev" -f "github-jonsully1"

After running these commands, you'll be prompted for a passphrase. You can leave it empty and proceed.

This process generates a public and private key pair for each account. The public key has a .pub extension, while the private key has no extension.

Step 2: Add SSH keys to the SSH Agent

To use the keys, add them to the SSH Agent:

ssh-add -K ~/.ssh/github-jonsully1dev
ssh-add -K ~/.ssh/github-jonsully1

This ensures that your SSH keys are loaded and available for authentication.

Step 3: Add SSH public keys to GitHub

Next, add each public key to its corresponding GitHub account:

  1. Copy the public key:

    You can open the public key file and copy its contents:

    cat ~/.ssh/github-jonsully1dev.pub
    cat ~/.ssh/github-jonsully1.pub
    

    Alternatively, copy the content directly to the clipboard:

    pbcopy < ~/.ssh/github-jonsully1dev.pub
    pbcopy < ~/.ssh/github-jonsully1.pub
    
  2. Paste the public key on GitHub:

    • Sign in to your GitHub account.
    • Navigate to Settings > SSH and GPG keys > New SSH Key.
    • Paste your copied public key and provide a recognizable title.

Repeat these steps for each GitHub account.

Step 4: Create a Config File and Make Host Entries

Configure SSH to differentiate between your accounts by editing the config file in your .ssh directory:

vim ~/.ssh/config

Add the following entries:

# Personal GitHub account
Host github.com-jonsully1
  HostName github.com
  User git
  IdentityFile ~/.ssh/github-jonsully1

# Work GitHub account
Host github.com-jonsully1dev
  HostName github.com
  User git
  IdentityFile ~/.ssh/github-jonsully1dev

This configuration allows SSH to use the correct key for each account based on the specified host.

Step 5: Clone GitHub repositories using different accounts

When cloning repositories, specify the appropriate host to use the corresponding SSH key:

# For personal account
git clone git@github.com-jonsully1:username/repository.git

# For work account
git clone git@github.com-jonsully1dev:username/repository.git

By following these steps, you can seamlessly manage multiple GitHub accounts on a single machine.