Tuesday, October 14, 2014

Generate SSH Public Key for GitHub and Bitbucket

ssh key github bitbucket

This post demonstrate how to generate SSH private and public key with simple steps and help you to add public key inside your GitHub and Bitbucket account.

But first why we need this SSH public key?

Whenever you access or do some Git operation on remote repository using HTTPS connection from your local machine, you need to authenticate yourself first using username and password.

It looks simple earlier, but later becomes headache.

SSH connection helps us to authenticate using public and private keys, that we generate only once, later we don’t need to authenticate, SSH will do this behalf of us.

But how does it works?

SSH authenticate you using an identity. And this identity is combination of private, public key.

Private Key resides in your local machine and public key inside your GitHub or Bitbucket account.

When you access Git repository, SSH combines these two keys and authenticates you.
                                      
To use SSH you need to install it first. I assumed that you have already installed it, if not 
then please visit http://www.openssh.com and install SSH to your local machine.

Generate private-public keys

Us following steps to generate private and public key, later copy public key and add it in your GitHub and Bitbucket account.

Open terminal and type following command

  ssh-keygen            

It will ask location for key files, hit Enter to select default location

Then it will ask passphrase (password), you can type your password or cant keep it empty

It will ask re-passhprase

once done, keys get generated and get saved inside provided location. 
Here is sample output of ssh-keygen operation.

Then add your key to the ssh-agent.
  ssh-add ~/.ssh/id_rsa    

Later we need to add public key to GitHub and Bitbucket account. To do so we need get 
content of public key, this resides inside ~/.ssh path.

  cat ~/.ssh/id_rsa.pub     

Now copy this key and visit GitHub or Bitbucket to add it into your account.

GitHub

Open a browser and log into your GitHub account.

  • Select setting icon from top right corner of menu bar.
  • Then select “SSH keys” from left side panel
  • Now add new public key by selecting “Add SSH key” button. It will open a panel to enter public key.
  • Add label for public key and then add copied public key inside key area.
  • Now finish this process by clicking "Add key" button.


That’s it, you done with GitHub.

Bitbucket

Open a browser and log into your Bitbucket account.

  • Select Avatar->Manage Account from the application menu.
  • Under Security section Click SSH keys.
  • Then click Add key button. It will prompt a dialog to add public key.
  • Add label for public key and then add copied public key inside key area.
  • Now finish this process by clicking Add key button.


That’s it, you done with Bitbucket.

Zainul Abedin Shaikh

Founder of Zainabed, Passionate about programming. Follow him on Twitter

And if you enjoyed this post, please consider sharing it on Facebook, Google+ or Twitter below