To use GitHub with SSH on Linux for multiple accounts, follow these steps. This setup ensures each account uses its own SSH key, allowing you to commit to repositories under different identities.
Step 1: Generate SSH Keys for Each Account
-
Work Account:
ssh-keygen -t ed25519 -C "your-work-email@example.com" -f ~/.ssh/id_rsa_work
- Press
Enter for the default file path and add a secure passphrase (optional).
-
Personal Account:
ssh-keygen -t ed25519 -C "your-personal-email@example.com" -f ~/.ssh/id_rsa_personal
Step 2: Add Public Keys to GitHub
-
Copy the public keys to your clipboard:
- Work:
cat ~/.ssh/id_rsa_work.pub
- Personal:
cat ~/.ssh/id_rsa_personal.pub
-
Go to each GitHub account’s SSH settings:
Step 3: Configure SSH to Use Multiple Keys
Create or edit the ~/.ssh/config file to map each GitHub account to its SSH key:
nano ~/.ssh/config
Add the following entries (replace username with your GitHub username for each account):
# Work Account
Host github-work
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_work
# Personal Account
Host github-personal
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_personal
Step 4: Update Remote URLs in Repositories
Ensure your cloned repositories use the Host names from the SSH config. For example:
-
For a Work Repo:
git remote set-url origin git@github-work:your-work-username/repo-name.git
-
For a Personal Repo:
git remote set-url origin git@github-personal:your-personal-username/repo-name.git
Verify the remote URL with:
git remote -v
Step 5: Add SSH Keys to the Agent
-
Start the SSH agent (if not already running):
eval "$(ssh-agent -s)"
-
Add both keys to the agent:
ssh-add ~/.ssh/id_rsa_work
ssh-add ~/.ssh/id_rsa_personal
Step 6: Test SSH Connections
Check if SSH works for each account:
-
Work:
ssh -T git@github-work
Output: Hi [work-username]! You've successfully authenticated...
-
Personal:
ssh -T git@github-personal
Output: Hi [personal-username]! You've successfully authenticated...
Troubleshooting
If you still can’t commit:
- Check Remote URLs: Ensure the remote URL uses
github-work or github-personal.
- Verify SSH Agent: Run
ssh-add -l to confirm keys are loaded.
- Check Permissions: Ensure SSH files have correct permissions:
chmod 600 ~/.ssh/config
chmod 600 ~/.ssh/id_rsa_* # For private keys
- Debug SSH: Use
ssh -vvv git@github-work to see detailed logs.
Example Workflow
-
Commit to Work Repo:
cd /path/to/work-repo
git commit -m "Your message"
git push origin main
-
Commit to Personal Repo:
cd /path/to/personal-repo
git commit -m "Your message"
git push origin main
This setup ensures your commits are tied to the correct GitHub account. If you encounter issues, double-check the ~/.ssh/config syntax and remote URLs.