Managing multiple GitHub accounts (e.g., personal and company accounts) can be tricky, especially when it comes to using SSH keys. You might encounter scenarios like:
- Working on personal open-source projects alongside company repositories.
- Collaborating with multiple organizations.
- Keeping work and personal development separate for better management.
This guide walks you through setting up and configuring SSH keys to work seamlessly with multiple accounts.
Why Can't I Use the Same SSH Key for Both Accounts?
GitHub requires each SSH key to be associated with a single account for security reasons. If you try to upload the same key to multiple accounts, you'll get an error: "Key is already in use."
The solution? Use separate SSH keys for each account and configure your system to differentiate between them.
Step-by-Step Guide
- Check for Existing SSH Keys
Run the following command to see if you already have SSH keys:
ls ~/.ssh
Look for files like id_rsa and id_rsa.pub. You’ll need to generate new keys if you don't see any.
- Generate a New SSH Key for Your Personal Account
If you already have an SSH key for your company account, you’ll need to create a new one for your personal account:
ssh-keygen -t rsa -b 4096 -C "your-personal-email@example.com"
When prompted:
- File Location: Save the key with a unique name, e.g., ~/.ssh/id_rsa_personal:
Enter file in which to save the key (/Users/you/.ssh/id_rsa): ~/.ssh/id_rsa_personal
Passphrase: Add one for extra security (optional).
This will generate two files:
~/.ssh/id_rsa_personal (private key)
~/.ssh/id_rsa_personal.pub (public key)
- Add Your Public Key to GitHub
Each GitHub account must have its own SSH key. Upload the public key for your personal account:
For Your Personal Account:
- Copy the public key:
cat ~/.ssh/id_rsa_personal.pub
- Log in to your personal GitHub account.
- Go to Settings > SSH and GPG keys > New SSH key.
- Add a title (e.g., "Personal SSH Key") and paste the key.
- Click Add SSH key.
For Your Company Account:
If you haven’t already, ensure the key associated with your company account is uploaded similarly.
- Configure Your SSH Config File
To use the correct key for each account, update your SSH configuration file.
- Open the SSH config file:
nano ~/.ssh/config
- Add the following configuration:
Host github-personal
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_personal
IdentitiesOnly yes
Host github-company
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa
IdentitiesOnly yes
Host github-personal
and Host github-company
: These are aliases you’ll use when connecting to GitHub.
IdentityFile
: Specifies which SSH key to use for each account.
Save and exit the file.
- Test Your Configuration
Verify that the SSH keys are working for each account:
For Your Personal Account:
ssh -T github-personal
You should see:
Hi personal-username! You've successfully authenticated, but GitHub does not provide shell access.
For Your Company Account:
ssh -T github-company
You should see:
You've successfully authenticated, but GitHub does not provide shell access.
- Use the Correct Host When Cloning Repositories
When working with repositories, use the Host alias you defined:
For Personal Repositories:
git clone git@github-personal:username/repository.git
For Company Repositories:
git clone git@github-company:organization/repository.git
This ensures the correct SSH key is used for authentication.
You can easily manage multiple GitHub accounts on the same machine by creating separate SSH keys and configuring the ~/.ssh/config file. This approach avoids errors like "Key is already in use" and keeps your workflow seamless.
Happy coding!
(I've had this in my drafts for ages, decided to publish today finally)
Top comments (0)