Skip to content

Instantly share code, notes, and snippets.

@shakeeb91
Last active August 18, 2021 08:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save shakeeb91/cd3d3c387f339fbd93ac7388b3c885e0 to your computer and use it in GitHub Desktop.
Save shakeeb91/cd3d3c387f339fbd93ac7388b3c885e0 to your computer and use it in GitHub Desktop.
Single user to access Multiple Bitbucket accounts using SSH Keys

Single user to access Multiple Bitbucket accounts using SSH Keys

ISSUE: Not able to access different bitbukcet accounts from single ssh access key

It is not possible to access different bitbucket account using single ssh key. So to access it from single computer we need to create multiple SSH keys and set those accordingly in bitbucket.

Generate SSH keys

By default we havse single SSH key in out home directory. https://confluence.atlassian.com/bitbucketserver/creating-ssh-keys-776639788.html

shakeeb@shakeeb-xps-13:ls ~/.ssh/
id_rsa  id_rsa.pub

To generate new ssh keys you can use a command

shakeeb@shakeeb-xps-13:ssh-keygen -f ~/.ssh/newsshkey
Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/shakeeb/.ssh/newsshkey.
Your public key has been saved in /home/shakeeb/.ssh/newsshkey.pub.
The key fingerprint is:
SHA256:cXp0SOdzj9d1gBYrLNrCxJDyJgXztvsBnnkivpoBP84 shakeeb@shakeeb-xps-13
The key's randomart image is:
+---[RSA 2048]----+
|  o...    . oo.  |
|  .ooo   o +o. . |
|   +o o o *.= . o|
|  ..o+ o * o o o+|
|.  oo + S .   . +|
|.. . = . .     . |
|. + * o          |
| * o + .         |
|o.E.  .          |
+----[SHA256]-----+

shakeeb@shakeeb-xps-13:ls ~/.ssh/
id_rsa  id_rsa.pub  newsshkey  newsshkey.pub

See you will get new public ( newsshkey.pub ) and a private key ( newsshkey ) Note: Don't share your private key with any one.

On BitBucket:

Account:1

Add your default keys or any of the public key. I am using id_rsa.pub for my first Account. *You can set it inside: Repo > Settings > AccessKeys. * https://bitbucket.org/Account1/project1/admin/access-keys/

Change Account1 with your accountname

Now login to your system and take a git clone to your private repo and you can easily clone your repository.

Account:2

Add your default keys or any of the public key. I am using newsshkey.pub for my Second Account.

Now login to your system and take a git clone to your private repo ( from account # 2 ) not able to clone and you get an error

git clone git@bitbucket.org:Account2/repository.git
Cloning into 'repository'...
Forbidden
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
Reason: It still using id_rsa.pub public key to access account # 1.

Solution:

  1. Create a config file inside ~/.ssh
shakeeb@shakeeb-xps-13:~$ touch ~/.ssh/config
shakeeb@shakeeb-xps-13:~$ ls -la ~/.ssh/config 
-rw-r--r-- 1 shakeeb shakeeb 116 Dec 22 14:05 /home/shakeeb/.ssh/config
  1. Add these in config file.
shakeeb@shakeeb-xps-13:~$ nano ~/.ssh/config
shakeeb@shakeeb-xps-13:~$ cat ~/.ssh/config 
Host myaccount2access
   HostName bitbucket.org
   User git
   IdentityFile /home/shakeeb/.ssh/newsshkey
   IdentitiesOnly yes
  1. Now try to clone the repo using Host value just as below.
git clone git@myaccount2access:shakeeb91/repository.git

a) You need to replace account2 with Host value we use account2 b) shakeeb91 is the repository account name ( You need to use your own accountname ) c) repository is the repo name

git clone git@myaccount2access:shakeeb91/repository.git
Cloning into 'repositoru'...
Warning: Permanently added the RSA host key for IP address '2406:da00:ff00::22e9:9f55' to the list of known hosts.
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (3/3), done.

Reference issues on Stackoverflow. https://stackoverflow.com/questions/21139926/how-to-maintain-multiple-bitbucket-accounts-with-multiple-ssh-keys-in-the-same-s

@shakeeb91
Copy link
Author

shakeeb91 commented Dec 22, 2019

So in short:

1)Create Config file inside .ssh of your home directory.
2)Add a code:

Host myaccount2access
   HostName bitbucket.org
   User git
   IdentityFile /home/shakeeb/.ssh/newsshkey
   IdentitiesOnly yes

Host myaccount3access
   HostName bitbucket.org
   User git
   IdentityFile /home/shakeeb/.ssh/newsshkey2
   IdentitiesOnly yes

and then clone the repository.
It will call the Host: myaccount2access and uses newsshkey
git clone git@myaccount2access:account2/repository.git

If we run below than it will call the Host: myaccount3access and uses newsshkey2
git clone git@myaccount3access:account3/repository.git

If means you can access multiple accounts with your desired keys which you added in respective bitbucket repo / account.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment