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
- Multiple SSH keys, and
- 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 "firstname.lastname@example.org" 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