How to Setup SSH-Key in GitHub for Git Operations
Tags: git, github, beginners, tutorial
Published: 2022-05-02 16:43:18

## How to Setup SSH-Key for Git Operations
### Table of Contents
  - [Introduction](#introduction)
  - [Summary of Steps](#summary-of-steps)
  - [Create SSH Keys](#create-ssh-keys)
  - [Copy Public key to GitHub Settings](#copy-public-key-to-github-settings)
  - [Clone Repo using SSH URL](#clone-repo-using-ssh-url)
  - [Issue and Resolution](#issue-and-resolution)
  - [Bibliography and Reference](#bibliography-and-reference)

## Introduction
- In this blog, we will see how to setup passwordless Git operation for our GitHub repos
- GitHub stopped supporting password authentication for repository related Git Operations [from Aug-13-2021](https://github.blog/changelog/2021-08-12-git-password-authentication-is-shutting-down/)
- 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):
```
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: 
```
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]---+
```
## Copy Public key to GitHub Settings
- Goto GitHub Settings

![GitHub Settings](https://github.com/chefgs/repo_images/blob/master/gh_sshkey/ghsettings.png?raw=true)

- Select `SSH and GPG key` from left pane

![SSH and GPG key setting](https://github.com/chefgs/repo_images/blob/master/gh_sshkey/sshkeysetting.png?raw=true)

- Select `New SSH key`

![New SSH key](https://github.com/chefgs/repo_images/blob/master/gh_sshkey/newkey.png?raw=true)

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

![Paste pub key content](https://github.com/chefgs/repo_images/blob/master/gh_sshkey/newkeyadd.png?raw=true)

- 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](https://github.com/chefgs/repo_images/blob/master/gh_sshkey/clone.png?raw=true)

- 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.
```
- Once you enter your `SSH key password`, the repo will be cloned in your workstation

## Issue and Resolution
### Issue 1
- 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`
```

### Resolution for issue 1
- Use the command to get rid of the issue.
- We need to do `git configure` on the repo and change URL config,
```
# 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/"
```

### Issue 2
- Another issue might be due to Organisations enabled with SSO auth for GitHub. Below error may appear,
```
ERROR: The `Acme Corp' organization has enabled or enforced SAML SSO. To access
this repository, you must use the HTTPS remote with a personal access token
or SSH with an SSH key and passphrase
that has been authorized for this organization. Visit
https://docs.github.com/articles/authenticating-to-a-github-organization-with-saml-single-sign-on/ for more information.

fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
```

### Resolution for issue 2
- So we need to **authorize** our system SSH-KEY to use for the specific Organisations. Please find more [details here](https://docs.github.com/en/enterprise-cloud@latest/authentication/authenticating-with-saml-single-sign-on/authorizing-an-ssh-key-for-use-with-saml-single-sign-on)

## Conclusion
- In this blog we discussed setting up GitHub ssh keys to perform password-less login to do Git operations

- This article was previously published on my dev community personal profile, re-publishing for the benefit of a wider audience.
- Hope this article is helpful to people getting started with kubernetes concepts.

## Bibliography and Reference
[GitHub Docs for Working with SSH key](https://docs.github.com/en/github/authenticating-to-github/connecting-to-github-with-ssh/working-with-ssh-key-passphrases)

[GitHub Docs for cloning repo using SSH URL](https://docs.github.com/en/get-started/getting-started-with-git/about-remote-repositories#cloning-with-ssh-urls)

[Authorizing an SSH key for SSO Enabled Orgs](https://docs.github.com/en/enterprise-cloud@latest/authentication/authenticating-with-saml-single-sign-on/authorizing-an-ssh-key-for-use-with-saml-single-sign-on)

[Creating SSH key for Windows 10](https://ubuntu.com/tutorials/ssh-keygen-on-windows#3-key-generation-with-ubuntu-on-wsl)

### Follow me on,
- [GitHub](https://github.com/chefgs)
- [LinkedIn](https://www.linkedin.com/in/saravanan-gnanaguru-1941a919/)
- [Twitter](https://twitter.com/saransid)
- [gsaravanan.dev](https://gsaravanan.dev)