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.
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.
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.
In simple steps without much detail 😊.
$ ls -al ~/.ssh
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
- 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
- Add clipboard to github accounts:
- Go to Settings
- Select SSH and GPG keys from the menu to the left.
- Click on New SSH key, provide a suitable title, and paste the key in the box below
- Click Add key — and you’re done!
# 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
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
# 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