Mobio Technological Solutions Blog

Mobio Solutions

Multiple Accounts and SSH Keys

Problem is, Bitbucket doesn’t allow you to use the same SSH key with more than one Bitbucket account, and here is the Quick Tips to manage it.

About SSH key

Using the SSH protocol, you can connect and authenticate to remote servers and services. With SSH keys, you can connect to GitHub without supplying your username or password at each visit.

Generating a new SSH key

  • Open Git Bash.
  • Paste the text below, substituting in your GitHub/GitLab/Bitbucket email address.
  • ssh-keygen -t rsa -C “your_email@example.com”
  • When you’re prompted to “Enter a file in which to save the key,” press Enter. This accepts the default file location.
  • Enter a file in which to save the key (/c/Users/you/.ssh/id_rsa):[Press enter]
  • At the prompt, type a secure passphrase/password.
  • Enter passphrase (empty for no passphrase): [Type a passphrase]
    Enter same passphrase again: [Type passphrase again]
  • Go to C:\Users\< Current User >\.ssh\id_rsa.pub
  • Copy All Content than Pest into (Go to bitbucket Setting -> Click on SSH Key -> Add New key)

Generating a Second SSH key

  • Open Git Bash.
  • Paste the text below, substituting in your GitHub/GitLab/Bitbucket email address.
  • ssh-keygen -t rsa -f ~/.ssh/< Name of Second .pub file > -C “your_email@example.com”
    • Example : ssh-keygen -t rsa -f ~/.ssh/< accountB > -C “your_email@youremail.com”
  • When you’re prompted to “Enter a file in which to save the key,” press Enter. This accepts the default file location.
  • Enter a file in which to save the key (/c/Users/you/.ssh/id_rsa):[Press enter]
  • At the prompt, type a secure passphrase/password for second account. Plz remember this password its user for Push and Pull Command .
  • Enter passphrase (empty for no passphrase): [Type a passphrase]
    Enter same passphrase again: [Type passphrase again]
  • Go to C:\Users\< Current User >\.ssh\< accountB >.pub
  • Copy All Content than Pest into (Go to bitbucket Setting -> Click on SSH Key -> Add New key)

Create a SSH config file

When you have multiple identity files, consider creating an SSH config file 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 used in this example is:

  • Host alias
  • HostName bitbucket.org
  • IdentityFile ~/.ssh/config

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

  • Open a terminal window.
  • Edit the ~/.ssh/config file.
  • If you don’t have a config file, create one.
  • Add an alias for each identity combination for example:

To use multiple keys create a file at C:\Users\< Current User >\.ssh\config with contents similar to:

  • Host bitbucket.org
  • User git
  • Hostname bitbucket.org
  • PreferredAuthentications publickey
  • IdentityFile ~/.ssh/id_rsa
  • Host bitbucket- < Name of Second .pub file > accountB
  • User git
  • Hostname bitbucket.org
  • PreferredAuthentications publickey
  • IdentitiesOnly yes
  • IdentityFile ~/.ssh/< Name of Second .pub file >accountB

Example of Clone Repository using 2 different Accounts

With this set up I can clone with my default key as Bitbucket suggests:

Example : Git clone git@bitbucket.org:username/project.git

This below repository is contains Second Git Account

Example : Git clone git@bitbucket.org:username/project_demo.git

Just change host bitbucket.org of second git repository URL :

Syntax : Git clone git@bitbucket_< Name of Second ~/ssh/.pub file >:username/project_demo.git

Example : Git clone git@bitbucket_accountB:username/project_demo.git

Impact of Other Git Command

Example:
  • git remote set-url origin git@< Default/other Account Host >:username/project_demo.git
  • git remote add origin git@< Default/other Account Host >:username/project_demo.git

Leave a Reply

Your email address will not be published. Required fields are marked *