DEV Community

Islam Muhammad
Islam Muhammad

Posted on

Add Multiple ssh keys in one machine

![railway](https://images.unsplash.com/photo-1464062198447-e6e5c40c43c5?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=1394&q=80) 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 your personal acount and the others for work. I will explain it for gihub but the steps can be applicable for any git providers out there.

What is SSH?

SSH or Secure Shell is a network communication protocol that enables two computers to communicate (c.f http or hypertext transfer protocol, which is the protocol used to transfer hypertext such as web pages) and share data. An inherent feature of ssh is that the communication between the two computers is encrypted meaning that it is suitable for use on insecure networks.

SSH is often used to "login" and perform operations on remote computers but it may also be used for transferring data.

How it is work?

When you set up SSH key, you create a key pair that contains a private key (saved to your local computer) and a public key (uploaded to Git Providers like github, gitlab or bitbucket). The git provider uses the key pair to authenticate anything the associated account can access. This two-way mechanism prevents man-in-the-middle attacks.

How to setup one in my machine?

In simple steps without much detail 😊.

View exist ssh keys

$ ls -al ~/.ssh
Enter fullscreen mode Exit fullscreen mode

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

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

  • Copy generated key(s) to clipboard
$ clip < ~/.ssh/id_rsa_personal_github.pub          # for github account
$ clip < ~/.ssh/id_rsa_work_github.pub               # for gitlab account
Enter fullscreen mode Exit fullscreen mode
  • Add clipboard to github accounts:
  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
Enter fullscreen mode Exit fullscreen mode

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

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

Resources

Top comments (0)