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

Dominika Zając
3 min readOct 18, 2019

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: https://en.wikipedia.org/wiki/Public-key_cryptography ). 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

2. 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.

3. 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)"

4. 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.

5. 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.

Dropdown with the‘Settings’ option

6. 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

7. 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

8. 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’.

9. Confirm changes with your GitHub password if needed.

10. 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!

PS. If you found this article useful, please clap the button below, leave a comment or send it to your friends. Thanks in advance!

--

--

Dominika Zając

Full-stack developer from Cracow, Poland. Google Developer Expert in Web. Passionate about a11y, best coding practices and singing.