DEV Community

Saravanan Gnanaguru
Saravanan Gnanaguru

Posted on • Updated on

How to Setup SSH key for Git Operations in GitHub

Table of Contents

Introduction

  • In this blog, we will see how to setup password-less Git operation for our GitHub repos
  • Starting from Aug-13-2021 GitHub stopped supporting password authentication for repository related Git Operations.
  • Instead, token-based authentication methods like personal access, OAuth, SSH Key, or GitHub App installation token will be required for all authenticated Git operations.
  • In this blog, we will see how to setup SSH key based authentication in GitHub for doing Git operations

Summary of Steps

  • Step 1: Create SSH keys for your workstation using ssh-keygen command
  • Step 2: Copy the SSH Public Key into GitHub Settings
  • Step 3: Use SSH URL for the repository to clone

Create SSH Keys

I'm using Ubuntu OS workstation and performed the steps in it.

  • Open terminal and Run ssh-keygen command The command creates following two files in $USER_HOME/.ssh directory,
  • id_rsa private key file, and
  • id_rsa.pub public key file
$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key ($USER_HOME/.ssh/id_rsa):
Enter fullscreen mode Exit fullscreen mode

Then press enter, to move further,

  • It is important to Enter passphrase for your SSH key, to use the key for cloning private repo from GitHub
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Enter fullscreen mode Exit fullscreen mode

Finally, we can see the below output, which shows the path where the public and private key files are stored

Your identification has been saved in $USER_HOME/.ssh/id_rsa.
Your public key has been saved in $USER_HOME/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:VRqScsqwertyuioXGpazvfrw username@workstation_name
The key's randomart image is:
+---[RSA 2048]--+
| .+. o=o .     |
|.. o.oo.+      |
|o .   oo+ o    |
|.+   o -= +    |
|o   . o S      |
| .o+   o .     |
|.o..+ o O.     |
|o.o .-- =O.*.  |
|.+..E ooO.     |
+----[SHA256]---+
Enter fullscreen mode Exit fullscreen mode

Copy Public key to GitHub Settings

  • Goto GitHub Settings

GitHub Settings

  • Select SSH and GPG key from left pane

SSH and GPG key setting

  • Select New SSH key

New SSH key

  • Copy your workstation's (id_rsa.pub) public key content here

Paste pub key content

  • Save and the key will be listed in the SSH key section

Clone Repo using SSH URL

  • Now you can use the SSH URL to clone the repository

Clone using ssh url

  • While cloning, you would be prompted to enter the SSH key password
$ git clone git@github.com:github_userid/repo_name.git
Cloning into 'repo_name'...
Enter passphrase for key '$USER_HOME/.ssh/id_rsa': 
Checking connectivity... done.
Enter fullscreen mode Exit fullscreen mode
  • Once you enter your SSH key password, the repo will be cloned in your workstation

Issue and Resolution

  • Sometime we will get below error while updating the old repo, which was previously cloned using http and username/password,
`fatal: could not read Username for 'https://github.com': terminal prompts disabled`
Enter fullscreen mode Exit fullscreen mode
  • Use the command to get rid of the issue.
# To avoid the error 
# we need to change gt config to use ssh/git method
git config --global --add url."git@github.com:".insteadOf "https://github.com/"
Enter fullscreen mode Exit fullscreen mode
  • Windows WSL and Ubuntu Permission denied (publickey) error.
  • When you setup Github login in windows using WSL and Ubuntu make sure to create the SSH key file using the name id_rsa only. If we try to use other names as key file name, then we will get Error: Permission denied (publickey) error.
  • Refer the docs here

Bibliography and Reference

GitHub Docs for Working with SSH key

GitHub Docs for cloning repo using SSH URL

Creating SSH key for Windows 10

Top comments (0)