About meEventsArticlesPodcastsContact

How to add an SSH key to your GitHub account and why you need it?

By Dominika Zając
Published in Articles
October 18, 2019
2 min read
How to add an SSH key to your GitHub account and why you need it?

Photo by Silas Köhler on Unsplash

Do you remember your coding world before you met git? I do! Severals files like version-1.cpp, version-2.cpp, final.cpp, final-please-work.cpp took a lot of place on my laptop when I just started coding. Then revelation — I can use a versioning system like git. Singing up to GitHub completely changed my coding convenience. It took me almost a year to discover the next big step: adding an SSH key (and now I can’t imagine my daily work without it!).

What is that?

SSH keys are nothing more than an authorization method that allows you to avoid typing the password each time when you want to connect from your trusted computer to the server (in our case to your GitHub). The method is safer than a traditional password and based on strong asymmetric cryptography (to more details about that mechanism visit Wikipedia. Everything is based on a pair of keys — public and private. The first one is sent to the server and openly distributed to anyone on the Internet, the second should be available only for you on your local machine.

How to add the SSH key to the GitHub account?

SSH keys are really popular, so Github prepared a special mechanism to make the process of adding key as simple as can be. I describe it below step by step (for Linux users):

  1. In terminal type command:
ssh-keygen -t rsa -b 4096 -C "youremail@test.com"

What does it mean?

  • ssh-keygen is a built-in tool for creating pairs of SSH keys
  • -t rsa is responsible for the selection of cryptographic algorithm (rsa is the most popular, you can also choose dsa, ecdsa or ed25519)
  • -b 4096 — is a key size (4096 bits in our case)
  • -C “youremail@test.com makes your email will be added as a comment to the keys
  1. You will be asked by the tool where the key should be located and if you want to secure it with a passphrase. In the first case, you can just click enter — it will select a default location(/home/you/.ssh/id_rsa). After that pair of keys will be generated.

  2. To avoid typing passphrase from key every time when you are using SSH communication, you can use a tool called ssh-agent. Firstly, you should start ssh-agent in the background by typing in terminal:

eval "$(ssh-agent -s)"
  1. Later, add your private key to ssh-agent by
ssh-add ~/.ssh/id_rsa

Remember to replace ~/.ssh/id_rsa with your location of the private key if you have chosen a non-default location.

  1. Now, you can add the public key to your GitHub account. Go to the web version of Github, click on the arrow in the top right corner (next to your avatar) and choose the ‘Settings’ option from the dropdown.

Settings in GitHub Dropdown with the‘Settings’ option

  1. In the menu on the left side choose “SSH and GPG keys” and click on the green ‘New SSH key’ button at the top.

‘SSH and GPG keys’ tab in GitHub settings ‘SSH and GPG keys’ tab in GitHub settings

  1. Go back to your terminal, install xclip and use it to copy your public key to your clipboard.
sudo apt install xclip
xclip -sel clip < ~/.ssh/id_rsa.pub
  1. Paste copied key in the ‘Key’ area and add a meaningful title for your key (e.g. personal_laptop). At the end click the green button ‘Add SSH key’.

  2. Confirm changes with your GitHub password if needed.

  3. Voilà! You’ve just added your SSH key to GitHub. Now you can programming in a more comfortable and safer way!

Good luck and happy coding!


Previous Article
How to contribute to the open-source project?
Dominika Zając

Dominika Zając

Full-stack developer

Related Posts

Comments in the code
June 05, 2022
5 min
Icons made by Freepik
from www.flaticon.com

© 2022, All Rights Reserved.

Quick Links

About meContact

Social Media