How to setup ssh key on GitHub

How to setup ssh key on GitHub

Git is a distributed version control system designed to handle any size project quickly and efficiently. Git and GitHub are essential tools for developers. Most IT companies use GitHub to manage their small as well as large project. In this tutorial, we will learn about How to setup ssh key on GitHub and how we can generate and configure the SSH keys on GitHub.

There are other Git hosting services such as Bitbucket, GitLab, etc. But the most popular choice for the developers is GitHub. For each machine and server, we should use SSH keys for the secure connection.

Read Also: How to Set Up SSH Key-Based Authentication on Ubuntu 20.04 

Prerequisites

  • Linux-based operating systems like Ubuntu, CentOS, etc.
  • Access of Root user or another user with Sudo privileges.
  • Access to a GitHub Account.

For this tutorial, we are using Ubuntu 22.04 server. You guys can use any type of Linux based operating system. This tutorial will work for every Linux based operating system.

What Are SSH Keys?

SSH stands for the Secure Shell. SSH keys are the access credential which is used in the SSH protocol. It enables secure access over the internet to access remote servers and devices.

SSH is widely used for the following functions:-

  • To access remote systems
  • To execute commands on remote servers or systems in a secure and encrypted environment.
  • Secure remote delivery of software updates 
  • Secure automated and interactive file transfers

SSH keys work with two keys, private and public keys. The public key is stored on the SSH server and the private key is stored on the host where the SSH client is working.

Create an SSH Key

To create the SSH key, open the terminal and run the following command.

sudo ssh-keygen

Enter the name and path for the keys, otherwise press Enter button for the default name and path. If you want to protect the ssh keys, then enter your desired password twice in the passphrase section. Otherwise, press Enter button to skip this step.

Generate new ssh keys with ssh-keygen command

We have successfully generated the SSH keys. You can find these keys in the .ssh directory present under the Home directory of the user.

Now we need to add the public key to the GitHub for the command line access. To see the content of the public key, run the following command.

cat ~/.ssh/id_rsa.pub

Copy and save the public key. The Public key will look like this.

The generated public key

Add SSH Key to GitHub Account

  • Log into the GitHub account, click on the Avatar and then click on the Settings option.
GitHub setting option
  • Click on the SSH and GPG keys option present on the left side of the GitHub account.
SSH keys option in GitHub settings
  • Click on the New SSH key option to add the Public SSH key to the GitHub.
Add SSH key in GitHub
  • Paste the content of the SSH public key and then click on the Add SSH key button as mentioned below.
Paste the SSH public key

Test SSH Connection With GitHub

After adding the SSH key on GitHub, now we need to test the SSH connection with GitHub with the following command.

ssh -T [email protected]

You will get an output like this if everything is correctly configured.

Test the SSH connection with GitHub

Now you have successfully setup the SSH key on GitHub.

Conclusion

In today’s tutorial, we have discussed the need for SSH protocol and the SSH keys, the differences between private and public keys, and how we can generate and add them to the GitHub account. In the upcoming tutorials, we’ll share more about Git and GitHub.