shou2017.com
JP

Setting Up Public and Private Keys for Bitbucket

Thu Mar 1, 2018
Sat Aug 10, 2024
Git

The Bitbucket setup for Rails tutorials seemed a bit challenging for beginners, so here are some notes.

1- Check if a Public Key Already Exists

Run cat ~/.ssh/id_rsa.pub to check if a public key has already been created. If not, you will see No such file or directory.

If a key already exists, skip to step 3.

$ cat ~/.ssh/id_rsa.pub
  cat: /home/vagrant/.ssh/id_rsa.pub: No such file or directory

2- Create Public and Private Keys for SSH Authentication

Generate an SSH key by running ssh-keygen -t rsa -C aaa@mail.

Press Enter for all prompts.

$ cd ~/.ssh
~/.ssh$ ssh-keygen -t rsa -C aaa@mail # Replace with your email address
Generating public/private rsa key pair.
Enter file in which to save the key (/home/vagrant/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/vagrant/.ssh/id_rsa.
Your public key has been saved in /home/vagrant/.ssh/id_rsa.pub.
The key fingerprint is:
******************************************************* aaa@mail
The key's randomart image is:
+---[RSA 2048]----+
|      .    .+.. o|
|     +   . . + +.|
|    . + o * . *oo|
|     + o + B +.Xo|
|    . + S + +.B.+|
|     . . o . ooo.|
|       E    ..o+o|
|        o    +. o|
|         .... .  |
+----[SHA256]-----+

Two files will be created:

id_rsa: Private key
id_rsa.pub: Public key, to be registered with Bitbucket

Change the file permissions to secure the key.

$ chmod 600 id_rsa

Set up the SSH key on your PC:

$ vim ~/.ssh/config

Add the following content to the end of the ~/.ssh/config file. Save with :wq or discard changes with :q!.

Host bitbucket.org
  HostName bitbucket.org
  IdentityFile ~/.ssh/id_rsa
  User git

3- Register the Public Key with Bitbucket

Select the user icon in the bottom left corner to access Bitbucket Settings.

Setting Up Public and Private Keys for Bitbucket

Choose SSH keys under Security.

Setting Up Public and Private Keys for Bitbucket

Click Add Key.

Setting Up Public and Private Keys for Bitbucket

Provide a label and paste the key using the following command:

$ cat ~/.ssh/id_rsa.pub | xclip -sel clip

If xclip is not installed, manually copy the content starting from ssh-rsa to the email address.

~$ cat ~/.ssh/id_rsa.pub
ssh-rsa
**************************************************************************
**************************************************************************
aaa@mail

Verify the Connection

When prompted with Are you sure you want to continue connecting (yes/no)?, type yes.

$ ssh -T [email protected]

logged in as account_name.

You can use git or hg to connect to Bitbucket. Shell access is disabled.

Ensure the connection is successful.

See Also