How to Manage Multiple SSH Keys for Multiple GitHub Accounts on One Computer
Recently my son asked me how to manage multiple SSH keys for multiple GitHub accounts on one computer. He starts his journey as computer science student and he need to work with multiple GitHub accounts for his projects.
My first attempt was to find an article or ready solution to point him to. But I couldn't find a good one. So I decided to write this article to help him and other developers who’re facing the same issue.
Step-by-Step guide # Anchor link
1. Generate Two SSH Keys # Anchor link
If you haven’t already, generate two SSH keys for each GitHub account:
ssh-keygen -t ed25519 -C "[email protected]" -f ~/.ssh/id_ed25519_personal
ssh-keygen -t ed25519 -C "[email protected]" -f ~/.ssh/id_ed25519_edu
- The
-f
flag specifies the filename for the keys (e.g., id_ed25519_personal and id_ed25519_edu).
If you are using a legacy system that doesn't support the Ed25519 algorithm, use:
ssh-keygen -t rsa -b 4096 -C "[email protected]" -f ~/.ssh/id_rsa_personal ssh-keygen -t rsa -b 4096 -C "[email protected]" -f ~/.ssh/id_rsa_edu
2. Add Keys to the SSH Agent # Anchor link
Add both keys to your SSH agent:
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519_personal
ssh-add ~/.ssh/id_ed25519_edu
3. Update the SSH Config File # Anchor link
Edit (or create) your ~/.ssh/config file to define configurations for each GitHub account.
# GitHub Personal Account
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_personal
# GitHub Education Account
Host github.edu
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_edu
4. Use Configured Hosts in Git URLs # Anchor link
When cloning or working with Git repositories, use the corresponding host name defined in the SSH config:
For Personal account:
git clone [email protected]:username/repo.git
For Education account:
git clone [email protected]:username/repo.git
5. Test Your Configuration # Anchor link
Verify the setup by testing the SSH connection for each account:
ssh -T github.com
ssh -T github.edu
If the setup is correct, GitHub will identify each account and display a success message.
Tips # Anchor link
If you already cloned repositories using the default [email protected] format, update the origin remote URL to use the appropriate host:
git remote set-url origin [email protected]:username/repo.git
Make sure the SSH public keys (id_ed25519_personal.pub and id_ed25519_edu.pub) are added to the respective GitHub accounts in their settings.
Setup Git Global Configuration # Anchor link
To avoid conflicts, set up a global Git configuration for each account:
git config --global user.name "Personal Name"
git config --global user.email "[email protected]"
then you need to update the global configuration for the second account:
touch ~/.gitconfig_edu
echo "[user]" >> ~/.gitconfig_edu
echo " name = Student Name" >> ~/.gitconfig_edu
echo " email = [email protected]" >> ~/.gitconfig_edu
and then you need update the global configuration file ~/.gitconfig
to include the second configuration file:
echo "[includeIf \"gitdir:~/path/to/your/education/**\"]" >> ~/.gitconfig_edu
echo " path = ~/.gitconfig_edu" >> ~/.gitconfig_edu
Note: for Windows users ruleset
includeIf
could be different.echo "[includeIf \"gitdir/i:C:/path/to/your/education/**\"]" >> ~/.gitconfig_edu
This setup allows you to:
- Use the correct name and email for each account.
- Automatically switch between configurations based on the repository path.
- Keep your global configuration clean and organized.
How it works # Anchor link
When you run a Git command in a repository located in the ~/path/to/your/education/
directory, Git will use the configuration from the ~/.gitconfig_edu
file.
In all other cases, Git will use the default global configuration.
Testing the Configuration # Anchor link
To confirm the correct profile is being applied in a specific directory:
git config --get user.name
git config --get user.email
Important notes:
- The included files (e.g.,
~/.gitconfig_edu
) must be valid Git configuration files.- Make sure to replace
~/path/to/your/education/**
with the actual path to your education repositories.- If a directory doesn’t match any includeIf condition, the global configuration will be used.
That’s it! You’ve successfully set up and managed multiple SSH keys for multiple GitHub accounts on one computer.
May the 4th be with you,
Alex