Sub topic 7
How SSH key pairs prove identity
This chapter assumes you can clone, commit, push, and pull, and that you are comfortable in a terminal. You do not need any SSH experience. Here you will learn the model that SSH authentication rests on. Later chapters generate a key and register it with GitHub.
Two keys, one identity
An SSH key pair is two files created together by a single command:
- The private key stays on your machine. It never goes to GitHub, a teammate, or a chat window.
- The public key is safe to share. You paste it into your GitHub account settings.
The two keys are mathematically linked. A signature made with the private key can be checked with the public key. Nobody can work backward from the public key to the private key.
This differs from a personal access token. A token is a shared secret: GitHub holds a record of it, and you send it with each request. With SSH, GitHub holds only the public half. The secret never crosses the network.
The challenge and response
When you run git fetch or git push over SSH, the server does not ask for a password. It proves you hold the private key through a signature exchange.
The signed data is specific to this session, so a recorded signature cannot be replayed later. The private key is used only to sign locally. It is never transmitted.
What GitHub learns from the key
Every SSH connection to GitHub uses the username git. That is why remote URLs look like [email protected]:OWNER/REPO.git. GitHub does not identify you by the username. It identifies you by which registered public key matched.
This has two consequences:
- A public key can belong to only one account on GitHub. You cannot reuse the same key for a second account.
- Authentication tells GitHub who you are. What you can do with a repository is still decided by your permissions on it.
The passphrase protects the file
Anyone who copies your private key file can act as you. A passphrase is an optional password that encrypts the private key on disk. A stolen file is then useless without it. The cost is that you must type the passphrase whenever the key is used, and a later chapter shows how ssh-agent removes that repetition.
Common mistakes
- Pasting the private key into GitHub. GitHub accepts only the public key, which is the file ending in
.pub. - Sharing or committing the private key. Treat it like a password and never put it in a repository.
- Expecting a key to grant access on its own. A valid key for an account without write access to a repository still gets a permission error.
Exercise
You paste the contents of ~/.ssh/id_ed25519 into GitHub's SSH key settings, and GitHub rejects it. Which file should you have used, and why?
Show answer
Use ~/.ssh/id_ed25519.pub. The file without the extension is the private key, which must stay on your machine. GitHub only needs the public key to verify signatures made by the private key.
Exercise
Why does an SSH remote URL start with git@ even though your GitHub username is different?
Show answer
All SSH connections to GitHub use the fixed username git. GitHub identifies your account by matching the public key you offer, not by the SSH username.