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! โญ๐Ÿš€


Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

AWS Q Developer image

Your AI Code Assistant

Automate your code reviews. Catch bugs before your coworkers. Fix security issues in your code. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

๐Ÿ‘‹ Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someoneโ€™s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay