Good content takes time and effort to come up with.

Please consider supporting us by just disabling your AD BLOCKER and reloading this page again.







Managing Multiple GIT Key Pairs Or Account In The Same Computer


GIT GITHUB SSH KEYS SSH CONFIG SSH UBUNTU PUBLIC KEY SECURITY PRIVATE KEY
20th April 2020 7 mins read
Share Article     Share On WhatsApp   Buy Me Coffee | StackCoder Buy Coffee


Now days you might working for more than 1 companies or you will might be having your personal GITHUB account and company GITHUB account where you want to manage multiple GIT key pairs in your computer without affecting the other accounts.


Prerequisites


I hope you know how to generate the SSH key pairs with ssh-keygen in your computer. If your new to it then I have written an article on it How To Generate SSH Key with ssh-keygen In Linux / Unix.


Step 1 - Generate Multiple SSH Keys With ssh-keygen


To generate new public and private key pair run the following command. Make sure you don't share id_rsa key with anyone and remember to keep it safe.


ssh-keygen -t rsa -b 4096 -C "company_email@gmail.com"


-t - Type of the key you want to generate RSA, DSA. But RSA is very secure so better stick with it.

-b - Number of bits then key needs to be generated. The higher the bits the more its secure. Keep 4096 bits its very secure one.

-C - comments


When you add the above command you will get the following confirmation


ssh-keygen -t rsa -b 4096 -C "company_email@gmail.com"

Generating public/private rsa key pair.

Enter file in which to save the key (/Users/channaveer/.ssh/id_rsa):


Make sure to cross verify the path and be-careful with the following points:

  1. If your already having id_rsa file then it will overwrite that if you proceed and you wont be able to get back the older keys.
  2. Good thing is you can rename with the alternate name


ssh-keygen -t rsa -b 4096 -C "company_email@gmail.com"

Generating public/private rsa key pair.

Enter file in which to save the key (/Users/channaveer/.ssh/id_rsa):


Since I have generate the SSH keys for the first time I just pressed enter.


Now it will ask you to enter passphrase and confirm the passphrase. If you want you can leave it empty. Its like one level security added before proceeding.


ssh-keygen -t rsa -b 4096 -C "company_email@gmail.com"

Generating public/private rsa key pair.

Enter file in which to save the key (/Users/channaveer/.ssh/id_rsa):

Enter passphrase (empty for no passphrase):

Enter same passphrase again: 


After confirm the passphrase then it will generate random art image. So the following is the complete process in one glance


ssh-keygen -t rsa -b 4096 -C "company_email@gmail.com"

Generating public/private rsa key pair.

Enter file in which to save the key (/Users/channaveer/.ssh/id_rsa):

Enter passphrase (empty for no passphrase):

Enter same passphrase again: 

Your identification has been saved in id_rsa.

Your public key has been saved in id_rsa.pub.

The key fingerprint is:

SHA256:sqWiJ1O0KQuj4LfzTlXHjFNjgsxVXx4pe7oeg0Vd1dw company_email@example.com

The key's randomart image is:

+---[RSA 4096]----+

     o oo.=   +B|

|       +  B o.+.E|

|         + + ooo |

   .   . o .. . |

|   . o..S    .o  |

|+ . + .=    o.   |

|+o +..o    . o.  |

|..++o.       .o  |

 o==o      ..   |

+----[SHA256]-----+


This creates id_rsa & id_rsa.pub key in ~/.ssh path.


I think this was for your main company SSH key. Similarly you can create for your personal, just remember when it asks for
Enter file in which to save the key (/Users/channaveer/.ssh/id_rsa): id_rsa_personal
This creates id_rsa_personal & id_rsa_personal.pub keys for personal usage.
Similarly you can use the same for other accounts too.



Step 2 - Add SSH Key To ssh-agent


To add SSH key to ssh-agent use the following command


eval "$(ssh-agent -s)"


You get the following output as:

Agent pid 27796


Now its time to add SSH private key to ssh-agent with the following. If you had generated for the 1st time then replace id_rsa. If you have given any other name in the place of id_rsa then replace in the following:


ssh-add ~/.ssh/id_rsa_personal


Now you have added the SSH keys for your company and personal account. Repeat the Step 2 if you have multiple accounts.


If you do ls -la ~/.ssh the you will see the list of all you SSH keys, like the following


ls -la ~/.ssh
-rw------- 1 wifidabba staff  3326 Jan 7 13:13 id_rsa
-rw-r--r-- 1 wifidabba staff  750 Jan 7 13:14 id_rsa.pub
-rw------- 1 wifidabba staff  1876 Mar 12 13:30 id_rsa_channaveer
-rw-r--r-- 1 wifidabba staff  405 Mar 12 13:30 id_rsa_channaveer.pub

Step 3 - Add SSH keys to ~/.ssh/config File


Now you have created multiple SSH keys. Its time to configure all of those. If you don't have config file inside ~/.ssh/ folder then create one with the touch command


touch ~/.ssh/config


By default you may have the following configuration in ~/.ssh/config file


Host *
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/id_rsa


Then comment it and add the following configuration file, here we have configured Company and Personal account details.


Make sure to change company_git_account_user_name with your company github account name


Remember HOST for later usage in your projects in Step 5.
github.com - For Company Project
github.com-channaveer - For Personal Project


#Host *
# AddKeysToAgent yes
# UseKeychain yes
# IdentityFile ~/.ssh/id_rsa

#Company File Configuration
Host github.com
    HostName github.com
    User git
    AddKeysToAgent yes
    UseKeychain yes
    IdentityFile ~/.ssh/id_rsa
    IdentitiesOnly yes
    User company_git_account_user_name


#Personal
Host github.com-channaveer
    HostName github.com
    User git
    AddKeysToAgent yes
    UseKeychain yes
    IdentityFile ~/.ssh/id_rsa_channaveer
    IdentitiesOnly yes
    User channaveer


I will explain what each and every line does


#Name which you will later use while adding GIT account
Host github.com
    
    #Host name, here its github.com, you can have bitbucket / gitlab
    HostName github.com
    User git
    
    #Tells whether you have added keys to your working terminal
    AddKeysToAgent yes
    
    #This option allows MAC Os user to store the PASSPHRASE key to remember in terminal
    UseKeychain yes
   
    #When you add remote github account then which file it has to verify with
    IdentityFile ~/.ssh/id_rsa 
    
    #Specifies that ssh should only use the authentication identity files configured in the ssh_config files, even if ssh-agent offers more identities. The argument to this keyword must be 'yes' or 'no'
    IdentitiesOnly yes
    User company_git_account_user_name

Step 4 - Add GIT Project With Respective GIT Keys


Okay I have generate all these keys whats next? You need to set company and personal project keys else when you commit in you personal project you might see company username in commit history instead of personal name.


GIT Global Username & Email


By default you can configure Company account details with global account in your laptop. So that whenever you add or create a project by default the SSH will take with company details. You can do it as follows


git config  --global user.email "compnay_email@gmail.com"

git config --global user.name "Channaveer Hakari"


GIT project level Username & Email


You can configure this for your project level with the following , here you can add your personal email and name.


git config user.email "channaveer_email@gmail.com"

git config user.name "Channaveer Hakari"

Step 5 - Getting GIT Keys Setup In Projects


Remember the HOST name that I had highlighted in Step 3 . For the below example I am using same GITHUB project url


Company Project Setup (Use GIT SSH URL)


First go to the project directory here my project is in /var/www/html/database_backup, then initialize the project with git init then add the your company remote github project SSH URL which will be like the following


git@github.com:channaveer/DatabaseBackup-with-PHP-MySQLi.git


Remember this key git@github.com in the above URL this was set in HOST of ~/.ssh/config, github.com is for your company project.


cd /var/ww/html/database_backup
git init
git remote add origin git@github.com:channaveer/DatabaseBackup-with-PHP-MySQLi.git


Now your all set to do your first commit. This takes your globally set username when you commit your changes


git config --global user.email "compnay_email@gmail.com"


Personal Project Setup (Use GIT SSH URL)


First go to the project directory here my project is in /var/www/html/database_backup, then initialize the project with git init then add the your company remote github project SSH URL which will be like the following


git@github.com-channaveer:channaveer/DatabaseBackup-with-PHP-MySQLi.git


Remember to github.com-channaveer in the above URL this was set in HOST of ~/.ssh/config, github.com-channaveer is for your personal project.


cd /var/ww/html/database_backup
git init
git remote add origin git@github.com-channaveer:channaveer/DatabaseBackup-with-PHP-MySQLi.git


Now your all set to do your first commit. This takes your globally set username when you commit your changes


git config --global user.email "compnay_email@gmail.com"



Conclusion


Congrats guys you have made it this long way. Hope you guys have enjoyed the same way I have enjoyed writing this article.


Hope it helped you. Happy coding :)








Author Image
AUTHOR

Channaveer Hakari

I am a full-stack developer working at WifiDabba India Pvt Ltd. I started this blog so that I can share my knowledge and enhance my skills with constant learning.

Never stop learning. If you stop learning, you stop growing