If you are working with multiple Github/Bitbucket accounts using ssh then you may face this problem, that you can’t use same ssh key on another account.
I face this problem everytime i reinstall operating system on my computer. And i have to google to find way to fix this problem. So i’m writing this to help me and you also.
So basically we will generate multiple ssh keys on single computer and merge those all ssh keys in Config file. And that config file will behave like bridge to all bitbucket/github account and deliver ssh key to those accounts accordingly.
ssh-keygen -t rsa
ssh-add ~/.ssh/id_rsa
id_rsa
and id_rsa.pub
. 1st one is the secret key and second one is public key.Let say if you are working with 3 company account and 1 is your personal account. So add your default ssh key to your personal account. And create 3 new ssh keys for your 3 companies.
cd ~/.ssh
ssh-keygen -t rsa -C "company_name1" -f "company_name1"
ssh-add ~/.ssh/company_name1
cd ~/.ssh
ssh-keygen -t rsa -C "company_name2" -f "company_name2"
ssh-add ~/.ssh/company_name2
cd ~/.ssh
ssh-keygen -t rsa -C "company_name3" -f "company_name3"
ssh-add ~/.ssh/company_name3
config
file inside ~/.ssh
directory.nano ~/.ssh/config
This command will open config
file in command line editor and if this file doesn’t exist then it will create that file for you.
config
file, and modify according to your need.Host bitbucket.org
HostName bitbucket.org
IdentityFile ~/.ssh/id_rsa
Host companyname1.bitbucket.org
HostName bitbucket.org
IdentityFile ~/.ssh/company_name1
Host companyname2.bitbucket.org
HostName bitbucket.org
IdentityFile ~/.ssh/company_name2
Host companyname3.bitbucket.org
HostName bitbucket.org
IdentityFile ~/.ssh/company_name3
Host github.com
HostName github.com
IdentityFile ~/.ssh/id_rsa
Host companyname1.github.com
HostName github.com
IdentityFile ~/.ssh/company_name1
Host companyname2.github.com
HostName github.com
IdentityFile ~/.ssh/company_name2
Host companyname3.github.com
HostName github.com
IdentityFile ~/.ssh/company_name3
As you can see i’ve mapped company_name1
ssh key with account. Now add that company_name1.pub
’s ssh key on you companyname1.bitbucket.org
account.
I’m just giving dummy name, you can write anything which make sense to you and manageable for you.
Add key
and you are doneRepeat process for the next account, except this time copy the another company_name
Your personal project can be cloned same as before, but to clone project from any of the above company account you have to tweak then clone url.
Suppose if clone url of your company account looks like below,
git clone git@bitbucket.org:companyname/company-project.git
Then you have to modify this to look like below url (just add ssh key name which you have mapped in config file)
git clone git@company_name.bitbucket.org:companyName/company-project.git
You need to change only bold part.