🚀 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/
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
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 "[email protected]" -f ~/.ssh/id_rsa_example
👉 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
📌 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
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
📌 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.
- Copy the SSH key:
cat ~/.ssh/id_rsa_example.pub
Go to GitHub → Settings → SSH and GPG keys → New SSH Key
🔗 GitHub SSH Key SettingsPaste the key and give it a title (e.g.,
Example Account
).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 [email protected]:your-username/your-repo.git
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 [email protected]:your-username/your-repo.git
To verify your remote settings:
git remote -v
🔄 Step 6: Check SSH Authentication
To confirm that Git is using the correct SSH key, run:
ssh -T [email protected]
✅ Expected output:
Hi your-username! You've successfully authenticated, but GitHub does not provide shell access.
If you see a different username, check your SSH config and re-run:
ssh-add ~/.ssh/id_rsa_example
🔧 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
❌ 2. Wrong GitHub Account Showing?
Run:
ssh -T [email protected]
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
🔁 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 [email protected]:your-username/your-repo.git
git remote -v # Verify
🔑 4. Using HTTPS Instead of SSH?
If you're accidentally using HTTPS, update your repository to SSH:
git remote set-url origin [email protected]:your-username/your-repo.git
🔄 5. Refresh GitHub Authentication
If you're facing authentication issues, refresh your GitHub token:
gh auth refresh -h github.com -s repo
If that doesn’t work, log out and log in again:
gh auth logout
gh auth login
🎯 Final Verification
After following these steps, run:
git push origin main
✅ 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! ⭐🚀