DEV Community

Uthsob Chakraborty
Uthsob Chakraborty

Posted on • Updated on

How to Add an SSH Key for GitHub on Linux

GitHub is a popular platform for version control and collaborative software development. To securely access and interact with your GitHub repositories from a Linux machine, you can use SSH keys for authentication. This article will guide you through the process of generating and adding an SSH key to your GitHub account on a Linux system.

Step 1: Check for Existing SSH Keys:

Before generating a new SSH key, it's essential to check if you already have one. Open a terminal and use the following command to list your SSH keys:

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

If you see files named id_rsa or id_dsa in the output, you likely already have SSH keys. You can skip to Step 4 to add your existing key to GitHub. If not, continue to generate a new SSH key.

Step 2: Generate a New SSH Key:

To generate a new SSH key, open a terminal and enter the following command, replacing <your_email@example.com> with the email associated with your GitHub account:

ssh-keygen -t ed25519 -C "<your_email@example.com>"
Enter fullscreen mode Exit fullscreen mode

You can also use the RSA algorithm (-t rsa) instead of Ed25519 if you prefer.

The command will prompt you to choose a location to save the key (press Enter to accept the default location ~/.ssh/id_ed25519 or specify a different location) and set a passphrase for added security. Make sure to remember this passphrase or store it securely.

Step 3: Add the SSH Key to SSH-Agent:

To ensure your SSH key is actively used, add it to the SSH-Agent:

eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
Enter fullscreen mode Exit fullscreen mode

Replace ~/.ssh/id_ed25519 with the path to your generated SSH key if it's different.

Step 4: Add the SSH Key to GitHub:

  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. Provide a title for your SSH key (e.g., "Linux Workstation") and paste your public key (usually found in ~/.ssh/id_ed25519.pub) into the "Key" field.
  6. Click the "Add SSH key" button.

Step 5: Test the SSH Key:

To make sure your SSH key is working correctly, run the following command in your terminal:

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

You may see a warning message regarding authenticity; type "yes" to confirm. If everything is set up correctly, you should receive a "Hi ! You've successfully authenticated..." message.

Adding an SSH key for GitHub on a Linux system enhances the security and convenience of accessing your repositories. By following the steps outlined in this article, you can securely connect to your GitHub account, making it easier to manage your code and collaborate with others in the GitHub community.

Top comments (1)

Collapse
 
abc_wendsss profile image
Wendy Wong

Hi Uthsob, Welcome to the DEV Community and publishing your first article on DEV. Really enjoyed your article on sharing tips on SSH authentication on Github with code snippets. Please feel free to include up to 4 more hashtags so that your article can reach a wider audience. ๐ŸŒ