Generate and Add SSH Key in GitHub

Yashraj singh
4 min readSep 2, 2023

--

Photo by Gabriel Beaudry on Unsplash

Tables of Contents

  1. Introduction to GitHub
  2. What Is an SSH Key?
  3. Generating an SSH Key Pair
    3.1. Checking for Existing SSH Keys
    3.2. Generating a New SSH Key
    3.3. Adding a Secure Passphrase (Optional)
  4. Adding Your SSH Key to GitHub
    4.1. Copying Your SSH Public Key
    4.2. Adding the SSH Key to Your GitHub Account
    4.3. Testing Your SSH Key
  5. Managing Multiple SSH Keys (Optional)
  6. Related Articles
  7. Conclusion

Introduction

In this guide, we will walk you through the process of generating and adding an SSH key to your GitHub account. SSH keys are a secure way to authenticate and interact with your GitHub repositories without the need for a username and password. By the end of this tutorial, you’ll have a clear understanding of how to create an SSH key and link it to your GitHub account, enhancing the security and convenience of your GitHub workflow.

What is an SSH Key ?

SSH (Secure Shell) keys are a pair of cryptographic keys that provide a secure way to authenticate and encrypt communications between your local system and remote servers or services, such as GitHub. An SSH key consists of two parts: a private key that should be kept secret and a public key that you can share with services like GitHub.

Generating an SSH Key Pair:

Before you can start using SSH keys with GitHub, you need to generate a key pair. Here’s how you can do it:

Checking for Existing SSH Keys:

Before generating a new key pair, it’s a good practice to check if you already have existing SSH keys. Open your terminal or command prompt and enter the following command:

Windows

dir /a "C:\Users\<user_name>\.ssh"

Linux or MacOS

ls -al ~/.ssh

Generating a New SSH Key:

If you don’t have existing SSH keys, you can generate a new pair using the following command:

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

This command generates a new RSA key pair with a 4096-bit key length. Replace “your_email@example.com” with your GitHub-associated email address.

Adding a Secure Passphrase (Optional):

You can add an extra layer of security to your SSH key by creating a passphrase. It’s optional but highly recommended. When you add a passphrase, anyone with access to your private key will need to know the passphrase to use it.

Adding Your SSH Key to GitHub:

Once you have your SSH key pair ready, you can add the public key to your GitHub account:

Copying Your SSH Public Key:

To copy your public key to your clipboard, use the following command

Windows

type C:\Users\<user_name>\.ssh\id_rsa.pub

Linux / MacOS

cat ~/.ssh/id_rsa.pub 

Adding the SSH Key to Your GitHub Account:

  1. Log in to your GitHub account.
  2. Click on your profile picture in the upper right corner and select “Settings.”
  3. In the left sidebar, click on “SSH and GPG keys.”
  4. Click the “New SSH Key” button.
  5. Give your SSH key a descriptive title.
  6. Paste your copied SSH key into the “Key” field.
  7. Click the “Add SSH Key” button.

Testing Your SSH Key:

To ensure that your SSH key is correctly set up, you can test it by running the following command:

ssh -T git@github.com

If everything is set up correctly, you should receive a message confirming your successful authentication.

Managing Multiple SSH Keys (Optional):

If you use multiple GitHub accounts or services, you may need to manage multiple SSH keys. You can do this by creating a SSH config file and specifying which key to use for each repository or service.

Related Articles

Below, you’ll find some related articles I’ve written on GitHub. You can explore them if they are relevant to your needs.

Conclusion:

Adding an SSH key to your GitHub account is a crucial step in enhancing the security and convenience of your GitHub workflow. With SSH keys, you can securely interact with your repositories without the need for passwords. This guide has provided you with a step-by-step walkthrough of generating, adding, and testing an SSH key, ensuring that you can confidently use this secure authentication method with GitHub.

--

--