DEV Community

Mayank Tamrkar
Mayank Tamrkar

Posted on

How to add multiple ssh keys for github

πŸš€ Managing Multiple SSH Keys for GitHub

Managing multiple SSH keys is useful when working with multiple GitHub accounts (e.g., work and personal). This guide will show you how to:

βœ… Check existing SSH configuration

βœ… Generate multiple SSH keys

βœ… Configure SSH for different accounts

βœ… Clone repositories and set remotes

βœ… Authenticate and troubleshoot issues

Let's get started! πŸš€


πŸ” Step 1: Check Existing SSH Configuration

Before generating new keys, check if you already have an SSH key:

ls ~/.ssh/
Enter fullscreen mode Exit fullscreen mode

If you see files like id_rsa and id_rsa.pub, that means you already have an SSH key set up. To view your existing SSH config:

cat ~/.ssh/config
Enter fullscreen mode Exit fullscreen mode

If you don’t have an SSH config file yet, don’t worryβ€”we’ll create one! πŸŽ‰


πŸ›  Step 2: Generate a New SSH Key for an Additional GitHub Account

To create a second SSH key (e.g., id_rsa_example):

ssh-keygen -t rsa -b 4096 -C "your-email@example.com" -f ~/.ssh/id_rsa_example
Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ Press Enter for all prompts (unless you want a passphrase for extra security).

After that, add your new SSH key to the SSH agent:

eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_rsa_example
Enter fullscreen mode Exit fullscreen mode

πŸ“Œ Step 3: Configure SSH for Multiple Accounts

Now, let's configure SSH so Git knows which key to use for different GitHub accounts.

Edit the SSH Config File

Run:

nano ~/.ssh/config
Enter fullscreen mode Exit fullscreen mode

Add the following entries for multiple accounts:

Host github.com
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa  # Default key

Host github.com-example
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa_example

Host github.com-example2
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa_example2
Enter fullscreen mode Exit fullscreen mode

πŸ“Œ Save and exit: Press CTRL + X, then Y, and hit Enter.


πŸ”‘ Step 4: Add the SSH Key to GitHub

Now, add your new SSH key to GitHub.

  1. Copy the SSH key:
   cat ~/.ssh/id_rsa_example.pub
Enter fullscreen mode Exit fullscreen mode
  1. Go to GitHub β†’ Settings β†’ SSH and GPG keys β†’ New SSH Key

    πŸ”— GitHub SSH Key Settings

  2. Paste the key and give it a title (e.g., Example Account).

  3. Click Add SSH Key.


πŸš€ Step 5: Clone a Repository Using the New SSH Key

When cloning a repository, use the Host alias from your SSH config:

git clone git@github.com-example:your-username/your-repo.git
Enter fullscreen mode Exit fullscreen mode

This tells Git to use id_rsa_example instead of the default id_rsa.

If you already cloned a repository using HTTPS, change the remote to SSH:

cd your-repo

git remote set-url origin git@github.com-example:your-username/your-repo.git
Enter fullscreen mode Exit fullscreen mode

To verify your remote settings:

git remote -v
Enter fullscreen mode Exit fullscreen mode

πŸ”„ Step 6: Check SSH Authentication

To confirm that Git is using the correct SSH key, run:

ssh -T git@github.com-example
Enter fullscreen mode Exit fullscreen mode

βœ… Expected output:

Hi your-username! You've successfully authenticated, but GitHub does not provide shell access.
Enter fullscreen mode Exit fullscreen mode

If you see a different username, check your SSH config and re-run:

ssh-add ~/.ssh/id_rsa_example
Enter fullscreen mode Exit fullscreen mode

πŸ”§ Troubleshooting Common Issues

🚫 1. SSH Key Not Working?

Try refreshing SSH authentication:

ssh-add -D  # Remove all SSH keys
ssh-add ~/.ssh/id_rsa_example  # Add the correct key
Enter fullscreen mode Exit fullscreen mode

❌ 2. Wrong GitHub Account Showing?

Run:

ssh -T git@github.com-example
Enter fullscreen mode Exit fullscreen mode

If the wrong account appears, update your ~/.ssh/config file and restart SSH:

ssh-agent -k  # Stop SSH agent
exec ssh-agent bash  # Restart SSH agent
ssh-add ~/.ssh/id_rsa_example
Enter fullscreen mode Exit fullscreen mode

πŸ” 3. Remove and Re-add a Remote

If your Git remote URL is incorrect or broken, remove and re-add it:

git remote remove origin
git remote add origin git@github.com-example:your-username/your-repo.git
git remote -v  # Verify
Enter fullscreen mode Exit fullscreen mode

πŸ”‘ 4. Using HTTPS Instead of SSH?

If you're accidentally using HTTPS, update your repository to SSH:

git remote set-url origin git@github.com-example:your-username/your-repo.git
Enter fullscreen mode Exit fullscreen mode

πŸ”„ 5. Refresh GitHub Authentication

If you're facing authentication issues, refresh your GitHub token:

gh auth refresh -h github.com -s repo
Enter fullscreen mode Exit fullscreen mode

If that doesn’t work, log out and log in again:

gh auth logout
gh auth login
Enter fullscreen mode Exit fullscreen mode

🎯 Final Verification

After following these steps, run:

git push origin main
Enter fullscreen mode Exit fullscreen mode

βœ… If it works, you're all set!


πŸŽ‰ Conclusion

Now you know how to:

  • Manage multiple SSH keys
  • Configure SSH for different GitHub accounts
  • Clone repositories with specific SSH keys
  • Troubleshoot common SSH & GitHub issues

πŸ”Ή If you found this helpful, star this repo and share it with others! β­πŸš€


Top comments (0)