DEV Community

Cover image for How to set up SSH keys and connect them to GitHub

How to set up SSH keys and connect them to GitHub

augustocarmona profile image Augusto Carmona Perez ・4 min read

Ever wonder what SSH is for?

Every time we connect to GitHub, to be able to do a push from our repository we have to enter our password. But this has two problems: the first is that we always have to do it (every time we need to git push) -if you are going to use long and complicated passwords, like me, it can be quite annoying-, and the second is that in principle, we are working with secure connections (https).
However, when working in this way, the username and password are stored in the local environment, so if at any point our laptop is stolen, our repositories will become highly vulnerable.

HTTPS protocol

So to protect the projects we have on GitHub and our work, maybe the best option is to create a public and private key environment on Git and Github.

If we clone our repositories with SSH, not only will the security be much more stronger, but we will not have to re-enter our password to be able to git push.
To do this we must create a public key and a private key in our local environment, and once created we must send our public key to GitHub, in this way we can use the SSH protocol at all times.
This connection is not only 100% encrypted, but we can also add a new passphrase to make it more secure ...

SSH protocol

Generate a new SSH key: (Any operating system)

In your console you just have to enter the following command (with the email you have associated with your GitHub account):

ssh-keygen -t rsa -b 4096 -C ""

This command generates the key and then asks to be indicated in which part of our system it should save said key (I suggest simply to press enter, since the default location it offers us is our user).


After this, it will ask for a passphrase (a password with spaces), for example: "7h1s is 4 pa55phr4se ".
The passphrase is optional, but I suggest you add it. They will not be asked to git push.

From now on, your public and private keys are already created, you just need to add them to your environment...

Check process and add it to the environment (Windows and Linux)

eval $ (ssh-agent -s)

Check that the SSH keyserver is on. It will return agent pid -and a number- and then you can continue.

ssh-add ~/.ssh /id_rsa

Finally add your private key with this command, it is located in home, in the ssh folder, and the key is called id_rsa ( is the public key, we will use it later).

Here you are adding the private key to the system. You should never copy and paste this private key anywhere or show it to anyone, just add it here.

Check Process and Add It to Environment (Mac)

  • eval "$ (ssh-agent -s)"

Again, verify that the SSH keyserver is turned on. It will return agent pid -and a number- and then you can continue.

Are you using macOS Sierra 10.12.2 or higher? You have to modify (or create) the config file.

This file is in the same folder as the SSH keys (with ls -al you can see if it exists)

You have to do the following:

  • cd ~/.ssh to go to the SSH folder
  • If config doesn't exist, create one ...
  • With Vim: vim config (to work on the console, in my opinion, this is the best editor)
  • With VSCode: code config

  • Paste the following configuration into the file ...
Host *
 AddKeysToAgent yes
 UseKeychain yes
 IdentityFile ~/.ssh/id_rsa
Enter fullscreen mode Exit fullscreen mode
  • Add your key: ssh-add -K ~/.ssh/id_rsa

SSH keys are not per repository or project, but per person, so I recommend following this process for each computer that you want to have connected to GitHub.

Connecting to GitHub with SSH:

Find your public key where you have it stored (your .ssh folder, and it's called, open it with any editor (it can be VsCode or Vim), and copy it.

After this, go to your GitHub profile, to the settings tab, option SSH and GPG Keys,New SSH Key, name it as you like: "Dell Linux for example" and there paste your key public in Key.

Hit Add SSH Key, enter your password and voila!

From now on, whenever you want to clone a repository on GitHub instead of connecting it with the HTTPS protocol, do it with the SSH protocol and you will no longer have to re-enter passwords every time you git push. And it's a thousand times more secure!

Discussion (2)

aghost7 profile image
Jonathan Boudreau

I like to have different keys for each service I use. You can do this by specifying the host like so in your ssh config:

    User git
    IdentityFile ~/.ssh/github
Enter fullscreen mode Exit fullscreen mode
augustocarmona profile image
Augusto Carmona Perez Author

Thanks! It is a very good idea to have different keys!

Forem Open with the Forem app