Using GitHub with SSH on Linux with multiple accounts involves a few steps, including generating SSH keys, adding them to your GitHub accounts, configuring SSH to use the correct key for each account, and adjusting your Git repository configurations to use the appropriate SSH URL. Here's a step-by-step guide to help you manage multiple GitHub accounts on Linux:
1. Generate SSH Keys for Each Account
First, you need to generate SSH keys for each of your GitHub accounts. Use the ssh-keygen command to create a new SSH key. It's a good practice to give each key a unique filename to distinguish between them.
bash
# Generate SSH key for the first account
ssh-keygen -t rsa -b 4096 -C "email1@example.com"
# Save it to a unique filename, e.g., ~/.ssh/id_rsa_account1
# Generate SSH key for the second account
ssh-keygen -t rsa -b 4096 -C "email2@example.com"
# Save it to another unique filename, e.g., ~/.ssh/id_rsa_account2
2. Add SSH Keys to Your GitHub Accounts
Copy the Public Key: After generating the keys, you need to copy the public key for each account to your clipboard. You can do this by running:
bash
cat ~/.ssh/id_rsa_account1.pub
# Copy the output
cat ~/.ssh/id_rsa_account2.pub
# Copy the output
Add Public Keys to GitHub:
Log in to your first GitHub account.
Go to Settings > SSH and GPG keys > New SSH key.
Paste the public key you copied for this account.
Repeat this process for your second GitHub account using the second public key.
3. Configure SSH to Use the Correct Keys
Create/Edit SSH Config File: Open or create the ~/.ssh/config file in your favorite text editor.
Add Host Entries for GitHub: Add entries for each GitHub account using a different host alias. This tells SSH which key to use for which GitHub account.
Change the Remote URL: For each repository cloned, you need to adjust the remote URL to use the SSH host alias you've configured.
Navigate to your repository directory.
Check the current remote URL:
bash
git remote -v
Change the remote URL to use the appropriate SSH host alias:
bash
# For the first account
git remote set-url origin git@github-account1:username/repo.git
# For the second account
git remote set-url origin git@github-account2:username/repo.git
Verify: After changing the remote URL, you can verify that you're using the correct SSH key by attempting to push changes to your repository.
bash
git push origin main
# or your current branch
5. Test Your SSH Connection
To test if your SSH configuration is correct, you can use:
You should see a message indicating that you've successfully authenticated.
Troubleshooting
Permission Denied: If you encounter permission denied errors, ensure that your SSH keys are correctly added to your GitHub accounts and that the ~/.ssh/config file is correctly configured.
Key Already in Use: If you're told a key is already in use, generate a new key for the second account instead of reusing the first account's key.
By following these steps, you should be able to manage multiple GitHub accounts on Linux using SSH.