Back to Blog
Lesson 21 of the Git & GitHub: Git & GitHub from Zero course
GitAugust 8, 20264 min read

SSH Key Authentication: Secure Git Access for Beginners

Learn to generate SSH keys and link them to your GitHub account. Replace password-based Git authentication with secure, professional-grade key pairs.

sshauthenticationsecuritygithubgitdevops
A detailed close-up of a bunch of metallic keys resting on a dark textured surface.

Previously in this course, we covered the basics of Introduction to GitHub, where you set up your account. Now that you have a profile, we need to bridge the gap between your local terminal and the cloud. Instead of typing your password every time you push code, we'll use SSH key authentication to establish a secure, persistent connection.

Why Use SSH for Authentication?

When you interact with a remote repository, GitHub needs to verify your identity. While HTTPS is common, it often requires credential managers or constant password prompts.

SSH (Secure Shell) provides a more robust security posture. It uses asymmetric cryptography: you hold a "private" key that never leaves your machine, and you provide a "public" key to GitHub. The two work together to prove you are who you say you are without ever transmitting a password over the wire.

Generating Your SSH Key Pair

First, open your terminal and check if you already have keys. Navigate to your hidden .ssh directory:

Bash
ls -al ~/.ssh

If you see files named id_rsa.pub or id_ed25519.pub, you already have a key. If not, let’s generate a modern, secure Ed25519 key. Run the following command, replacing the email with the one you configured in Configuring User Identity:

Bash
ssh-keygen -t ed25519 -C "your_email@example.com"
  1. Save location: Press Enter to accept the default file location.
  2. Passphrase: You'll be asked for a passphrase. This adds a layer of security—even if someone steals your physical laptop, they can't use your keys without this phrase. Choose a strong one, or press Enter for none (not recommended for production machines).

Adding the Public Key to GitHub

Now that the keys exist, you need to share the public half with GitHub. First, copy the contents of your public key to your clipboard:

On macOS:

Bash
pbcopy < ~/.ssh/id_ed25519.pub

On Windows (Git Bash):

Bash
cat ~/.ssh/id_ed25519.pub | clip

On Linux:

Bash
cat ~/.ssh/id_ed25519.pub
# Copy the output manually

Next, log in to GitHub:

  1. Go to your Settings (click your profile photo in the top right).
  2. On the left sidebar, click SSH and GPG keys.
  3. Click New SSH key.
  4. Give it a title (e.g., "Work Laptop") and paste your key into the Key field.
  5. Click Add SSH key.

Testing the Connection

Verify that your machine can talk to GitHub correctly by running:

Bash
ssh -T git@github.com

You should see a message like: "Hi [username]! You've successfully authenticated, but GitHub does not provide shell access." This confirms your setup is complete and your identity is verified.

Common Pitfalls

  • Copying the wrong key: Always ensure you copy the file ending in .pub. The file without an extension is your private key; never share or upload that.
  • Permission denied: If you get a "Permission denied" error during the test, ensure your local ssh-agent is running and the key is added using ssh-add ~/.ssh/id_ed25519.
  • File permissions: SSH is strict. Your .ssh folder should have permissions set to 700 and your key files to 600. If you encounter errors, check them with ls -l ~/.ssh.

Practice Exercise

  1. Verify your current SSH key status.
  2. Generate a new key pair if you don't have one.
  3. Successfully add the public key to your GitHub account.
  4. Run the ssh -T test command and verify the successful authentication message.

FAQ

Is it safe to leave the passphrase blank? It's convenient, but less secure. If your machine is ever compromised, the attacker has immediate access to any service using that key. Use a passphrase if you value high-security standards.

Do I need a new key for every repository? No. One SSH key pair is sufficient for all your GitHub repositories. You only need to add the public key to your GitHub account once.

How is this different from HTTPS? HTTPS is a protocol for web traffic; SSH is a protocol for secure command-line interaction. While both can be used for Git, SSH is the industry standard for developer workflows and automation.

Recap

We've successfully moved from password-based authentication to secure SSH key pairs. You now have a cryptographically secure way to push your code to the cloud. This is a foundational step for the collaborative workflows we'll build in the next few lessons.

Up next: Linking Local Repositories

Similar Posts