DEV Community

loading...

Add Multiple ssh keys in one machine

Islam Muhammad
Senior Software Engineer, Reader πŸ“–., blogger ✍, explorer πŸ—Ί , and a geek who is passionate about the latest technology and web development πŸ‘¨β€πŸ’»
・3 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 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

Discussion (0)