W3Dudes

Using SSH Keys with Multiple GitHub Account (Windows, Linux/Unix and OSX)

by W3 Dudes 16 October 2021, 08:00

A brief explanation about how to manage multiple SSH keys for accessing different GitHub accounts and projects. With this blog you will be able to understand how to configure and access GitHub projects from different GitHub account (which has different credentials).

Pre-Requisite

Follow the below command to create public key in your machine (Use Git Bash if you are using windows operating system)


Implementation

1) Go to .ssh Directory

Go to the default .ssh directory in your machine. Based on the operating system the location should will be like below.

$ cd ~/.ssh/


2) Create directory if there is no .ssh directory exists in your machine

Some operating system might not have the .ssh directory by default. In that case we can create the directory by running below command. If you are using windows operating system make sure that you have enabled an view hidden files option to view dot (.) files

$ mkdir ~/.ssh


3) Excute ssh-keygen command

We need to run the ssh-keygen to create ssh public key for the machine. Make sure that you are on ~/.ssh/ directory before executing the command below.

Once you have executed the ssh-keygen command, the output will look something like below.

$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/w3dudes/.ssh/id_rsa): test
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in test
Your public key has been saved in test.pub
The key fingerprint is:
SHA256:XXXxxxxx0+xxx0xxxXXX0XX0xxxxxxX/xxXXXX1xX0x W3@Dudes
The key's randomart image is:
+---[RSA 3072]----+
|            .. .x|
|           .  .x.|
|          .   X .|
|         X   X X |
|        X = x = *|
|         * x = *+|
|        .   x X.*|
|       . x . =.Xx|
|      ..+..   ++*|
+----[SHA256]-----+


4) Create multiple ssh key by using above command.

You need to create multiple ssh key for multiple GitHub account as above (e.g. account1, account2, account3 etc,.).

The above example used test as a name for ssh key, you can use any name you want. But it is better to keep the account name as a ssh key name, so that we can identify the ssh-key with the name.



4) Create/Modify the ssh config

You need to create config file to use multiple github ssh account. Follow the procedure below to create config file.

$ cd ~/.ssh/
$ nano config


5) Add the below config based on your key

Copy paste the below content in your config file and modify the data accordingly.

Host github.com
  HostName github.com
  PreferredAuthentications publickey
  IdentityFile ~/.ssh/id_rsa
  User w3dudes    ->  your computer usename

Host github-w3dudes
  HostName github.com
  PreferredAuthentications publickey
  IdentityFile ~/.ssh/id_rsa_w3dudes
  User w3dudes    ->  your computer usename

Above config has two Host part. One is default github.com with identityfile as ~/.ssh/id_rsa. And other one is github-w3dudes with identityfile as ~/.ssh/id_rsa_w3dudes.

Likewise you can add multiple github accounts with multiple sshkey.

But there is a change in cloning the repo with above change implemented. Instead of cloaning with default command you need to use the command as below to clone the repo from different account

Normal github cloan behaviour will be like below

$ git@github.com:/Project.git

(e.g)
$ git@github.com:w3dudes/test.git


For the other github account cloan should be as below

The hostname after the git@ should be taken from config file which we have created from previous step.

$ git@github-w3dudes:/Project.git

(e.g)
$ git@github-w3dudes:w3dudes/test.git


Verdict

With this manner you can use multiple github account with ssh key in a single system.

0 comments