DEV Community

Cover image for Multiple SSH id
Matt Miller
Matt Miller

Posted on

Multiple SSH id

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:

  1. 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"
Enter fullscreen mode Exit fullscreen mode

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.

  1. 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
Enter fullscreen mode Exit fullscreen mode
  1. 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 the IdentityFile 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
Enter fullscreen mode Exit fullscreen mode

In this example, github-secondaccount is a custom alias for the second GitHub account to avoid conflicts with the default github.com.

  1. 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.

  2. 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
Enter fullscreen mode Exit fullscreen mode

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)

Collapse
 
ccoveille profile image
Christophe Colombier

If you want to go further I recommend you this article

dev.to/mbgeorge48/managing-multipl...