Skip to content

Instantly share code, notes, and snippets.

@arsho
Last active February 15, 2024 04:43
Show Gist options
  • Save arsho/b91add3f536154386b9a4dca9373d5bb to your computer and use it in GitHub Desktop.
Save arsho/b91add3f536154386b9a4dca9373d5bb to your computer and use it in GitHub Desktop.
Step by step instructions to add SSH key files in Bitbucket or Github

Add SSH Key to Bitbucket / Github in Ubuntu 16.04

What does SSH Keys do in Github / Bitbucket?

Set up SSH to reduce the risk of exposing your username and password. Some reasons you might want to use SSH key base authentication:

  • Is more effective if you push and pull from Bitbucket many times a day.
  • Removes the need to enter a password each time you connect.

Url formats of the repository differs from HTTPS to SSH:

HTTPS:	https://<repo_owner>@bitbucket.org/<accountname>/<reponame>.git
SSH:	git@bitbucket.org:<repo_owner>/<reponame>.git
	or
	ssh://git@bitbucket.org/<repo_owner>/<reponame>.git

Environment

  • Operating System : Ubuntu 16.04 LTS (64-bit)

Add SSH Keys in Ubuntu 16.04

The following steps showed the step by step installation guideline.

Set up your default identity

From the terminal, enter ssh-keygen at the command line. The command prompts you for a file to save the key in:

ssh-keygen

It will create two files; one public key file and one private key file. Both file will have your given name.

Add Key files in local machine

Keep both of the files in ~/.ssh location.

Add the public key to your bitbucket settings

  1. In Bitbucket go to Bitbucket settings>Account settings>SSH keys>Add key
  2. Copy the contents of your public key file using the following command:
cat ~/.ssh/id_rsa.pub

N.B.: id_rsa is the name I have given when generating the key files. It might be something else for your case. 3. Copy and paste the public key in the key field in Bitbucket and include proper label. Click Save.

Ensure Bitbucket SSH connects successfully

Return to the terminal window and write:

ssh -T git@bitbucket.org

For github

ssh -T git@github.com

Give your passphrase which you have given when generating the key files. If your connection is successful then you will get output like following:

logged in as YOUR BITBUCKET USERNAME.

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

For Github:

Hi YOUR GITHUB USERNAME! You've successfully authenticated, but GitHub does not provide shell access.

References

Add SSH Key to Bitbucket / Github in Windows 10

Generate the SSH key

$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/PATH_WILL_BE_SHOWN_HERE/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /PATH_WILL_BE_SHOWN_HERE/.ssh/id_rsa.
Your public key has been saved in /PATH_WILL_BE_SHOWN_HERE/.ssh/id_rsa.pub.

Add the key to the ssh-agent

  • If you don't want to type your password each time you use the key, you'll need to add it to the ssh-agent. To start the agent, run the following:
    $ eval $(ssh-agent) 
    Agent pid XXXX
    
  • Enter ssh-add followed by the path to the private key file:
    $ ssh-add /PATH_WILL_BE_SHOWN_HERE/.ssh/id_rsa
    Identity added: /PATH_WILL_BE_SHOWN_HERE/.ssh/id_rsa
    
  • View the SSH:
    cat /PATH_WILL_BE_SHOWN_HERE/.ssh/id_rsa.pub
    
  • Add the content in Github / Bitbucket SSH key section.

Reference

@ebustillo
Copy link

very useful article mister

@wahyono17
Copy link

Warning: Permanently added 'github.com,13.229.188.59' (RSA) to the list of known hosts.
Permission denied (publickey).

i have try many article but still

@shikha-shah
Copy link

Great article. I was stuck since couple of hours. Thank you s much.

@EspinoVic
Copy link

I don't get it, I generated the key in ubuntu server, but how am I gonna copy/paste it to bitbucket, this thing is a command interface not UI.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment