Do yourself a favor and start working with SSH connections, especially if you do Git and remote repos, this is a little guide of how to setup your ssh keys to later add to your Bitbucker repo, but the same procedure apply to GitHub or Gitlab. Don’t think ssh is only for git, it is also use for a lot of things involving remote connections.
The procedure we use apply for Linux , Mac and Windows or WSL. For windows users make sure you have installed Git and OpenSSH, in git bash just do
$ ssh -V
OpenSSH_9.3p1, OpenSSL 1.1.1u 30 May 2023
Usually every Linux distro comes with OpenSSH by default but in case don’t, like in WSL you can use pacman for Arch Linux to install it
$ sudo pacman -Sy openssh
Make sure your ssh-agent is running
$ ps -auxc | grep ssh-agent
test 641 0.0 0.0 7292 752 ? Ss 16:18 0:00 ssh-agent
If nothing appears, type: (repeat the previous line to verify if it is running)
$ eval $(ssh-agent)
Creating your SSH key pairs
Think about an electronic credentials to authorize remote access between two machines, these are made of two files, one them you have to keep it private an secure in your machine, and the other one with the extension .pub you have to share with the remote server ( in our case is Bitbucket ), by default in Linux these keys are store in a directory called ~/.ssh that goes in your user directory
Create SSH key pair, first make a new dir called .ssh (if does not existe yet)
$ cd ~
$ mkdir .ssh
Generate the ssh key pair, 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, it is recommended to use a more verbose name that identifies where it will use this key. for instance:myhost-bitbucket
orhomelinux-gitlabserver
just follow this simple rule<name of your machine>-<name of server to connect>
When the terminal asks you to introduce a passphrase, just hit enter to avoid this step if you like
$ ssh-keygen -t ed25519 -b 4096 -C "username@emaildomain.com" -f .ssh/ssh-key-name
Generating public/private ed25519 key pair.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in .ssh/ssh-key-name
Your public key has been saved in .ssh/ssh-key-name.pub
The key fingerprint is:
SHA256:6eePbO2gy3xWoPiUevf2ZFVOxniet1Yrq/Q3fdZ1IFc username@emaildomain.com
The key's randomart image is:
+--[ED25519 256]--+
| |
| oE|
| ..*|
| .. . o*o|
| .So . o o*|
| ..+ . .B|
| +. o+ .o+=|
| .oo=*o+o+o=|
| .=*+*=+o.o|
+----[SHA256]-----+
Verify the key was created
$ ls .ssh
ssh-key-name ssh-key-name.pub
Add the key to ssh agent
$ ssh-add .ssh/ssh-key-name
Identity added: .ssh/ssh-key-name (username@emaildomain.com)
Copy the contents of your public key
$ cat .ssh/ssh-key-name.pub
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIHwvf0Wy+Ti6NqdOvQYAxFR+QvsjLFn1n3oV19MJSga7 username@emaildomain.com
At http://bitbucket.org/, Select the Settings (gear icon on the top navigation bar). from the navigation bar at the top of the screen, Under Settings, select Personal Bitbucket Settings.
Under Security, select SSH keys, Select Add key., In the Add SSH key dialog, provide a Label to help you identify which key you are adding.
To test that the SSH key was added successfully, open a terminal on your device and run the following command:
$ ssh -T git@bitbucket.org
authenticated via ssh key.
You can use git to connect to Bitbucket. Shell access is disabled
Now you are pretty much ready to clone any repo under your permission scope (personal or team repos, etc..)
$ git clone git@bitbucket.org:modularmx/casiocanplus.git
Cloning into 'casiocanplus'...
remote: Enumerating objects: 329, done.
remote: Counting objects: 100% (329/329), done.
remote: Compressing objects: 100% (281/281), done.
remote: Total 329 (delta 101), reused 208 (delta 44), pack-reused 0
Receiving objects: 100% (329/329), 1.66 MiB | 2.31 MiB/s, done.
Resolving deltas: 100% (101/101), done.
NOTE: if for some reason every time after a reset when trying to connect the following message appears
$ ssh -T git@bitbucket.org
git@bitbucket.org: Permission denied (publickey).
You can solve it by appending the following lines to .bashrc file $ code ~/.bashrc
eval $(ssh-agent)
ssh-add ~/.ssh/ssh-key-name
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:
$ code ~/.ssh/config
Then add the following content (just replace ssh-key-name with your ssh key name)
Host bitbucket.org
AddKeysToAgent yes
IdentityFile ~/.ssh/ssh-key-name
More details here: https://support.atlassian.com/bitbucket-cloud/docs/set-up-personal-ssh-keys-on-linux/