How to Set Up GitHub SSH Keys on Raspberry Pi: Complete Guide

Setting up SSH keys for GitHub on your Raspberry Pi allows you to push and pull code without entering your password every time. This is especially useful when running automated scripts or working with multiple repositories.

In this guide, I'll walk you through the complete process of generating SSH keys, adding them to GitHub, and testing the connection on Raspberry Pi OS.

Prerequisites

Before we start, make sure you have:

  • A Raspberry Pi running Raspberry Pi OS (or any Debian-based distribution)
  • Access to the terminal (local or via SSH)
  • A GitHub account
  • Git installed on your Raspberry Pi

Step 1: Check for Existing SSH Keys

First, let's check if you already have SSH keys on your Raspberry Pi.

Open a terminal and run:

ls -al ~/.ssh

If you see files like id_ed25519, id_rsa, or similar, you already have SSH keys. You can either:

  • Use your existing keys (skip to Step 4)
  • Create new keys (continue with Step 2)

If the directory doesn't exist or is empty, proceed to create new keys.

Step 2: Generate a New SSH Key

We'll create an Ed25519 key, which is more secure and efficient than RSA keys.

Run this command, replacing the email with your GitHub email:

ssh-keygen -t ed25519 -C "your.email@example.com"

When prompted for the file location, press Enter to use the default location (~/.ssh/id_ed25519).

Next, you'll be asked for a passphrase. This is optional but recommended for security:

  • With passphrase: Enter a strong passphrase (you'll need to enter it when using the key)
  • Without passphrase: Press Enter twice (less secure but more convenient)

The command will create two files:

  • ~/.ssh/id_ed25519 (private key - keep this secret)
  • ~/.ssh/id_ed25519.pub (public key - this goes to GitHub)

Step 3: Start SSH Agent and Add Your Key

The SSH agent manages your SSH keys in memory. Let's start it and add your new key:

eval "$(ssh-agent -s)"

Now add your private key to the agent:

ssh-add ~/.ssh/id_ed25519

If you set a passphrase, you'll be prompted to enter it now.

Step 4: Copy Your Public Key

Display your public key so you can copy it to GitHub:

cat ~/.ssh/id_ed25519.pub

The output will look something like this:

ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAI... your.email@example.com

Copy the entire line (from ssh-ed25519 to the end of your email).

Step 5: Add the Key to GitHub

Now we need to add your public key to your GitHub account:

  1. Open GitHub in your web browser
  2. Sign in to your account
  3. Click your profile picture in the top-right corner
  4. Select "Settings" from the dropdown menu
  5. Click "SSH and GPG keys" in the left sidebar
  6. Click "New SSH key"
  7. Fill in the form:
    • Title: Give it a descriptive name (e.g., "Raspberry Pi")
    • Key type: Select "Authentication Key"
    • Key: Paste the public key you copied in Step 4
  8. Click "Add SSH key"

Step 6: Test Your SSH Connection

Let's verify that everything is working correctly:

ssh -T git@github.com

On the first connection, you'll see a warning about the authenticity of the host. This is normal. You should see something like:

The authenticity of host 'github.com (140.82.112.4)' can't be established.
ED25519 key fingerprint is SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU.
Are you sure you want to continue connecting (yes/no/[fingerprint])?

Type yes and press Enter.

If successful, you'll see:

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

Step 7: Configure Git (if needed)

If you haven't configured Git on your Raspberry Pi yet, set up your identity:

git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"

Step 8: Update Repository URLs (if needed)

If you have existing repositories that use HTTPS, you can switch them to SSH:

cd /path/to/your/repository
git remote set-url origin git@github.com:username/repository.git

Replace username/repository with your actual GitHub username and repository name.

Alternative: Using RSA Keys (for older systems)

If your Raspberry Pi has an older version of OpenSSH that doesn't support Ed25519, you can use RSA keys instead:

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

The rest of the process remains the same, but use ~/.ssh/id_rsa instead of ~/.ssh/id_ed25519 in the commands.

Troubleshooting

Permission Denied Error

If you get a "Permission denied" error when testing the connection:

  1. Check file permissions:

    chmod 700 ~/.ssh
    chmod 600 ~/.ssh/id_ed25519
    chmod 644 ~/.ssh/id_ed25519.pub
    
  2. Restart the SSH agent:

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

Key Not Found Error

If the SSH agent can't find your key:

  1. Check if the key exists:

    ls -la ~/.ssh/
    
  2. Add the key manually:

    ssh-add ~/.ssh/id_ed25519
    

Multiple Keys

If you have multiple SSH keys, you can manage them by creating a config file:

nano ~/.ssh/config

Add this content:

Host github.com
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_ed25519

Security Best Practices

  1. Use a strong passphrase for your SSH key
  2. Keep your private key secure - never share it
  3. Use different keys for different services
  4. Regularly rotate your keys (every 6-12 months)
  5. Monitor your GitHub account for suspicious activity

Working with Multiple Devices

If you want to use the same SSH key on multiple devices:

  1. Copy the private key to the other device
  2. Set proper permissions on the new device
  3. Add the key to the SSH agent on the new device

Or create separate keys for each device and add them all to your GitHub account with descriptive titles.

Conclusion

You now have SSH keys set up for GitHub on your Raspberry Pi! This setup will make it much easier to work with Git repositories, especially for automated scripts or when you need to push code frequently.

The SSH connection is now secure and passwordless, allowing you to focus on your development work without constant authentication prompts.

Having trouble with any of these steps? Drop me a message on X and I'll help you troubleshoot!

A Wifi Place Logo

Find Work-Friendly Cafes Near You

Check my free directory of laptop-friendly cafes with Wifi.

Browse Cafes