How to Connect SSH Key to GitHub?
Tags: sshkey, github, git, beginners
Published: 2021-12-02 13:36:35

Hello, previously we got to know about  [Introduction to Git & GitHub for Beginners](https://syedsohan.hashnode.dev/introduction-to-git-and-github-for-beginners) &  [How to Set Up Git?](https://syedsohan.hashnode.dev/how-to-set-up-git).

Today we will learn about how to connect to  [GitHub](https://github.com/)  using the **Secure Shell Protocol (SSH)**, which provides a secure channel over an unsecured network. With the help of SSH keys, we can connect to GitHub without providing our personal access (username and password) at each visit.

## What is SSH Key?

An SSH key is an access credential for the SSH (Secure Shell) network protocol. SSH is used for remote file transfer, network management, and remote operating system access. SSH keys always come in pairs, and every pair is made up of a private key and a public key.

- **Public key** – Everyone can see it, no need to protect it. (for encryption function)
- **Private key** – Stays in computer, must be protected. (for decryption function)

## Check if you have any existing SSH Key on your local computer

1. Open **Git Bash**

2. Enter `ls -al ~/.ssh` to see if existing SSH keys are present. Here you can see that I already have some SSH Keys.


![Screenshot_1.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1638450009481/vuNdI9kGm.png)


## Generate a new SSH Key

1) If there is no key then generate a new SSH key

``` $ ssh-keygen -t ed25519 -C "your_email@example.com" ```

and this will generate a new SSH key:

> Generating public/private ed25519 key pair.


2) 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_ed25519): [Press enter]

3) At the prompt, type a secure passphrase, press enter or you can give your passphrase.

> Enter passphrase (empty for no passphrase): [Type a passphrase]

> Enter same passphrase again: [Type passphrase again]

4) Then you will get something like this:

> Your identification has been saved in (/c/Users/you/.ssh/id_ed25519)

> Your public key has been saved in (/c/Users/you/.ssh/id_ed25519)



![Screenshot_2.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1638451386631/SG02azmgD.png)


## Add your SSH Key to your ssh-agent

- Start the ssh-agent in the background.

> eval "$(ssh-agent -s)"

- You will get your unique Agent pid.

> Agent pid 59566


![Screenshot_4.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1638445450091/xrwNWUbq1.png)


## Add your private SSH Key to your ssh-agent

> $ ssh-add ~/.ssh/id_ed25519

- You will get something like:

> Identity added: /c/Users/you/.ssh/id_ed25519 (your_email@example.com)



![Screenshot_5.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1638451451412/6stLxZys-.png)

## Add your SSH Key to your GitHub Account

- Copy the SSH public key `$ clip < ~/.ssh/id_ed25519.pub` to your clipboard.

![Screenshot_8.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1638446670953/PnokZY0KU.png)

- From your **GitHub Account** go to **Settings**.
- In the user settings sidebar, click **SSH and GPG keys**.
- Click the **New SSH Key** or **Add the SSH Key**.
- In the **Title** field, add a descriptive label for the new key.
- Paste your key into the **Key** field.
- Click **Add SSH Key**.
- If prompted, confirm your GitHub password.

## Test your Connection

1) Open Git Bash

2) Enter the following:

```bash
$ ssh -T git@github.com
``` 

3)  You may see a warning like this:

> The authenticity of host 'github.com (IP ADDRESS)' can't be established.

> RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.

> Are you sure you want to continue connecting (yes/no)?

4) Verify the message by typing **yes**:

> Hi username! You've successfully authenticated, but GitHub does not provide shell access.



![Screenshot_9.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1638451480153/oEg7ZV-yg.png)


## Thanks

**Thanks for reading this post. Stay tuned for more.**


> 
**You can find me [here](https://bio.link/syedsohan)  also.**

<a href="https://www.buymeacoffee.com/syedsohan" target="_blank"><center><img src="https://cdn.buymeacoffee.com/buttons/v2/default-yellow.png" alt="Buy Me A Coffee" style="height: 60px !important;width: 217px !important;"></center></a>