Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to maintain multiple bitbucket accounts with multiple ssh keys in the same system

Tags:

git

bitbucket

I have multiple Git accounts one is my personal use and one is for company use. Both accounts source need to be activated from my laptop. Here I generated two ssh keys like id_rsa.pub,id_benwork_rsa.pub and I configured the config of git as

Host sfsworkdid
 HostName bitbucket.org
 IdentityFile ~/.ssh/id_rsa
Host workdid
 HostName bitbucket.org
 IdentityFile ~/.ssh/id_benwork_rsa

So here is my problem: while pushing to any repo git asking the first ssh_key passphrase. Everytime I am changing the user.name in git config as git config user.name "mybitbucketusername". So please tell me how to maintain multiple git accounts with multiple ssh keys in the same system

I tried How to work with multiple ssh keys, Multiple bitbucket accounts but no use

push using multiple account / multiple identity on github / bitbucket is somewhat helpful to reach up to now

like image 581
RamBen Avatar asked Jan 15 '14 14:01

RamBen


People also ask

Can I use the same SSH key for different accounts?

So, No - you'll need a separate key for each account. Although you need multiple ssh key pairs for multiple accounts you can configure multiple ssh identities and use via aliases on your machine. You can also just use your username in place of "git" or "hg". Still need separate keys, though.

Can a system have multiple SSH keys?

For instance, you can run an Organization's GitHub account and another one for your personal projects all on the same computer. In this article, you will learn how to use multiple SSH keys for different GitHub accounts. While working with two different GitHub accounts, you must set them up using an SSH key.


4 Answers

Create multiple identities for Mac OSX, GitBash, and Linux

You should at this point already have created at least a single default identity. To see if you have a default identity already, list the contents of your .ssh directory. Default identity files appear as a id_encrypt and id_encrypt.pub pair. The encrypt value is either rsa or dsa. Use the ssh-keygen command to create a new identity. In the example below, the identity is named personalid.

$ ssh-keygen -f ~/.ssh/personalid -C "personalid"
Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /Users/manthony/.ssh/personalid.
Your public key has been saved in /Users/manthony/.ssh/personalid.pub.
The key fingerprint is:
7a:9c:b2:9c:8e:4e:f4:af:de:70:77:b9:52:fd:44:97 personalid
The key's randomart image is:
+--[ RSA 2048]----+
|         |
|         |
|        .|
|        Eo|
|  .  S  . ..|
|  . . o . ... .|
|  . = = ..o o |
|  . o X ... . .|
|  .ooB.o ..  |
+-----------------+

If you have multiple Bitbucket accounts, you need to generate a new public/private key pair for each account.

Create a SSH config file

When you have multiple identity files, create a SSH config file mechanisms to create aliases for your various identities. You can construct a SSH config file using many parameters and different approaches. The format for the alias entries use in this example is:

Host alias
HostName bitbucket.org
IdentityFile ~/.ssh/identity

To create a config file for two identities (workid and personalid), you would do the following:

  1. Open a terminal window.

  2. Edit the ~/.ssh/config file. If you don't have a config file, create one.

  3. Add an alias for each identity combination for example:

    Host workid
     HostName bitbucket.org
     IdentityFile ~/.ssh/workid
    Host personalid
     HostName bitbucket.org
     IdentityFile ~/.ssh/personalid
    
  4. Close and save the file.

Now, you can substitute the alias for portions of the repository URL address as below: [email protected]:accountname/reponame.git -> git@alias:accountname/reponame.git

Load each key into the appropriate Bitbucket account

enter image description here

Ensure the ssh-agent is running and loaded with your keys

enter image description here

Clone a repository using SSH and your alias configuration

To clone a repository with one of multiple SSH identities that you configured, you clone the repo and using your alias in the SSH URL. To try this for yourself, log into Bitbucket and do the following:

  1. Navigate to the repository Overview.
  2. Display the SSH URL.
    For example, ssh URL as:
    [email protected]:accountname/reponame.git
    then clone the repository using:
    git clone git@personalid:accountname/reponame.git

This refers to official solution Configure multiple SSH identities for GitBash, Mac OSX, & Linux, It works fine for me!

like image 111
Shannon Chou Avatar answered Oct 18 '22 02:10

Shannon Chou


Edit your ~/.ssh/config file as following !

Host bitbucket.org-yourusername
    HostName bitbucket.org
    User yourusername
    IdentityFile ~/.ssh/yoursshkey
    IdentitiesOnly yes

Change your remote git url to have your username before '@bitbucket.org'

git remote add origin [email protected]:company/app.git

or

git remote set-url origin [email protected]:company/app.git    

If you have not yet cloned your repository:

git clone [email protected]:company/app.git
like image 23
SudarP Avatar answered Oct 18 '22 02:10

SudarP


An alternative to the ~/.ssh/config method above is to specify the configuration variable core.sshCommand in the clone command itself. For example,

git clone --config core.sshCommand='ssh -i/home/username/.ssh/id_ed25519' [email protected]:the_best/awesome_repo.git

This will set the local repository configuration value and make subsequent push/pull commands 'just work'.

$ git config --local --get core.sshCommand
ssh -i/home/username/.ssh/id_ed25519

This is supported in git versions 2.10 and later.

like image 11
ctuffli Avatar answered Oct 18 '22 04:10

ctuffli


After searching a lot on the web and with the community help I figured out how to configure 2 differents bitbuckets account on my Mac - MacOS Monterey.

Suppose that you have 2 bitbucket accounts witch usernames are username1 and username12.

  1. Open the terminal and create 2 ssh file2 for both usernames:

ssh-keygen -f ~/.ssh/username1-Bitbucket

ssh-keygen -f ~/.ssh/username2-Bitbucket

  1. Start ssh-agent:

eval $(ssh-agent)

  1. Create or edit the ~/.ssh/config file:

Create:

touch ~/.ssh/config

open ~/.ssh/config

Edit:

open ~/.ssh/config

The ~/.ssh/config file should look like this:

Host username1-Bitbucket
    HostName bitbucket.org
    User username1
    IdentityFile ~/.ssh/username1-Bitbucket

Host username2-Bitbucket
    HostName bitbucket.org
    User username2
    IdentityFile ~/.ssh/username2-Bitbucket

Host *
    UseKeychain yes
    AddKeysToAgent yes
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/username1-Bitbucket
    IdentityFile ~/.ssh/username2-Bitbucket
    IdentitiesOnly yes
    PreferredAuthentications keyboard-interactive,password
  1. Add the keys to the ssh-agent:

ssh-add --apple-use-keychain ~/.ssh/username1-Bitbucket

ssh-add --apple-use-keychain ~/.ssh/username2-Bitbucket

  1. Copy the ssh key and add it to your bitbuckets accounts. See link:

pbcopy < ~/.ssh/username1-Bitbucket.pub

pbcopy < ~/.ssh/username1-Bitbucket.pub

  1. Check if connection to bitbucket succeeded (At least one) in order to add Bitbucket as a known hosts:

ssh -T [email protected]

  1. Clone your repositories using ssh and cd on the terminal to the repository folder.

  2. Check the reposiroties remote url:

git remote -v

You'll get information like this one:

[email protected]:companyUserName/repositoryName.git

  1. Change the repositories remote url:

git remote set-url origin username1@username1-Bitbucket:username1/repositoryName.git

git remote set-url origin username2@username2-Bitbucket:username2/repositoryName.git

So we changed [email protected]:companyUserName/repositoryName.git to username1@username1-Bitbucket:username1/repositoryName.git

git -> username1 or username2, the username of the accounts.

bitbucket -> username1-Bitbucket or username2-Bitbucket, the host alias of the config file.

companyUserName -> It's the username of the account that holds the repository, in our case the repositories are owned by username1 or username2.

repositoryName -> The name of the repository.

  1. Test your connection on both repositories creating a new branch and trying to push it:

git checkout -b newBranch

git push origin newBranch

If everything works correctly on both repositories, you are done!

Hope that this solution works for every Mac user.

That's all folk!

like image 6
Gastón Antonio Montes Avatar answered Oct 18 '22 03:10

Gastón Antonio Montes