DEV Community

Sospeter Mong'are
Sospeter Mong'are

Posted on

Using SSH Authentication with GitHub

Introduction

SSH authentication is a secure and convenient way to interact with GitHub repositories without needing to enter your credentials every time you push or pull changes. This guide will walk you through setting up SSH authentication with GitHub, generating an SSH key, adding it to GitHub, and configuring your Git client to use SSH.

1. Check for an Existing SSH Key

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

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

If you see files like id_rsa and id_rsa.pub (or id_ed25519 and id_ed25519.pub), you already have an SSH key.

2. Generate a New SSH Key (If Needed)

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

ssh-keygen -t ed25519 -C "[email protected]"
Enter fullscreen mode Exit fullscreen mode
  • Replace [email protected] with your GitHub email.
  • Press Enter to accept the default file location (~/.ssh/id_ed25519).
  • Set a passphrase for additional security (or leave it blank).

3. Add the SSH Key to the SSH Agent

To ensure the SSH key is used, add it to the SSH agent:

eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519  # Use id_rsa if you generated an RSA key
Enter fullscreen mode Exit fullscreen mode

4. Add the SSH Key to GitHub

  1. Copy your SSH public key:
   cat ~/.ssh/id_ed25519.pub  # Or `cat ~/.ssh/id_rsa.pub`
Enter fullscreen mode Exit fullscreen mode
  1. Go to GitHub β†’ Settings β†’ SSH and GPG Keys

    πŸ‘‰ GitHub SSH Key Settings

  2. Click "New SSH Key", enter a title (e.g., "Work Laptop"), paste your key, and click "Add SSH Key".

5. Test Your SSH Connection

To verify that SSH authentication is working, run:

ssh -T [email protected]
Enter fullscreen mode Exit fullscreen mode

If successful, you should see a message like:

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

6. Configure Git to Use SSH

If your repository was cloned using HTTPS, update the remote URL to use SSH:

git remote set-url origin [email protected]:your-username/repository-name.git
Enter fullscreen mode Exit fullscreen mode

Check that the change was applied:

git remote -v
Enter fullscreen mode Exit fullscreen mode

The output should now show:

origin  [email protected]:your-username/repository-name.git (fetch)
origin  [email protected]:your-username/repository-name.git (push)
Enter fullscreen mode Exit fullscreen mode

7. Push Changes Using SSH

Now that SSH is set up, you can push and pull changes without entering credentials:

git push origin main
Enter fullscreen mode Exit fullscreen mode

Conclusion

SSH authentication with GitHub enhances security and convenience by allowing secure, password-free interactions with your repositories. Once set up, SSH eliminates the need for Personal Access Tokens (PATs) required for HTTPS authentication.

By following these steps, you can configure SSH authentication, add your key to GitHub, and securely manage your repositories.


Troubleshooting

  • If you encounter Permission denied (publickey), ensure:
    • Your SSH key is added to GitHub.
    • The SSH agent is running (ssh-add -l should list your key).
    • Your remote URL is correctly set to use SSH (git remote -v).

Let me know when you get stuck in between the tutorial

Top comments (0)