🔐 A Complete Guide to SSH Keys: What They Are, How to Generate and Link to GitHub, and Why Use SSH over HTTP

Anish AgrawalAnish Agrawal
4 min read

🌐 Introduction

In today's tech world, security is a top priority, especially for developers working with code repositories and remote servers. SSH keys are powerful tools that provide secure, password-free authentication. This guide dives into everything you need to know about SSH keys: what they are, how to set them up with GitHub, and why they’re preferred over HTTP for secure operations. Let’s explore! 🚀


🛡️ What Are SSH Keys?

SSH, or Secure Shell, is a protocol for encrypting data exchanges between devices. SSH keys are cryptographic tools that enhance security by providing a pair-based authentication method:

  • 🔑 Public Key: This is shared with the server and acts as a lock.

  • 🔒 Private Key: This is kept secure on your device and acts as the key that only you hold.

🔐 Asymmetric encryption ensures that access is only granted if both keys match, making it highly secure. Unlike passwords, SSH keys provide two-factor security and eliminate the need for manual password input.


🚀 Why Use SSH over HTTP for Git Operations?

When using Git, you’ll encounter two primary ways to authenticate: SSH and HTTP. While HTTP is easier to set up initially, SSH is usually the better choice.

👍 Benefits of Using SSH

  • 🔐 Stronger Security: Password-free and encrypted.

  • ⚡ Convenience: No need to enter credentials each time.

  • 🤖 Automation-Friendly: Ideal for automated scripts without password exposure.

HTTP authentication, though familiar, can feel repetitive since it requires either regular re-authentication or a saved password token. SSH, however, is seamless after setup, making it the ideal choice for most developers.


🔨 How to Generate SSH Keys

Let’s get you set up with SSH keys. This guide covers Windows PowerShell and macOS/Linux steps for generating SSH keys.

Step 1: 🔍 Check for Existing SSH Keys

Before creating a new key, let’s check if any keys already exist on your system:

In PowerShell:

ls ~/.ssh

On macOS/Linux:

ls ~/.ssh

If you see files like id_rsa and id_rsa.pub, you already have a key pair!

Step 2: ⚙️ Generate a New SSH Key

In PowerShell:

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

In macOS/Linux Terminal:

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
  • 📂 Save Location: Press Enter to save in the default location (~/.ssh/id_rsa).

  • 🔏 Passphrase: Adding a passphrase is optional but recommended for extra security.

Step 3: 🔄 Start the SSH Agent and Add Your Key

The SSH agent runs in the background to manage SSH keys. Start it and add your key with the following commands:

In PowerShell:

Start-Service ssh-agent
ssh-add ~/.ssh/id_rsa

In macOS/Linux Terminal:

eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_rsa

🔗 Linking Your SSH Key to GitHub

Once your SSH key is ready, it’s time to link it to GitHub, allowing you to connect securely without a password.

Step 1: 📋 Copy Your Public SSH Key

Copy your public key to your clipboard. This is the key you’ll paste into GitHub.

In PowerShell:

Get-Content ~/.ssh/id_rsa.pub | Set-Clipboard

In macOS/Linux Terminal:

cat ~/.ssh/id_rsa.pub | pbcopy

Step 2: 🔗 Add the SSH Key to Your GitHub Account

  1. Go to GitHub and sign in.

  2. Navigate to Settings > SSH and GPG keys.

  3. Click New SSH Key ➕.

  4. Give it a name (like “My Laptop Key”), paste the public key, and hit Add SSH key.


✅ Testing Your SSH Connection

To confirm everything’s working, test your SSH connection with GitHub:

ssh -T git@github.com

If your connection is successful, you’ll see:

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

This message means your key is linked to GitHub, and you’re good to go! 🎉


💡 Tips and Best Practices for SSH Key Management

  1. 🔐 Use Strong Passphrases: This adds an extra layer of protection.

  2. ♻️ Rotate Your Keys Regularly: Update keys periodically for optimal security.

  3. 📂 Secure Backup: If your SSH key is critical, keep a secure backup.


🌟 Conclusion

SSH keys are a must-have for secure and efficient GitHub workflows. By using SSH over HTTP, you’re taking advantage of advanced security, convenience, and automation capabilities. Now that you’re set-up, you’ll be able to clone, push, and pull your GitHub repositories safely and hassle-free!

Ready to code securely?

0
Subscribe to my newsletter

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

Written by

Anish Agrawal
Anish Agrawal