DEV Community

Cover image for Secure and Seamless: Setting Up SSH Authentication with GitHub on Linux
Sujit Kumar
Sujit Kumar

Posted on

Secure and Seamless: Setting Up SSH Authentication with GitHub on Linux

Setting Up SSH with GitHub

GitHub provides SSH (Secure Shell) authentication, which allows you to securely interact with your repositories without having to enter your username and password each time. This guide will walk you through the process of setting up SSH for your GitHub account.

Step 1: Check for Existing SSH Keys

Before generating a new SSH key pair, check if you already have existing SSH keys on your system. Open your terminal and enter the following command:

ls -al ~/.ssh
Enter fullscreen mode Exit fullscreen mode

Look for files with names like id_rsa and id_rsa.pub. If they exist, you can skip to Step 3. If not, proceed to Step 2.

Step 2: Generate a New SSH Key Pair

To generate a new SSH key pair, open your terminal and run the following command:

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
Enter fullscreen mode Exit fullscreen mode
  • -t rsa: Specifies the key type (RSA).
  • -b 4096: Specifies the key length (4096 bits is recommended for security).
  • -C "your_email@example.com": Adds a comment (usually your email address) for identification.

Press Enter and follow the prompts to choose the key's location and set a passphrase if desired.

Step 3: Add the SSH Key to Your SSH Agent

The SSH agent is a program that holds your SSH keys in memory, allowing you to use them without entering your passphrase every time. To add your SSH key to the agent, use the following command:

ssh-add ~/.ssh/id_rsa
Enter fullscreen mode Exit fullscreen mode

Replace ~/.ssh/id_rsa with the path to your private SSH key if it's located elsewhere.

Step 4: Copy the SSH Public Key

Your SSH public key is used for authentication. To display it in your terminal, run:

cat ~/.ssh/id_rsa.pub
Enter fullscreen mode Exit fullscreen mode

Copy the entire contents of the public key, including the ssh-rsa prefix till including your email address.

Step 5: Add the SSH Key to Your GitHub Account

  1. Log in to your GitHub account.
  2. Click on your profile picture in the top right corner and select "Settings."
  3. In the left sidebar, click on "SSH and GPG keys."
  4. Click the "New SSH key" button.
  5. Paste your SSH public key into the "Key" field.
  6. Provide a title for the key (e.g., "My Personal SSH Key").
  7. Click the "Add SSH key" button.

Step 6: Test the SSH Connection

To ensure that your SSH key is set up correctly, open your terminal and run the following command:

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

Congratulations! You've successfully set up SSH with GitHub. You can now securely interact with your repositories without entering your username and password each time you push or pull.
Thanks for reading! ^_^ see you soon.

Top comments (0)