Using multiple SSH keys with GitHub allows you to manage access to multiple GitHub accounts or repositories from the same computer or development environment. Here's how you can set it up:
-
Generate SSH Keys: First, generate SSH keys for each GitHub account or repository you want to access. You can generate SSH keys using the
ssh-keygen
command in your terminal or command prompt. Make sure to specify a unique name and location for each SSH key pair.
ssh-keygen -t rsa -b 4096 -C "your-email@example.com"
When prompted, specify a unique filename for the SSH key pair (e.g., id_rsa_github1
, id_rsa_github2
, etc.) and optionally set a passphrase for added security.
-
Add SSH Keys to SSH Agent: Use the
ssh-add
command to add each SSH private key to the SSH agent. This allows the SSH agent to manage your SSH keys and automatically use them when authenticating with GitHub.
ssh-add ~/.ssh/id_rsa_github1
ssh-add ~/.ssh/id_rsa_github2
-
Configure SSH Config File: Create or edit the SSH config file (
~/.ssh/config
) to specify which SSH key to use for each GitHub host. You can use theIdentityFile
directive to specify the path to the SSH private key.
# GitHub account 1
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_github1
# GitHub account 2
Host github-secondaccount
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_github2
In this example, github-secondaccount
is a custom alias for the second GitHub account to avoid conflicts with the default github.com
.
Add SSH Public Keys to GitHub: Copy the contents of each SSH public key (
id_rsa_github1.pub
,id_rsa_github2.pub
, etc.) and add them to the SSH keys section in the GitHub account settings.Test SSH Connection: Finally, test the SSH connection to GitHub to ensure everything is set up correctly.
ssh -T git@github.com
ssh -T git@github-secondaccount
You should see a success message confirming the connection to GitHub.
With these steps, you can use multiple SSH keys to authenticate with GitHub and access repositories associated with different GitHub accounts or organizations from the same computer or development environment.
Top comments (1)
If you want to go further I recommend you this article
dev.to/mbgeorge48/managing-multipl...