Sign in
Log inSign up

Setting up GitHub authentication using SSH.

Hubert Nare's photo
Hubert Nare
·Nov 20, 2021·

4 min read

Hello friend, so you want authenticate your GitHub but you don't know how to? Well I got you, let's dive right in to it.

So how do we go about it? Well, we have to create an SSH key pair to begin with. This so called SSH key pair consists of one private key and one public key, making a complete SSH "key pair". From our SSH key pair, were are going to pass one of the key to your GitHub profile, that is the public SSH key and other remaining key, the private SSH key is passed to what we call an "ssh-agent", sounds fair enough huh?. So who is this ssh-agent anyway? Okay, take the ssh-agent is the guy who is responsible for all the negotiations with GitHub so that we don't have to go through all the hustle of passing in our username and personal access token (PAT) each time we want to communicate with GitHub from our Terminal, the "ssh-agent" does all the dirty work for us. How cool is that?

"The ssh-agent is a helper program that keeps track of user's identity keys and their passphrases. The agent can then use the keys to log into other servers without having the user type in a password or passphrase again. This implements a form of single sign-on (SSO)." read more from SSH Academy

Okay enough of this, let's get working...

  • First and foremost make sure you installed Git on your pc, to do that goto git bash page and download Git Bash installation file and hit install.
  • After installing Git Bash you need to setup your name and email.

To setup your name and email follow the two steps below :)

  1. Setting up your name; copy and paste the command below and replace 'Your Name' with your actual name quoted in brackets in your Terminal.
    git config --global user.name == 'Your Name'
    
  2. Setting up your email; copy and paste the command below and replace 'Your Email' with your email address in your Terminal.

    git config --global user.email == 'Your Email'
    

    Now let's create our Git SSH key, shall we?

    In your terminal, paste the command below and hit enter;

    ls -al ~/.ssh
    

    If the command yields results like 'id_rsa' or 'id_rsa.pub' then you dont need to generate a new key your are all set to go to the following step.

Generating a new rsa SSH key pair;

  1. copy and paste the command below to generate a new SSH key;

    ssh-keygen -t rsa -b 4096 -C 'Your_Email_Address'
    

    -t refers to SSH key type (in this case we're using the rsa SSH key type). -b refers to size of SSH key in bits, The minimum bit length is 768 bits and the default length is 2048 bits. So in the command above we're setting the SSH key to a size of 4096 bits.

  2. When you press enter you will be prompted to enter a "passphrase".

  3. A passphrase is not mandatory. However, it is recommended that you specify a passphrase to protect your private key against unauthorized use (just press enter to skip this for now).

  4. Congrats, the command above just generated an SSH key pair for you consisting of a public/private rsa key pair, and saved it for you in a specific path.

This is the moment you get to ask yourself are we done here? Well no, just a few steps left please bear with me..

Now let's add our public SSH key to the ssh-agent. But wait! We have to start the ssh-agent first by pasting the command below;

eval `ssh-agent -s`

Done?

Okay now you can go on to adding that public SSH key to ssh-agent using the command below;

ssh-add ~/.ssh/id_rsa

Okay so were going to finish off by just copying our private key from our newly generated SSH key pair to your Github profile.

Copy the public SSH key to clipboard by running the command below, this is the key that you are going to paste to GitHub.

clip < ~/.ssh/id_rsa.pub

Note that the public rsa SSH key filename id_rsa.pub has a .pub extension and the private has no extension.

Finally goto your Github profile > Settings > SSH and GPG keys > New SSH key (button) > Add SSH key title of your choice > paste the key press ctrl + v combination to achieve this. > press Add SSH key (button).

Settings. Settings.jpg

SSH and GPG keys. SSH and GPG keys.jpg

Click on 'New SSH key' button. New SSH Key btn.jpg

Now;

  1. enter SSH key title of your choice.
  2. paste in the Public SSH key you copied to clipboard in your terminal.
  3. Click on 'Add SSH key' button. Title and Key.jpg

If you get prompted to pass in your password just follow through and hit 'Confirm Password'. Password comfirmation.png

Wrapping up!

Run this last command below you to test for connection.

ssh -T git@github.com

That's it!, you have successfully added a new SSH key to your Github profile.