Ssh Keys For Github

Ssh Keys For Github

·

3 min read

In order to push your code changes to github from your local machine, github needs to authenticate you. Authentication is done by adding a public ssh key to your github account which, by a complex mathematical proof, matches the private ssh key that will be stored in your local machine.

Let's get to the nuts and bolts of this tutorial and let me show how to do that.

STEP 1

Ensure you have git installed in your computer

If you use windows OS, you can download the installer at gitforwindows.org.

STEP 2

Open git bash and generate an ssh key using the command below:

ssh-keygen -t rsa -b 4096 -C “youremail.com”

-t means the type of key. In our case it is rsa. For more details about the different types of keys, read this article

-b means the number of bits your key will have. In our case its 4096 which is more secure. "youremail. com" should be the email address you used to signup for a github account.

Press enter in every prompt( ofcourse you can change the file name and add a passphrase but the defaults are enough to make it work).

A folder named .ssh will be created in users>>ADMIN

There are two files in .ssh named:

  1. id_rsa
  2. id_rsa. pub

The substring rsa might be different for you.

id_rsa is the private key. You should not share it with anyone.

id_rsa. pub is the public key which we will add to github later.

STEP 3

Open git bash, type this command. eval “$(ssh-agent -s)”

You should get something like:

Agent pid 665

Type this command in git bash to create a config file for your keys.

touch ~/.ssh/config

Open the config file you just created using vim by typing the command below.

vim ~/.ssh/config

Alternatively you can open the file using any text editor.

Add the following contents to the config file:

Host *

AddKeysToAgent yes

IdentityFile ~/.ssh/id_rsa

Remember to replace rsa with the substring that your key has.

STEP 4

Copy the id_rsa. pub key by opening it in the terminal and highlighting it.

cat ~/.ssh/id_rsa.pub

On your github account, navigate to settings>>SSH and GPG keys.

Paste the copied key in SSH section after giving it a name.

STEP 5

Type the following command in git bash.

ssh-add ~/.ssh/id_rsa

Finally type the following

ssh -T git@github.com

You might get this warning

Are you sure you want to continue connecting (yes/no/[fingerprint])? yes Warning: Permanently added 'github. com' (ED25519) to the list of known hosts.

Type yes and press Enter.

If you get this message, Hi amschel99! You've successfully authenticated, but GitHub does not provide shell access. Everything went on fine and now you can push code changes to github from your terminal.

I hope this helps, Thanks for reading.

Â