๐ 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 "your-email@example.com" -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 git@github.com-example: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 git@github.com-example: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 git@github.com-example
โ
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 git@github.com-example
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 git@github.com-example: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 git@github.com-example: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! โญ๐
Top comments (0)