Setting up multiple SSH keys for different GitHub accounts on the same machine is useful if you manage multiple GitHub accounts, such as a personal account and a work account. Here are the steps to configure SSH keys for different GitHub accounts on a single computer:
1. Generate SSH Keys
First, you need to generate a unique SSH key for each GitHub account. Open your terminal and run:
ssh-keygen -t ed25519 -C "your_email@example.com"
When prompted to “Enter a file in which to save the key,” specify a unique path for each key:
# For your first GitHub account
/home/yourusername/.ssh/id_ed25519_github_account1
# For your second GitHub account
/home/yourusername/.ssh/id_ed25519_github_account2
Repeat this for each account, changing the email and file path each time.
2. Add SSH Keys to the SSH Agent
Start the ssh-agent in the background and add your SSH private keys to it:
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519_github_account1
ssh-add ~/.ssh/id_ed25519_github_account2
3. Upload Public Keys to GitHub
For each GitHub account, you need to upload the corresponding public key. You can get the public key using:
cat ~/.ssh/id_ed25519_github_account1.pub
Copy the output and then:
- Log into your GitHub account.
- Go to Settings > SSH and GPG keys.
- Click on “New SSH key”, paste your public key into the field, and save it.
Repeat this for each account.
4. Configure SSH Config File
To make it easy to use different keys with different GitHub accounts, you can set up an SSH config file. Edit or create the config file in your .ssh
directory:
nano ~/.ssh/config
Add a configuration block for each GitHub account in the file:
# GitHub Account 1
Host github.com-account1
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_github_account1
IdentitiesOnly yes
# GitHub Account 2
Host github.com-account2
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_github_account2
IdentitiesOnly yes
5. Use SSH Keys
When you clone a repository, use the alias you set in your SSH config file to specify which account and key to use. For example:
git clone git@github.com-account1:username1/repository.git
Replace username1/repository.git
with the actual user and repository you wish to clone from the first account. Use the corresponding alias for other accounts.
6. Check SSH Key Usage
To verify which key is being used with GitHub, you can run:
ssh -T git@github.com-account1
This should return a message that you’ve successfully authenticated, but GitHub does not provide shell access.
This setup ensures that you can work with multiple GitHub accounts from the same machine without key conflicts.