Connect to Github Through SSH

To connect to GitHub through ssh you need to follow these steps:

Step 1. Check for existing keys

  1. Open Git Bash on Windows or Terminal on Mac.

Enter ls -al ~/.ssh to see if existing SSH keys are present:

$ ls -al ~/.ssh
  1. Lists the files in your .ssh directory if they exist
  2. Check the directory listing to see if you already have a public SSH key.

By default, the filenames of the public keys are one of the following:

  • id_dsa.pub
  • id_ecdsa.pub
  • id_ed25519.pub
  • id_rsa.pub

If you have them, skip next step and jump to Step 3. Adding SSH key to the ssh-agent.

Step 2. Generating a new SSH key

  1. Open Git Bash on Windows or Terminal on Mac.
  2. Paste the text below, substituting in your GitHub email address.
$ ssh-keygen -t rsa -b 4096 -C [email protected]


This creates a new ssh key, using the provided email as a label.

> Generating public/private rsa key pair.
  1. When you’re prompted to “Enter a file in which to save the key,” press Enter. This accepts the default file location.
> Enter a file in which to save the key (/c/Users/you/.ssh/id_rsa):[Press enter]
  1.   At the prompt, type a secure passphrase. Leave it empty.
> Enter passphrase (empty for no passphrase): [Type a passphrase]
> Enter the same passphrase again: [Type passphrase again]

 

It is done. You have a new SSH key in the specified folder. You need it for the next 2 sections.

Step 3. Adding SSH key to the ssh-agent

To  add your SSH key to the ssh-agent follow these steps:

  1. Start ssh-agent in the background.
$ eval $(ssh-agent -s)
> Agent pid 59566
  1. Add your SSH private key to the ssh-agent. If you created your key with a different name, or if you are adding an existing key that has a different name, replace id_rsa in the command with the name of your private key file.
$ ssh-add ~/.ssh/id_rsa

Great. Now you have added SSH key.

Step 4. Adding the SSH key to your GitHub account

The last step is adding the key to your github account:

  1. Copy the SSH key to your clipboard. If your SSH key file has a different name than the example code, modify the filename to match your current setup. When copying your key, don’t add any newlines or whitespace.
$ clip < ~/.ssh/id_rsa.pub
# Copies the contents of the id_rsa.pub file to your clipboard


Tip: If clip command isn’t working, you can locate the hidden .ssh folder, open the file in your favorite text editor, and copy it to your clipboard.

  1. In the upper-right corner of any page, click your profile photo, then click Settings.
  2. In the user settings sidebar, click SSH and GPG keys.
  3. Click New SSH key or Add SSH key.
  4. In the “Title” field, add a descriptive label for the new key. For example, if you’re using a personal Mac, you might call this key “Personal MacBook Air”.
  5. Paste your key into the “Key” field.
  6. Click Add SSH key.

8. If prompted, confirm your GitHub password.

That’s it. You are connected to Github through ssh and ready to deliver your exercises.