Quick Bite: SSH Key
Tags: sshkey, github, gitlab, bitbucket
Published: 2021-12-04 16:01:20

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](https://cdn-images-1.medium.com/max/800/0*jJzB3JWly81htkLi)

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

![SSH and GPG keys option from menu list](https://cdn-images-1.medium.com/max/800/0*Rx_F7epXpEfHdDUv)

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

![New SSH key button](https://cdn-images-1.medium.com/max/800/0*jAhWbcCNR4rY6G_6)

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](https://cdn-images-1.medium.com/max/800/0*o1STZak2zkggtTH5)

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](https://cdn-images-1.medium.com/max/800/0*510zMOsrpvTUQuSg)

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

![SSH Keys from menu list](https://cdn-images-1.medium.com/max/800/0*-XYTx9dB2-0c2Uv5)

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](https://cdn-images-1.medium.com/max/800/0*bdi7YDIhzfFvrI1B)

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](https://cdn-images-1.medium.com/max/800/0*ry234e4mgEsTbNTd)

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](https://cdn-images-1.medium.com/max/800/0*rimOaJbyexOEZ5iq)

Step 5- Click on "**Add key**"

![Add key button](https://cdn-images-1.medium.com/max/800/0*F_P_fRc_BfRptmrC)

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](https://cdn-images-1.medium.com/max/800/0*B2xTNI96zdvtc64C)

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:**
- https://koz.tv/how-to-generate-and-copy-ssh-key-in-mac-os/
- https://jdblischak.github.io/2014-09-18-chicago/novice/git/05-sshkeys.html
- https://www.youtube.com/watch?v=kHkQnuYzwoo&ab_channel=EdGoad
- https://www.udemy.com/course/the-complete-junior-to-senior-web-developer-roadmap/
- https://techies-world.com/wp-content/uploads/2018/02/flat800x800075f.u1.jpg