How to configure and use a dedicated SSH Key in a Bitbucket  project?

How to configure and use a dedicated SSH Key in a Bitbucket project?


SSH

SSH stands for "Secure Shell." It is a cryptographic network protocol used to securely access and manage network devices and servers over a potentially unsecured network.

SSH provides a secure way to establish a remote connection to a system, allowing users to execute commands and manage files on a remote machine as if they were physically present at the computer.


Requirements

You need to have an SSH key added to your Bitbucket project.


Solution


List all SSH keys

ls ~/.ssh/

Output

calaca@calaca-PC ~/var/www $ ls ~/.ssh/
config  id_ed25519  id_ed25519.pub  known_hosts  ssh_key_adc  ssh_key_adc.pub

Check the content

Select the desired SSH key and review its contents.

cat ~/.ssh/ssh_key_adc.pub

Output

calaca@calaca-PC ~ $ cat ~/.ssh/ssh_key_adc.pub
ssh-ed25519 ABADC3NzaC1l0DI1NTE5AAAAIMWf5MaychLIR0mbAvXfpy9IPvrczLr55WTZUu3Tqad email@myemail.com

Create a SSH config file

touch ~/.ssh/config

Understand the configuration

In this step, we'll use the Bitbucket project clone link. In my example, I'll use the following clone link

git@bitbucket.org:my_organization_name/my_app.git

Open the SSH config file

code ~/.ssh/config

I used VS Code Studio to open it, however, you can use your favorite text editor.


Configure the SSH Key config file

Basically, the ssh key config file has 03 components: Host, HostName and IdentifyFile.

Let's start with the easiest: HostName.


HostName

Since we're configuring Bitbucket, our HostName is the following:

HostName bitbucket.org

The previous and the following code snippets must be inside the ~/.ssh/config file.


IdentifyFile

Here, we're going to insert the path of our private ssh key.

IdentityFile ~/.ssh/ssh_key_adc

So far, we have the following code inside the ~/.ssh/config file.

HostName bitbucket.org
IdentityFile ~/.ssh/ssh_key_adc

Host

The Host can be virtually anything you want, but I recommend using something like the following:

Host bitbucket.org-work_adc

When you use SSH to connect to a remote server, you typically use the server's domain name or IP address.

However, with this configuration, you're creating an alias for the host configuration that you can use in your SSH commands.

So far, our code looks something like the following:

Host bitbucket.org-work_adc
    HostName bitbucket.org
    IdentityFile ~/.ssh/ssh_key_adc

Review SSH config file

The following is the complete code:

Host bitbucket.org-work_adc
    HostName bitbucket.org
    IdentityFile ~/.ssh/ssh_key_adc

A bitbucket clone link looks like the following:

git@bitbucket.org:my_organization_name/my_app.git

In order to use our ssh key, we'll have to change that link slightly based on the Host info. The new link would be like the following:

git@bitbucket.org-work_adc:my_organization_name/my_app.git

The reason for the change is due to the content of the ~/.ssh/config file:

Host bitbucket.org-work_adc
    HostName bitbucket.org
    IdentityFile ~/.ssh/ssh_key_adc

With this configuration, you're creating an alias for the host configuration that you can use in your SSH commands.

Let's modify the bitbucket clone link in order to apply that, the new bitbucket clone link is going to be:

git@bitbucket.org-work_adc:my_organization_name/my_app.git

Clone the project

git clone git@bitbucket.org-work_adc:my_organization_name/my_app.git

Open the folder's project

In my example

cd var/www/my_project_name

Check user associated with repo

git config --list

Output


Being more specific about the user

git config --list --local

The command git config --list --local is used to display the Git configuration settings for the current Git repository at the local level. It specifically lists the configuration settings that are specific to the repository where you run the command.


Remove user associated with repo

git config --local --unset user.email
git config --local --unset user.name

Add new user

git config --local user.name "My new name"
git config --local user.email "my-new-email@email.com"


Check repo info

git config --list --local


Celebrate

You've made it!

The Office GIF - The Office Happy - Discover & Share GIFs


Let's become friends


Final thoughts

I hope this article has been helpful to you. Please feel free to reach out if you have any questions. Your thoughts, suggestions, and corrections are more than welcome.

By the way, don't hesitate to drop your suggestions for new blog articles.

I look forward to seeing you next time