DEV Community

Cover image for Quick Bite: SSH Key
Nazmuz Shakib Pranto
Nazmuz Shakib Pranto

Posted on • Edited on

Quick Bite: SSH Key

SSH stands for - "Secure Shell"

It is a protocol that enables secure communication between 2 machines over the internet

An SSH key is just a secure alternative to email/password authentication to identify yourself as a valid contributor to a remote repository on GitHub, GitLab, and Bitbucket.

Entering an email and password can become a pain any time you may want to clone down or push your code up to a remote source, thus setting up SSH keys will save you some time, not to mention the added layer of security through encryption between communications.

SSH keys are generated using a combination of symmetric and asymmetric encryption as well as hashing to provide a secure tunnel when sharing data between devices.


Use Cases for SSH?

  • when you want to access another computer or a server remotely from your local machine
  • when you want to clone down or push changes to a remote repository sitting on GitHub, GitLab, or Bitbucket servers

How To Generate an SSH Key?

Prerequisites:

  • macOS
  • Terminal (built-in on macOS) / iTerm

Steps:

  1. Open Terminal (CMD + SPACE, search for "terminal", open Terminal.app)

  2. Run cd ~/.ssh

  3. Run ls

  4. If you see id_rsa.pub file, then you already have an SSH key (no need to generate a new one)

  5. To generate a new key, run ssh-keygen -t rsa -b 4096 -C "<EMAIL>" (replace <EMAIL> with your email)

  6. Next, you will be asked where you want to save your SSH key - just press RETURN key to accept the default location (/Users/<USERNAME>/.ssh/id_rsa)

  7. After that, a secure passphrase - press RETURN key twice to skip that step as well (generally, you don't need to set up a passphrase for SSH key)

  8. Finally, you should see a confirmation stating that your identification and the public key have been saved.

  9. That's it! Now, you have an SSH key on your local machine

Ok, great! Now, I have an SSH key. Wait… how do I access it?

How to Access SSH Key On Your Local Machine?

  • Open Terminal (CMD + SPACE, search for "terminal", open Terminal.app)
  • Run pbcopy < ~/.ssh/id_rsa.pub (to copy SSH key to clipboard)
  • Paste (CMD + v) to see your public SSH key

Ok, so now I have copied the SSH key, what do I do with it?


How To Add SSH Key to GitHub?

Step 1 - Go to https://github.com/

Step 2- Log into your account

Step 3- From the top right profile dropdown, choose "Settings"

Settings from profile dropdown

Step 4- Now, from the left vertical menu list, click on "SSH and GPG keys"

SSH and GPG keys option from menu list

Step 5- Under the SSH keys section, click on the green button - "New SSH key"

New SSH key button

Step 6- Now, fill in the details for the "Title" and "Key" fields

  • "Title" - the identifier for the SSH key, i.e., "Macbook Air (2020)"
  • "Key" - the public key you copied on your clipboard earlier

SSH key and Title fields

Step 7- Finally, click on "Add SSH key"


How To Add SSH Key to GitLab?

Step 1 - Go to https://gitlab.com/users/sign_in

Step 2- Log into your account

Step 3- From the top right profile dropdown, choose "Preferences"

Preferences from profile dropdown

Step 4- Now, from the left vertical menu list, click on "SSH Keys"

SSH Keys from menu list

Step 5- Now, fill in the details for "Key", "Title" and "Expires at" fields:

  • "Key" - the public key you copied on your clipboard earlier
  • "Title" - the identifier for the SSH key, i.e., "Macbook Air (2020)"
  • "Expires at" - when do you want the SSH key to expire

Key, Title and Expires at fields

Step 6- Finally, click on "Add SSH key"


How To Add SSH Key to Bitbucket?

Step 1- Go to https://bitbucket.org/

Step 2- Log into your account

Step 3- From the bottom left profile avatar dropdown, choose "Personal settings"

Personal settings from profile dropdown

Step 4- Now, under the "Personal settings" menu list on the left side, click on "SSH keys" under the "Security" section

SSH keys from personal settings list

Step 5- Click on "Add key"

Add key button

Step 6 - Fill in the fields for "Label" and "Key"

  • "Label" - the identifier for the SSH key, i.e., "Macbook Air (2020)"
  • "Key" - the public key you copied on your clipboard earlier

Fields for Label and Key

Step 7- Finally, click on "Add key" at the bottom


Now that you have added the SSH key to a remote account like GitHub, GitHub will no longer prompt you for your credentials (email and password) any time you clone a repository down or push updates to it.


How To Delete SSH Key from Local Machine?

  • Open Terminal (CMD + SPACE, search for "terminal", open Terminal.app)
  • Run rm ~/.ssh/id_rsa ~/.ssh/id_rsa.pub (deletes both public + private keys)

Already, that's all I had… hope that helps!


Before I leave, a few things…

  • 📑 bookmark this page in case you want to refer back to it later on
  • 👍 share this article with your peers or anyone who might need help setting up an SSH key
  • 💌 if I got anything wrong in this article, please leave a kind feedback

References:

Top comments (0)