DEV Community

Cover image for How to Connect SSH Key to GitHub?
Syed Sohan
Syed Sohan

Posted on • Originally published at syedsohan.hashnode.dev

How to Connect SSH Key to GitHub?

Hello, previously we got to know about Introduction to Git & GitHub for Beginners & How to Set Up Git?.

Today we will learn about how to connect to GitHub 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

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

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

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

Add your SSH Key to your GitHub Account

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

Screenshot_8.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:

$ ssh -T git@github.com
Enter fullscreen mode Exit fullscreen mode

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

Thanks

Thanks for reading this post. Stay tuned for more.

You can find me here also.

Buy Me A Coffee

Top comments (2)

Collapse
 
peter279k profile image
peter279k

Another recommended tip is as follows:

  • We can also consider generating the RSA key pairs with 4096 bits.

ssh-keygen 4096 non-interactive mode

  • If we don't want to generate RSA key pairs interactively, we can consider following command.

ssh-keygen with non-interactive

ssh-keygen key pairs directory

Collapse
 
syedsohan profile image
Syed Sohan

Thanks for sharing.