Skip to content

Add Multiple ssh keys in one machine

Published: at 10:25 AM

⏰ 2 min read

railway

Photo by Anton Darius on Unsplash

In this post i will explain how to add one or more ssh keys in one machine, use one for personal use and the others for work. I will explain it for gihub but the steps can be applicable for any git providers.

Before we start you have to install git first.

View exist ssh keys

$ ls -al ~/.ssh

Generate new ssh key

After running this command it will generate two file public/private keys

$ cd ~/.ssh
$ ssh-keygen -t rsa -f "id_rsa_personal_github"    # for personal account
$ ssh-keygen -t rsa -f "id_rsa_work_github"         # for work account

Adding the new SSH key to the corresponding (GitHub, Bitbuckt or Gitlab) account

$ clip < ~/.ssh/id_rsa_personal_github.pub          # for github account
$ clip < ~/.ssh/id_rsa_work_github.pub               # for gitlab account
  1. Go to Settings
  2. Select SSH and GPG keys from the menu to the left.
  3. Click on New SSH key, provide a suitable title, and paste the key in the box below
  4. Click Add key — and you’re done!

Registering the new SSH Keys with the ssh-agent

# start the ssh-agent in the background
$ eval $(ssh-agent -s)
Agent pid 59566

$ ssh-add ~/.ssh/id_rsa_personal_github    # for personal account
$ ssh-add ~/.ssh/id_rsa_work_github        # for work account

Creating the SSH config file

Using this file to tell git installed on your machine what key to use when pushing to upstream.

cd ~/.ssh/
$ touch config           # Create the file if not exists
$ code config            # Open the file in VS code or use any editor

config file

# Personal GitHub Account
Host github.com
   HostName github.com
   User git
   IdentityFile ~/.ssh/id_rsa_personal_github
# Work GitLab Account
Host gitLab.com
   HostName gitLab.com
   User git
   IdentityFile ~/.ssh/id_rsa_work_github

Resources