Set up Repository Access keys on Linux

Add access keys to your Bitbucket Cloud repositories to allow a user or service to authenticate when pulling or cloning a repository over SSH. For example, you may want to use an access key to authenticate with Bitbucket when a build server checks out and tests your code. 

An access key has the following features and limitations:

  • Grant read-only access to a public or private repository.

  • Don't require additional users on your plan.

  • Can be added to multiple repositories.

  • Can't also be associated with an account.

  • Don't require a passphrase when used for automated processes.

Before you can add an access key to a repository, you'll need to generate a unique SSH key just as you would for your individual account. To generate and add an SSH key:

  1. Install OpenSSH on your device.

  2. Start the SSH Agent.

  3. Create an SSH key pair.

  4. Add your key to the SSH agent.

  5. Provide Bitbucket Cloud with your public key.

  6. Check that your SSH authentication works.

Install OpenSSH on Linux

To install OpenSSH, we recommend using the OpenSSH package provided by your Linux distribution.

  • For Debian, Ubuntu, Linux Mint, and other Debian-based distributions:

    1 sudo apt update && sudo apt install openssh-client
  • For Fedora, CentOS, Red Hat Enterprise Linux, Oracle Linux, and other Fedora-based distributions:

    1 sudo dnf install openssh-clients
  • For Arch Linux and other Arch Linux-based distributions:

    1 sudo pacman -Sy openssh
  • For SUSE Linux, openSUSE Linux, and other SUSE-based distributions:

    1 sudo zypper install openssh

In the terminal, check that OpenSSH has been successfully installed by running the following command:

1 ssh -V

The output should show the installed version of OpenSSH.

Start the SSH agent

To allow git to use your SSH key, an SSH agent needs to be running on your device.

To check if it is already running, run the ps command. If the ssh-agent is already running, it should appear in the output, such as:

1 2 $ ps -ax | grep ssh-agent 19998 ?? 0:00.20 /usr/bin/ssh-agent -l

To start the agent, run:

1 eval $(ssh-agent)

You may need to add this command to your ~/.bashrc, ~/.zshrc, ~/.profile, or equivalent shell configuration file. Adding this command to a shell configuration file will ensure the agent is running when you open a terminal.

Create an SSH key pair

To create an SSH key pair:

  1. Open a terminal and navigate to your home or user directory using cd, for example:

    1 cd ~
  2. Generate a SSH key pair using ssh-keygen, such as:

    1 ssh-keygen -t ed25519 -b 4096 -C "{username@emaildomain.com}" -f {ssh-key-name}

    Where:

    • {username@emaildomain.com} is the email address associated with the Bitbucket Cloud account, such as your work email account.

    • {ssh-key-name} is the output filename for the keys. We recommend using a identifiable name such as bitbucket_work.

  3. When prompted to Enter passphrase, you can either provide a password or leave the password empty. If you input a password, you will be prompted for this password each time SSH is used, such as using Git command that contact Bitbucket Cloud (such as git push, git pull, and git fetch). Providing a password will prevent other users with access to the device from using your keys.

Once complete, ssh-keygen will output two files:

  • {ssh-key-name} — the private key.

  • {ssh-key-name}.pub — the public key.

Add your key to the SSH agent

To add the SSH key to your SSH agent (ssh-agent):

  1. Run the following command, replacing the {ssh-key-name} with the name of the private key:

    1 ssh-add ~/{ssh-key-name}
  2. To ensure the correct SSH key is used when connecting to Bitbucket, update or create your SSH configuration file (~/.ssh/config) with the following settings:

    1 2 3 Host bitbucket.org AddKeysToAgent yes IdentityFile ~/.ssh/{ssh-key-name}

    Where {ssh-key-name} is the location of the private key file once it has been added to the ssh-agent.

Provide Bitbucket Cloud with your public key

To add an SSH key to a Bitbucket repository:

  1. At bitbucket.org, navigate to the repository and select Repository settings.

  2. Under Security, select Access keys.

  3. Select Add key.

  4. In the Add SSH key dialog, provide a Label to help you identify which key you are adding. For example, Work Laptop <Manufacturer> <Model>. A meaning full label will help you identify old or unwanted keys in the future.

  5. Open the public SSH key file (public keys have the .pub file extension) in a text editor. The public key should be in the .ssh/ directory of your user (or home) directory. The contents will be similar to:

    1 ssh-ed25529 LLoWYaPswHzVqQ7L7B07LzIJbntgmHqrE40t17nGXL71QX9IoFGKYoF5pJKUMvR+DZotTm user@example.com
  6. Copy the contents of the public key file and paste the key into the Key field of the Add SSH key dialog.

  7. Select Add SSH key.

    • If the key is added successfully, the dialog will close and the key will be listed on the Access keys page.

    • If you receive the error That SSH key is invalid, check that you copied the entire contents of the public key (.pub file).

Check that your SSH authentication works

To test that the SSH key was added successfully, open a terminal on your device and run the following command:

1 ssh -T git@bitbucket.org

If SSH can successfully connect with Bitbucket using your SSH keys, the command will produce output similar to:

1 2 3 authenticated via ssh key. You can use git to connect to Bitbucket. Shell access is disabled

 

Still need help?

The Atlassian Community is here for you.