Connect to GitHub using SSH

Mostafa Brazi
3 min readSep 27, 2021

Imagine with me a secure access to your Github account from you local machine without supplying your username/password each time you visit your projects/repositories. Absolutely possible 👍

Let’s see how we can setup such authentication.

Note: command line used in this article are for the mac OS

Do I have an SSH key ?

First you need to check that your local machine has an SSH key:

  • List all files under .ssh folder ls -al ~/.ssh
  • If the list contains files such: id_rsa.pub or id_ecdsa.pub or id_ed25519.pub then you already have an ssh key, in this case skip next step.

If not generate one and add it to the ssh-agent. To do so, follow the steps below:

1- Open the terminal and run the command

ssh-keygen -t ed25519 -C "xxx@yyy.com"
  • Replace xxx@yyy.com with your Github email)
  • In case Ed25519 algorithm not supported on your OS, use the command below instead :ssh-keygen -t rsa -b 4096 -C "xxx@yyy.com"
  • You have the choice to keep the default file name or change it and then you will be asked to enter a passphrase

2- Add the key to the ssh-agent following this step-by-step from the official Github docs page “Adding your SSH key to the ssh-agent

Now we finished with our SSH key, Let’s link it in the Github server.

Link the SSH key to my Github account

  • Login to your Github account on the web
  • Click on the top right profile photo and go to Setting
Github — settings
SSH and CPG keys
Add new SSH key
  • You will be prompted to the screen below.
Add new SSH key
  • As a title, you need to specify something that will help you remember which machine this key is created for (for example: my-macbook-air, my-dell-inspiron-5050, pc-hp-993,…)

993 was random 😅 not sure hp 993 exists😜😜

  • Now you need to go back to the terminal, copy the content of the public key by running pbcopy < ~/.ssh/id_ed25519.pub and paste it in ‘Key’
  • Save by clicking ‘Add SSH key

Now you are ready to access you Github account from your machine without any username / password authentication.

As an example, let’s clone a repository

Go to SSH tab and copy your repo url
git clone git@github.com:<your_username>/<your_repository>.git

After your first successful access, you will notice that Github marked the key as used.

Github will give you the last used date of your key

Let me know in the comments if it was helpful 😃

Thank you guys.

--

--