DEV Community

John  Ajera
John Ajera

Posted on

How to Configure SSH for GitHub Authentication

How to Configure SSH for GitHub Authentication

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 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: 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 5: 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.
```



### Step 6: Configure Git to Use SSH (Optional)

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



```sh
git remote set-url origin git@github.com:username/repository.git
```



### Conclusion

You've successfully configured SSH authentication for GitHub! 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](https://docs.github.com/en/authentication/troubleshooting-ssh).
Enter fullscreen mode Exit fullscreen mode

Top comments (0)