March 23, 2025

Managing Multiple SSH Git Accounts on One Machine (For Nerds)

If you work with multiple Git accounts (e.g., personal, work, open-source contributions), managing SSH keys efficiently is crucial. This guide provides an in-depth look into setting up multiple SSH keys for different Git accounts, debugging common issues, and understanding SSH authentication at a deeper level.


1. Why You Need Multiple SSH Keys for Git

GitHub, GitLab, and Bitbucket allow SSH authentication, eliminating the need to enter credentials repeatedly. However, when you have multiple accounts, using the same SSH key across them may lead to conflicts.

For instance:

  • You might need different keys for personal and work repositories.
  • Some organizations enforce separate SSH keys for security.
  • You contribute to multiple projects and want isolated access.

Is This the Best Way? Are There Alternatives?

Using SSH keys is one of the most secure and convenient methods for authentication. However, there are other ways to manage multiple Git accounts:

  1. Using HTTPS & Git Credential Helper: Instead of SSH, you can authenticate using HTTPS and a credential helper to store your passwords securely.

    • Pros: No need to configure SSH.
    • Cons: Requires entering credentials periodically or using a credential manager.
  2. Using Different User Profiles: You can create separate user profiles on your machine and configure different Git settings for each.

    • Pros: Full isolation between accounts.
    • Cons: More cumbersome, requires switching users frequently.
  3. Using SSH Key Switching Manually: Instead of configuring ~/.ssh/config, you can manually specify the SSH key during each Git operation.

    • Example:
      GIT_SSH_COMMAND="ssh -i ~/.ssh/id_ed25519_work" git clone git@github.com:workuser/repo.git
      
    • Pros: No persistent configuration needed.
    • Cons: Requires specifying the key for every command.

Using ~/.ssh/config remains the most automated and hassle-free solution, making SSH authentication seamless across multiple accounts.


2. Generating Multiple SSH Keys

Each SSH key is a cryptographic pair consisting of a private and public key. To create separate keys for different accounts:

ssh-keygen -t ed25519 -C "your-email@example.com"

When prompted:

  • File to save the key: Choose a unique filename, e.g., ~/.ssh/id_ed25519_work for a work account and ~/.ssh/id_ed25519_personal for a personal account.
  • Passphrase: You can add one for extra security.

Example:

Generating public/private ed25519 key pair.
Enter file in which to save the key (/Users/yourname/.ssh/id_ed25519): ~/.ssh/id_ed25519_work
Enter passphrase (empty for no passphrase):

3. Adding SSH Keys to SSH Agent

Ensure the SSH agent is running:

eval "$(ssh-agent -s)"

Then, add your newly generated SSH keys:

ssh-add ~/.ssh/id_ed25519_work
ssh-add ~/.ssh/id_ed25519_personal

To list currently added SSH keys:

ssh-add -l

If you see The agent has no identities, restart the SSH agent and re-add the keys.


4. Configuring SSH for Multiple Git Accounts

Modify or create the SSH configuration file:

nano ~/.ssh/config

Add the following entries:

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

# Work GitHub Account
Host github-work
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_ed25519_work
  • Host github-personal: This is a custom alias for GitHub personal use.
  • IdentityFile ~/.ssh/id_ed25519_personal: Specifies the SSH key to use.
  • HostName github.com: The real hostname of GitHub.

Now, Git will use the correct key automatically.


5. Adding SSH Keys to GitHub / GitLab

Each Git service requires adding your public key for authentication.

Get the Public Key

To display the public key:

cat ~/.ssh/id_ed25519_work.pub

Copy the key and add it to GitHub / GitLab / Bitbucket under:

  • GitHub → Settings → SSH and GPG keys
  • GitLab → Profile → SSH Keys
  • Bitbucket → Personal Settings → SSH Keys

6. Cloning Repositories Using Multiple Accounts

When cloning a repository, use the custom alias instead of github.com:

# For personal account:
git clone git@github-personal:yourusername/personal-repo.git

# For work account:
git clone git@github-work:yourworkuser/work-repo.git

7. Testing SSH Connections

Verify that SSH authentication is working:

ssh -T git@github-personal
ssh -T git@github-work

Expected output:

Hi yourusername! You've successfully authenticated...

If you see a permission error, ensure the correct key is added to the SSH agent (ssh-add -l).


8. Fixing Common Issues

1. SSH Key Not Used Correctly

Run:

ssh -vT git@github-personal

If you see Permission denied (publickey), make sure:

  • The correct SSH key is added to the SSH agent.
  • The key is correctly configured in ~/.ssh/config.

2. Wrong Host in Git Remote URL

Check the remote URL:

git remote -v

If it shows github.com, update it:

git remote set-url origin git@github-work:yourworkuser/work-repo.git

3. Too Many Authentication Failures

If you have multiple SSH keys and face authentication failures, specify the identity explicitly:

ssh -i ~/.ssh/id_ed25519_work -T git@github.com

9. Advanced: Using Different Git Configurations Per Account

If you want different Git usernames and emails for each account:

git config --global user.name "Personal Name"
git config --global user.email "personal@example.com"

For work repos:

git config --local user.name "Work Name"
git config --local user.email "work@example.com"

This ensures commits from work and personal accounts are correctly attributed.


Final Thoughts

By configuring multiple SSH keys, you can seamlessly work with different Git accounts without switching credentials manually. Understanding SSH authentication helps prevent conflicts and ensures a smooth development workflow.

Happy coding! 🚀