Welcome to Birdies Nest
Back to snippets

Generate a GitHub SSH key

Create an Ed25519 SSH key and connect it to your GitHub account.

SSH keys let you push and pull over SSH without typing a password every time. Below you'll make one named github and connect it to your account — or grab the ready-made script at the end.

Stays in your browser — it just fills the commands below so you can copy them as-is.

Install SSH (if it's missing)

  1. 1

    First check whether SSH is already there — ssh -V prints the OpenSSH version. If you get one, jump to the next section; only keep going here if you see “command not found”.

    ssh -V
  2. 2

    Debian/Ubuntu: refresh the package list and install the OpenSSH client.

    sudo apt update && sudo apt install -y openssh-client
  3. 3

    Fedora/RHEL use dnf instead. (macOS and Windows 10+ already ship with SSH, so you can skip this.)

    sudo dnf install -y openssh-clients

Create & connect your key

  1. 1

    Start in your SSH folder. The ~ (tilde) is shorthand for your home directory, so ~/.ssh means /home/<your-username>/.ssh — where SSH keeps its keys. mkdir -p creates it first if it isn't there yet.

    mkdir -p ~/.ssh && cd ~/.ssh
  2. 2

    List the folder so you don't clobber a key you already use. If you see github and github.pub (or id_ed25519), a key with that name already exists.

    ls -al ~/.ssh
  3. 3

    Make an Ed25519 key. -C tags it with your GitHub email, and -f github names the files github (private) and github.pub (public) right here in ~/.ssh instead of the default id_ed25519. Prefer the prompts? Run ssh-keygen -t ed25519 and type github when it asks “Enter file in which to save the key”. Set a passphrase or press Enter to skip.

    ssh-keygen -t ed25519 -C "your_email@example.com" -f github
  4. 4

    Tell SSH to use this key for GitHub. Append a Host block to ~/.ssh/config so github.com is matched to your new key via IdentityFile — then ssh and git pick it up automatically, with no ssh-agent to start. The block below creates ~/.ssh/config if it doesn't exist yet, and IdentitiesOnly yes makes SSH offer only this key.

    cat >> ~/.ssh/config <<'EOF'
    
    Host github.com
      HostName github.com
      User git
      IdentityFile ~/.ssh/github
      IdentitiesOnly yes
    EOF
  5. 5

    Print the public key, then copy the whole single line that starts with ssh-ed25519 — no extra spaces or line breaks. (macOS: pbcopy < ~/.ssh/github.pub. Linux: xclip -sel clip < ~/.ssh/github.pub.)

    cat ~/.ssh/github.pub
  6. 6

    On GitHub, open your Account → Settings → SSH and GPG keys → New SSH key. Give it a title (e.g. your computer's name), paste the key into the Key box, and click Add SSH key.

  7. 7

    Test the connection — you should see “Hi <your-username>! You've successfully authenticated…”. The first time, type yes to trust GitHub's host.

    ssh -T git@github.com
  8. 8

    Prefer one shot? Save the block below as github-key-generation.sh, change the email, then run bash github-key-generation.sh. It does steps 1–5 and prints the public key for you to paste in step 6.

    #!/usr/bin/env bash
    # github-key-generation.sh — create an Ed25519 key named "github" and point GitHub at it.
    set -euo pipefail
    
    EMAIL="your_email@example.com"   # change to your GitHub email
    KEY="$HOME/.ssh/github"
    
    mkdir -p "$HOME/.ssh"
    ssh-keygen -t ed25519 -C "$EMAIL" -f "$KEY"
    
    # Point github.com at the new key (only if it is not already configured).
    if ! grep -q "^Host github.com$" "$HOME/.ssh/config" 2>/dev/null; then
      cat >> "$HOME/.ssh/config" <<EOF
    
    Host github.com
      HostName github.com
      User git
      IdentityFile $KEY
      IdentitiesOnly yes
    EOF
    fi
    
    echo
    echo "Public key — paste this into GitHub → Settings → SSH and GPG keys → New SSH key:"
    cat "$KEY.pub"