DEV Community

John  Ajera
John Ajera

Posted on • Edited on

16

How to Configure SSH for GitHub Authentication on Linux

How to Configure SSH for GitHub Authentication on Linux

Secure Shell (SSH) authentication is the recommended way to securely connect to GitHub repositories without needing to enter your credentials every time. In this guide, you'll learn how to configure SSH authentication for GitHub on Linux step by step.

Step 1: Check for Existing SSH Keys

Before generating a new SSH key, check if you already have one:

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

If you see files like id_rsa and id_rsa.pub, you already have an SSH key. You can use the existing key or generate a new one.

Step 2: Generate a New SSH Key

If you don’t have an SSH key or want a new one, generate it using:

ssh-keygen -o -a 100 -t ed25519 -f ~/.ssh/id_ed25519_github -C "ssh key for github"
Enter fullscreen mode Exit fullscreen mode

If your system doesn’t support ed25519, use:

ssh-keygen -o -a 100 -t rsa -b 4096 -m PEM -f ~/.ssh/id_rsa_github -C "ssh key for github"
Enter fullscreen mode Exit fullscreen mode

After running the command, you will be prompted to enter a strong passphrase. Type your passphrase, press Enter, then confirm it when prompted to enhance security.

Step 3: Add Your SSH Key to the SSH Agent

Start the SSH agent:

eval "$(ssh-agent -s)"
Enter fullscreen mode Exit fullscreen mode

Then add your key:

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

If you used RSA, replace id_ed25519_github with id_rsa_github.

Step 4: Persist SSH Key Across Sessions

To ensure your SSH key is always loaded when you open a new terminal session, there are two main methods. The recommended method is to configure your SSH settings, while the alternative is to modify your .bashrc file.

Recommended: Configure SSH to Automatically Add the Key

Edit (or create) your SSH config file:

nano ~/.ssh/config
Enter fullscreen mode Exit fullscreen mode

Add the following lines:

Host github.com
  IdentityFile ~/.ssh/id_ed25519_github
  AddKeysToAgent yes
Enter fullscreen mode Exit fullscreen mode

This ensures that your SSH key is automatically loaded by the SSH agent when needed.

Alternative: Load the SSH Key in .bashrc

If you want to explicitly load the SSH key every time a new terminal session starts, add the following command to your ~/.bashrc (or ~/.bash_profile on macOS):

echo 'eval "$(ssh-agent -s)" && ssh-add ~/.ssh/id_ed25519_github' >> ~/.bashrc
Enter fullscreen mode Exit fullscreen mode

Then reload your shell configuration:

source ~/.bashrc
Enter fullscreen mode Exit fullscreen mode

This ensures the SSH agent starts automatically and the key is added every time you open a new terminal session.

Step 5: Add the SSH Key to Your GitHub Account

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

If you used RSA, replace id_ed25519_github.pub with id_rsa_github.pub.

Now, go to GitHub > Settings > SSH and GPG Keys and click New SSH Key. Paste the copied key and save it.

Step 6: Test the SSH Connection

Verify that your SSH key is correctly added by running:

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

If prompted with:

This key is not known by any other names.
Are you sure you want to continue connecting (yes/no/[fingerprint])?
Enter fullscreen mode Exit fullscreen mode

Type yes to proceed.

You should see a message like:

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

Step 7: Configure Git to Use SSH (Optional)

If you're still using HTTPS for Git operations, switch to SSH:

git remote set-url origin git@github.com:username/repository.git
Enter fullscreen mode Exit fullscreen mode

Conclusion

You've successfully configured SSH authentication for GitHub on Linux! This setup allows you to securely connect to your repositories without entering your password repeatedly. Happy coding!

If you encounter any issues, check GitHub’s SSH troubleshooting guide.

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay