Search code examples
gitsshbitbucketssh-keysssh-keygen

Cannot get multiple ssh keys to work with BitBucket using aliases


I have two bitbucket accounts, one for my personal stuff and one for work. My personal one was working fine and the one I use at work also works fine; however I wanted to be able to connect to the work one from home but I am having trouble getting it to work.

My files are called:

## Unrelated ones I use for plan.io that still work

id_rsa
id_rsa.pub

## Personal one for bitbucket I just created because of seperation

homeid
homeid.pub

## Work one for bitbucket

workid
workid.pub

My config file is as:

Host [email protected]
 HostName bitbucket.org
 IdentityFile ~/.ssh/homeid


Host [email protected]
 HostName bitbucket.org
 IdentityFile ~/.ssh/workid

Now I got a little confused on how to do the cloning with the aliases, but tried:

git clone [email protected]:my-username/myrepo.git

But it fails with the error message:

Cloning into 'myrepo'...
Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

I have added the publick keys to the appropriate bitbucket accounts so not sure what I'm doing wrong here?


Solution

  • Ok worked it out.

    I was using the incorrect git clone address, the correct one being:

    git clone git@workid:my-username/myrepo.git
    

    ...but also had to change my config file to the below to change the hosts:

    Host homeid
     HostName bitbucket.org
     IdentityFile ~/.ssh/homeid
    
    
    Host workid
     HostName bitbucket.org
     IdentityFile ~/.ssh/workid