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.
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.
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.
Add your default keys or any of the public key. I am using newsshkey.pub for my Second Account.
-
You can set it inside: Repo > Settings > AccessKeys. * https://bitbucket.org/Account2/project1/admin/access-keys/
-
Change Account2 with your accountname *
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 [email protected]: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.
- 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
- 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
- Now try to clone the repo using Host value just as below.
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
So in short:
1)Create Config file inside .ssh of your home directory.
2)Add a code:
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.