DEV Community

Cover image for Multiple SSH id
Matt Miller
Matt Miller

Posted on • Edited on

1

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.


Enjoying the content? If you'd like to support my work and keep the ideas flowing, consider buying me a coffee! Your support means the world to me!

Buy Me A Coffee

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

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

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay