Working with Multiple ssh Keys and Multiple Git Repositories

With multiple, git hosting repositories available and lots of coding being done, you end in situations like the subject of this email. Say, for example you have 2 accounts on BitBucket, you would not be allowed to use the same ssh key across both the accounts. The idea is to have

  1. Multiple SSH keys, and
  2. Mapping those keys to individual accounts

Let us see how you would be able to go about it

vikas@vikas-Vostro-3560:~/.ssh$ ssh-keygen -t rsa -C "email@example.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/home/vikas/.ssh/id_rsa): some-temp
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in some-temp.
Your public key has been saved in some-temp.pub.

Now, in your /home/vikas/.ssh folder, you would get 2 keys, one called some-temp.pub(which is the public key) and the private key some-temp. The idea is to show that you can generate multiple ssh keys depending on your need.

Next, you would create a “config” file, which would map your alias with the right username and the ssh key. For example, if I have 2 accounts on BitBucket and 1 on GitHub then my config file would look something like this

vikas@vikas-Vostro-3560:~/.ssh$ vim config
Host bit-vhazrati bitbucket.org
Hostname bitbucket.org
IdentityFile ~/.ssh/bitbucket_id_rsa
User vhazrati

Host bit-vikashazrati bitbucket.org
Hostname bitbucket.org
IdentityFile ~/.ssh/id_rsa
User vikashazrati

Host git-vikashazrati github.com
Hostname github.com
IdentityFile ~/.ssh/id_rsa
User vhazrati

Once, we have the config file in place, the system understands which key is assigned to which account. So in my config file example above, user “vhazrati” on BitBucket has the “~/.ssh/bitbucket_id_rsa” mapped to it and user “vikashazrati” on BitBucket has the “~/.ssh/id_rsa” key mapped to it.

Now if you want to clone a repository with user “vhazrati”, you would do

vikas@vikas-Vostro-3560:~/.ssh$ git clone git@bit-vhazrati:vhazrati/knoldus-website.git

and to clone from the other account, you would do

git clone git@bit-vikashazrati:knoldussoftware/trade-router.git

Enjoy!

Written by 

Vikas is the CEO and Co-Founder of Knoldus Inc. Knoldus does niche Reactive and Big Data product development on Scala, Spark, and Functional Java. Knoldus has a strong focus on software craftsmanship which ensures high-quality software development. It partners with the best in the industry like Lightbend (Scala Ecosystem), Databricks (Spark Ecosystem), Confluent (Kafka) and Datastax (Cassandra). Vikas has been working in the cutting edge tech industry for 20+ years. He was an ardent fan of Java with multiple high load enterprise systems to boast of till he met Scala. His current passions include utilizing the power of Scala, Akka and Play to make Reactive and Big Data systems for niche startups and enterprises who would like to change the way software is developed. To know more, send a mail to hello@knoldus.com or visit www.knoldus.com

Leave a Reply

%d bloggers like this: