How to generate a new SSH key

How to generate a new SSH key

In this article, I share how to create and use a new SSH key to authenticate Git operations on GitHub.

Quick question.

What is SSH?

"SSH, Secure Shell in full, is a network protocol that is used for secure login from one computer to another." Another useful definition I found on Hostinger says, SSH is a remote administration protocol that allows users to access, control, and modify their remote servers over the internet.

One example of an application that uses the SSH protocol is GitHub.

Moving away from password-based authentication on GitHub

There are three ways GitHub authenticates Git operations.

  • Username and password with two-factor authentication
  • Personal access token
  • SSH key

However, GitHub announced that they would be phasing out password-based authentication for security enhancement reasons.

It is common to recycle passwords across multiple applications which makes it easier for attackers to try to regain access to say your GitHub account using your recycled password. With token-based authentication, there is more security due to the following characteristics:

  • Tokens are unique per user session or per device.
  • Tokens are randomly generated.
  • Tokens are encrypted and machine-generated making them stronger than human-generated passwords.

Generating a new SSH key

To generate a new ssh key, run ssh-keygen command in your terminal. Follow the instructions returned in the cmd. It will produce the following output.

$ ssh-keygen 

Generating public/private rsa key pair. 

Enter file in which to save the key (/home/pc_name/.ssh/id_rsa):

Enter passphrase (empty for no passphrase):  

Enter same passphrase again:

Your identification has been saved in /home/pc_name/.ssh/id_rsa.
Your public key has been saved in /home/pc_name/.ssh/id_rsa.pub.
The key fingerprint is: SHA256:Up6KjbnEV4Hgfo75YM393QdQsK3Z0aTNBz0DoirrW+c pc_name@belinda
The key's randomart image is:
+---[RSA 2048]----+ |    .      ..oo..| |   . . .  . .o.X.| |    . . o.  ..+ B| 
|   .   o.o  .+ ..| |    ..o.S   o..  | |   . %o=      .  | |    @.B...     . |
|   o.=. o. . .  .| |    .oo  E. . .. | +----[SHA256]-----+ pc_name (11:40)

You will need to enter a passphrase you can recall. It will be used as the password to access the private key on your computer.

Get the public key stored in this path /home/pc_name/.ssh/id_rsa.pub and add it as a new SSH key under your Github settings.

Authenticating Git operations on GitHub with your new SSH key

When creating a new git repository, you are provided a set of git commands to run in your local terminal. The commands look like this:

creating a new repository on Github.png

As you can see in that image, there are two ways of setting up the repo. Using the SSH protocol or the HTTPS protocol. If you already set up your repo with HTTPS protocol, no worries. You can easily add the SSH remote url path by clicking the Code button in your project's GitHub repo.

choosing https or ssh protocol on github.png Copy the link and then add that to your local repo using the following command

$ git remote set-url origin git@github.com:Codebmk/nextjs-crud-app.git

In case you didn't add save your passphrase in the SSH agent, you will be prompted to enter it every time you need to connect to GitHub. This is good in a way because it adds an extra layer of security in case an attacker gains access to your computer. If you add the passphrase to the SSH agent, you will not need to enter the passphrase everytime you need to connect to GitHub.


Thank you for reading till the end. Like, leave a comment, share with your network if you found this piece helpful.

Resources:

  1. About authentication to GitHub
  2. About remote repositories
  3. What is SSH?

Au revoir!