Nazmuz Shakib Pranto
Posted on December 4, 2021
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:
Open Terminal (CMD + SPACE, search for "terminal", open Terminal.app)
Run
cd ~/.ssh
Run
ls
If you see
id_rsa.pub
file, then you already have an SSH key (no need to generate a new one)To generate a new key, run
ssh-keygen -t rsa -b 4096 -C "<EMAIL>"
(replace<EMAIL>
with your email)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
)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)
Finally, you should see a confirmation stating that your identification and the public key have been saved.
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"
Step 4- Now, from the left vertical menu list, click on "SSH and GPG keys"
Step 5- Under the SSH keys section, click on the green button - "New SSH key"
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
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"
Step 4- Now, from the left vertical menu list, click on "SSH Keys"
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
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"
Step 4- Now, under the "Personal settings" menu list on the left side, click on "SSH keys" under the "Security" section
Step 5- Click on "Add key"
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
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
Posted on December 4, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.