Setting up SSH authentication for GitHub

To add a SSH key to GitHub it is necessary to work both in your computer and in GitHub web page. Lets start with the computer from which you want to commit code to your GitHub repositories. Later we will deal with GitHub.

@ your computer

First start by generating a ssh key. To generate a ssh key got to the command line and type:

$ ssh-keygen 
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/cadoado/.ssh/id_rsa): /Users/cadoado/.ssh/github_rsa
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /Users/cadoado/.ssh/github_rsa.
Your public key has been saved in /Users/cadoado/.ssh/github_rsa.pub.
The key fingerprint is:
SHA256:5egVnuUSe4NKS4l8HU4mnioQk4sacmjI3upq9eQ9Bfk cadoado@Cados-MBP
The key's randomart image is:
+---[RSA 2048]----+
|                 |
|   .             |
|  +     o B .    |
|oo + . = & X     |
|*o+   o S @ +    |
|=o.o . * E o .   |
|....= o =        |
| ..  + o         |
|=o      .        |
+----[SHA256]-----+

The passphrase can be an empty string (just press Enter). Now you can check the key pair was generated:

ls -la ~/.ssh/
total 48
drwx------   8 cadoado  staff   256  9 Jul 23:31 .
drwxr-xr-x+ 24 cadoado  staff   768  9 Jul 23:07 ..
-rw-r--r--   1 cadoado  staff    46  9 Jul 23:07 config
-rw-------   1 cadoado  staff  1823  9 Jul 23:31 github_rsa
-rw-r--r--   1 cadoado  staff   399  9 Jul 23:31 github_rsa.pub
-rw-------   1 cadoado  staff  1843  9 Jul 10:06 id_rsa
-rw-------   1 cadoado  staff   413  9 Jul 10:06 id_rsa.pub
-rw-r--r--   1 cadoado  staff   799  9 Jul 14:34 known_hosts

The file github_rsa is the private part, that you shell never ever disclose. The file github_rsa.pub is the public part, that you distribute whenever requested to.

Assign the key to GitHub editing – or creating – a config file:

$ vi ~/.ssh/config

Add these lines to the config file (here you must refer to the private part of the key):

Host github.com
  AddKeysToAgent yes
  UseKeychain yes
  IdentityFile ~/.ssh/github_rsa

It is essential assign the correct permissions on these files:

$ chmod 644 ~/.ssh/config
$ chmod 600 ~/.ssh/github_rsa
$ chmod 644 ~/.ssh/github_rsa.pub

Finally add the key to your keyring:

$ ssh-add -K ~/.ssh/github_rsa

@ GitHub

In GutHub select Settings:

image showing where settings are located in main menu

Select SSH and GPG keys:

select ssh from settings menu

Press New SSH key button:

button nwe ssh key

Finally copy/paste the public part of the previously generated key. Give the key a name because in the future you may end up with several keys (for each device you want to commit code from)

insert public part of generated key

Final remarks

Now you can communicate conveniently and securely with GitHub, avoiding constant typing of your user/password.

You may need to reset the project origin in your local machine, please refer to the post Changing GitHub project origin.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s