Monday, October 25, 2021

[SOLVED] How to generate multiple SSH public key and configure those on windows machine from gitbash?

Issue

Let's first describe the problem that I've faced:

  • I've two PC, one of them is the Windows machine and the other one is the Linux machine.

  • I want to generate two SSH public key on both of these machines. One is for my GitHub account and the other one is for my GitLab account.

  • I have used the following commands to generate the public SSH key which is perfectly work in my Linux machine but not in the Windows machine.

Commands:

Step 1: Generate SSH Key

ssh-keygen

Step 2: Write the directory and file name where I want to save the SSH key.

[ For my case, I've inserted the windows path in below ]

/c/Users/PC_USER_NAME/.ssh/id_rsa_hub

Step 3: Get the public key from the file

cat ~/.ssh/id_rsa_hub.pub

These steps perfectly work on my Linux machine but on Windows, I saw the authentication error. How can I configure the SSH public key's?


Solution

For the windows machine, you have to do one more configuration. Just follow the steps in below (if you're using the Git Bash):

  1. Go to the .ssh directory /c/Users/PC_USER_NAME/.ssh/, click right mouse button and choose "Git Bash Here"
  2. Create a file named "config" with the following command:
touch config
  1. Now open the config file with the command:
nano config
  1. Now write the following lines inside the config file

Let's assume you've created two files named id_rsa_hub for Github and id_rsa_lab for GitLab

# GITHUB
Host github.com
   HostName github.com
   PreferredAuthentications publickey
   IdentityFile ~/.ssh/id_rsa_hub

# GITLAB
Host gitlab.com
   HostName gitlab.com
   PreferredAuthentications publickey
   IdentityFile ~/.ssh/id_rsa_lab


Answered By - Fatema Tuz Zuhora