DEV Community

loading...

Multiple ssh keys in windows 10

arnob profile image Fahim Hoque ・4 min read

A lot of developers use windows as their daily driver and maintaining multiple version controls can prove to be a big burden. After looking into adding multiple ssh keys I've not seen a lot of good and explanatory tutorials out there. So after a lot of research I'm writing this article myself.

This entire venture will revolve around one single file called config

So let's get started?

Pre-requisites
You need to have git installed in your system

For this article I'll assume your name's John and you have 3 version controls.

  • GitHub
  • GitLab
  • BitBucket

Incase you're using some other version controls the process should be the same, but as we all know referring to the documentation is the best policy.

We'll start with adding ssh to github:

  1. Start Git Bash and type in the following command:

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

Let's see what is going on here,
ssh-keygen basically generates a ssh key of type(-t) rsa of 4096 bits. The -C stands for comment and you can type whatever you want to distinguish between your ssh keys

Some other type of encryptions are:

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
ssh-keygen -t ed25519 -C "your_email@example.com"

I would suggest using ed25519 for better security. One you've typed in the command you'll see a screen like below:

Alt Text

Just hit enter as this is the default location of ssh keys(id_rsa being the name of the file). The next screen will be something like below asking for passphrase :

Alt Text

You can just leave it by hitting enter(but having a passphrase is always better).

After hitting enter with/without a passphrase you'll get a randomart image of the key.

Now if you navigate to /C/Users/<username>/.ssh you'll see two files called id_rsa and id_rsa.pub. The id_rsa.pub is the public key you'll be adding to your version control.

You can open the .pub file in any text editor and copy the content there.

!important: Never ever copy your private key.

After you've copied your public file's content step 2 starts.

  1. Login to your github account and navigate to settings and to ssh keys:

Alt Text

Alt Text

There you'll see a new SSH key. Give your ssh key a title and paste the copied contents.

Alt Text

And click the Add SSH key. There you go you have your first ssh key added.

Now you can test your ssh connection using the following command:

ssh -T git@github.com

You may see something like this:

> The authenticity of host 'github.com (IP ADDRESS)' can't be established.
> RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
> Are you sure you want to continue connecting (yes/no)?
Enter fullscreen mode Exit fullscreen mode

Type in yes and you should see something like this:

> Hi john! You've successfully authenticated, but GitHub does not
> provide shell access.
Enter fullscreen mode Exit fullscreen mode

Now you can clone and push code from github using ssh

  1. Now comes the interesting part. Inside your .ssh folder open git bash and type in

notepad config

! please note that there's no extension to the config file

Sometimes the file will be created as config.txt you can simply fix that by typing mv config.txt config. It'll create a file called config and move all contents from config.txt to config

Now open the config file with any text editor and type in the followings:

#account- github
Host github.com
 HostName github.com
 IdentityFile ~/.ssh/id_rsa
Enter fullscreen mode Exit fullscreen mode

So now git knows config will automatically know that github's ssh key is inside id_rsa file.

  1. For the next step you might have to add ssh key to gitlab. Step 1 will remain same except the file name as we can't use the default name because that'll overwrite the contents within.

when prompted with poviding the name for ssh key bearing file type in

/c/Users/fhoqu/.ssh/id_rsa_gitlab

(Notice the _gitlab at the end?)

Keeping the file name relevant is important, so that you can understand it afterwards.

Copy the contents within id_rsa_gitlab.pub and repeat step 2 and go to you settings and add new ssh key.
For gitlab the ssh connection testing command is:

ssh -T git@gitlab.example.com

replace gitlab.example.com with your gitlab's domain instance

If everything is working fine you can go ahead bring modification to config file.

We have already defined the configuration files for github. Gitlab's configuration will be similar:

#account- codemaster
Host gitlab.com
 HostName gitlab.com
 IdentityFile ~/.ssh/id_rsa_gitlab
Enter fullscreen mode Exit fullscreen mode

So the entire config file will be something like this:

#account- github
Host github.com
 HostName github.com
 IdentityFile ~/.ssh/id_rsa

#account- codemaster
Host gitlab.com
 HostName gitlab.com
 IdentityFile ~/.ssh/id_rsa_gitlab
Enter fullscreen mode Exit fullscreen mode

There you go. You have configured and added multiple ssh keys for multiple version control systems. If you have multiple accounts in one version control system the entire process will be the same.

Please keep in mind anything can go wrong and the best way to troubleshoot is to read the documentation

Github's documentation on ssh key

GitLab's documentation on ssh key

N.B: this is my first post so any constructive feedback will be appreciated

Discussion (0)

Forem Open with the Forem app