Connecting Your Linux Machine to GitHub Using SSH

In this blog, I’ll guide you step-by-step on how to connect your Linux machine to GitHub using SSH. This method allows for secure, password-free authentication, making your Git operations more seamless. Whether you are cloning, pushing, or pulling repositories, having an SSH connection can make things much smoother.

Why Use SSH?

SSH (Secure Shell) enables encrypted communication between your local machine and GitHub, ensuring security and privacy. By setting up SSH keys, you can authenticate Git operations without repeatedly entering your GitHub credentials.

Prerequisites

  1. A GitHub account.

  2. A Linux machine (I'm using Ubuntu 22.04, but these instructions should work for other distributions too).

  3. Basic understanding of terminal commands.

Let’s dive into the steps!


Step 1: Check for Existing SSH Keys

Before creating a new SSH key, it’s good to check if you already have one. Open your terminal and type:

ls -al ~/.ssh

This will list any existing SSH keys. If you see files like id_rsa and id_rsa.pub, you already have a key pair. If not, or if you want to generate a new key, proceed to the next step.


Step 2: Generate a New SSH Key

To generate a new SSH key, use the following command:

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

Replace your_email@example.com with the email address associated with your GitHub account.

You will be prompted with:

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

Press Enter to accept the default file location.

Next, you will be asked to enter a passphrase. You can set a passphrase for an extra layer of security, but it's optional. If you don’t want to set a passphrase, just press Enter.


Step 3: Add SSH Key to the SSH Agent

Now that we have the SSH key, we need to add it to the SSH agent, which manages your keys.

Start the SSH agent:

eval "$(ssh-agent -s)"

Next, add your private SSH key to the agent:

ssh-add ~/.ssh/id_rsa

This will store the key in memory, so you won’t have to enter the passphrase every time you use the key.


Step 4: Add SSH Key to Your GitHub Account

Now, you need to add the SSH key to your GitHub account.

First, copy the contents of your public SSH key to your clipboard:

cat ~/.ssh/id_rsa.pub

This will display your public key in the terminal. Copy the entire output.

Next, go to GitHub:

  1. Log in to your GitHub account.

  2. Click on your profile picture in the top-right corner and go to Settings.

  3. On the left sidebar, click on SSH and GPG keys.

  4. Click the New SSH key button.

  5. In the Title field, give your key a recognizable name (e.g., "My Linux Machine").

  6. Paste the SSH key you copied earlier into the Key field.

  7. Click Add SSH key.

You might be prompted to enter your GitHub password for verification.


Step 5: Test the SSH Connection

To ensure everything is working correctly, test the connection between your Linux machine and GitHub by running:

ssh -T git@github.com

You may get a message like this:

The authenticity of host 'github.com (IP address)' can't be established.
Are you sure you want to continue connecting (yes/no)?

Type yes to continue. You should then see a message like:

Hi username! You've successfully authenticated, but GitHub does not 
provide shell access.

This means your SSH key has been successfully added, and your Linux machine is now connected to GitHub.


Step 6: Using SSH with Git

You can now clone repositories via SSH and push/pull code securely. For example, to clone a repository using SSH, run:

git clone git@github.com:username/repository.git

From now on, whenever you push or pull code from this repository, Git will use your SSH key for authentication.


Conclusion

And that’s it! You’ve successfully set up SSH authentication between your Linux machine and GitHub. This setup not only saves you from entering your credentials repeatedly but also ensures your connection is secure.

Feel free to leave any questions or feedback in the comments. Happy coding!


0
Subscribe to my newsletter

Read articles from Biswnath Mukherjee directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Biswnath Mukherjee
Biswnath Mukherjee