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!

Advertisements

About Vikas Hazrati

Vikas is the Founding Partner @ Knoldus which is a group of software industry veterans who have joined hands to add value to the art of software development. 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). To know more, send a mail to hello@knoldus.com or visit www.knoldus.com
This entry was posted in Agile, Architecture. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s