Set up additional SSH keys

While using the same password on multiple sites makes your accounts less secure, most of the time you can use the same SSH key for multiple accounts. However, there are specific situations when you'll need to set up more than one SSH key:

  • You have two different Bitbucket Cloud accounts. For example, if you have a work-related Bitbucket account and a personal Bitbucket account, each account must have its own SSH key.

  • You're using two different computers to log in to the same account.

  • You want to execute DVCS actions on a repository with a script that uses a public key with an empty passphrase, allowing it to run without human intervention.

Here are your options when setting up additional SSH keys:

 

Windows

macOS

Linux

Git

Command line prompt / Git Bash

Sourcetree

Terminal

Terminal

By this point, you should have already created at least one SSH key. This one SSH key is your default identity because it's the key that Bitbucket checks first when authenticating. If you don't have an SSH key, follow the steps on Set up an SSH key.

Set up additional SSH keys for Git

When using Git, you can use this section to create as many SSH keys as you need on Windows, macOS, or Linux.

Step 1. Prepare your default identity

  1. Determine your Git clone URL.

    $ git remote -v
    origin git@bitbucket.org:teamsinspace/bitbucketspacestation.git (fetch)
    origin git@bitbucket.org:teamsinspace/bitbucketspacestation.git (push)

  2. Update the remote URL with your Bitbucket username by replacing git@bitbucket.org with <username>@bitbucket.org. For this step and the ones that follow, enter your username in place of <username>.

    $ git remote set-url origin <username>@bitbucket.org:teamsinspace/bitbucketspacestation.git

Step 2. Create additional SSH keys

You'll need to create an additional SSH key for each extra Bitbucket account you have or each computer you use. For example, if you have four Bitbucket accounts, you need to generate 3 new SSH keys, meaning you'll have 4 keys in all.

  1. Generate your new SSH key. In place of <username>, enter the username of the Bitbucket account for which you're creating the SSH key.

    For Windows 7 or earlier

    You can only enter ssh-keygen into the Git Bash window. It won't work in the Command prompt.

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

  2. Add your new SSH key to your account. Make sure you're adding it to the right account.

    $ ssh-add ~/.ssh/<username>

Step 3. Add the public key to your Account settings

Add each public SSH key into the corresponding account. If you have an account that you access from two different locations, add both keys to that account.

  1. From Bitbucket, choose Personal settings from your avatar in the lower left.

  2. On the Account settings page, click SSH keys.

  3. On the SSH keys page, copy the contents of your public key file. If you have any existing keys, those appear on this page.

    • Windows: Open your ~/.ssh/<public_key> file and copy its contents.

    • macOS: Copy the output to the clipboard with this command: $ pbcopy < ~/.ssh/<public_key_file>

    • Linux: Copy the output to the clipboard with this command: $ cat ~/.ssh/<public_key_file>

  4. From Bitbucket, enter a Label for your new key, for example, Public key #2.

  5. Paste the copied public key into the SSH Key field:

  6. Click Save. Bitbucket sends you an email to confirm the addition of the key.

Next time you clone a repository, use the SSH URL for that repository and replace git in the <username>@bitbucket.org part of the URL with your Bitbucket username. For example:

git clone <username>@bitbucket.org:teamsinspace/bitbucketspacestation.git

If you want to change the URL of an existing repository using HTTPS or a different SSH URL, change the remote URL for your repository.

Set up additional SSH keys with Sourcetree (on Windows)

Use this section to create as many SSH keys as you need when using Sourcing on Windows.

Step 1. Create an SSH key

  1. From Tools, select Create or Import SSH Keys.

  2. From the PuTTY Key Generator dialog, click the Generate button.

  3. As the SSH key generates, hover your mouse over the blank area in the dialog. It may take a minute or two. When SSH key generation is complete, you see the public key and a few other fields.

  4. Update the Key comment with something that distinguishes it from your other SSH keys.

  5. Enter a passphrase for your SSH key in the Key passphrase and Confirm passphrase fields.

  6. Click Save public key. From the save dialog, choose where to save your public key, name the file, and click Save.

  7. Click Save private key. From the save dialog, choose where to save your private key, name the file, and click Save.

  8. Close the PuTTY Key Generator dialog.

Step 2. Install your private key on Pageant

Sourcetree comes with an SSH authentication agent called Pageant. Load your private key into Pageant to automatically authenticate so that you don't need to enter your passphrase.

  1. Double-click the Pageant (PuTTY Authentication Agent) icon in your system tray to open the Pageant Key List dialog.

  2. Click the Add Key button.

    The system displays the Select Private Key File dialog.

  3. In the Select Private Key File dialog, navigate to the private key file you saved in Step 1 and click Open.

  4. Enter the passphrase for your SSH key and click OK. Pageant shows your key in the running list.

  5. Press Close to close the dialog.

Step 3. Add the public key to your Account settings

  1. From Sourcetree, open the PuTTY Key Generator dialog by going to Tools > Create or Import SSH Keys.

  2. Click Load, navigate to your SSH folder, and click the private key.

  3. Enter your passphrase for the SSH key and click OK.

  4. Copy the public key in the first field.

  5. From Bitbucket, choose Personal settings from your avatar in the lower left.

    The Account settings page opens.

  6. On the Account settings page, click SSH keys.

    If you've already added keys, you'll see them on this page.

  7. Click Add key.

  8. From Bitbucket, enter a Label for your new key, for example, Public key #2.

  9. Paste the copied public key into the SSH Key field.

  10. Click Save. Bitbucket sends you an email to confirm the addition of the key.

Edit an SSH key

After you add a key, you can edit the key's Label but not the key itself. If you need to change the key's contents, you must delete and re-add the key.

Last modified on Nov 23, 2020
Cached at 10:51 PM on Nov 25, 2020 |

Additional Help

Ask the Community