Sub topic 10
Adding the public key to your GitHub account
This chapter assumes you have read the chapters on SSH key pairs and on generating a key. You should have an Ed25519 key pair in ~/.ssh, a GitHub account, and a terminal.
What registering a key does
GitHub cannot trust a key just because it exists on your machine. Registering the public key tells GitHub that anyone who can prove they hold the matching private key is you. The private key never leaves your computer, and only the .pub file is shared.
Copy the public key
Print the public key and copy the whole line. It begins with ssh-ed25519 and ends with the comment you set.
cat ~/.ssh/id_ed25519.pubMake sure the file ends in .pub. Pasting the private key into GitHub is a serious mistake. If you do it, delete that key pair and generate a new one.
Add the key in the web interface
- Sign in to GitHub and open Settings from your profile menu.
- Select SSH and GPG keys in the sidebar.
- Click New SSH key.
- Enter a Title that names the machine, such as
work-laptop. A clear title helps you find and remove the right key later. - Leave Key type as Authentication Key.
- Paste the public key into the Key field and click Add SSH key.
GitHub may ask you to confirm your password or a second factor first.
Add the key with GitHub CLI
If you use GitHub CLI, you can upload the key from the terminal. The gh auth login command can also do this during sign-in.
gh ssh-key add ~/.ssh/id_ed25519.pub --title "work-laptop"Authentication keys and signing keys
GitHub accepts the same public key for two purposes. An authentication key lets you push and pull. A signing key lets GitHub mark your commits as verified. If you want both, add the key twice, once for each type. Adding it as only one type does not enable the other.
Common errors
- Key is already in use. GitHub allows a given public key on only one account. Remove it from the other account, or generate a new key pair.
- Key is invalid. The pasted text is usually truncated or has a line break in it. Copy it again as a single line.
- Organization access. If the organization uses SAML single sign-on, you must authorize the key for that organization. A later chapter covers this.
Next step
Registering the key is half the setup. The key must also be available to your SSH client, either on disk or loaded in ssh-agent. The next chapter confirms the whole chain with ssh -T [email protected].
Exercise
You paste the contents of ~/.ssh/id_ed25519.pub into GitHub as an authentication key. Later you want your commits to show as verified. What must you do, and why?
Show answer
Add the same public key again, this time with the key type set to Signing Key. GitHub treats authentication and signing as separate roles, so a key registered for one does not work for the other.
Exercise
GitHub rejects your new key with the message that the key is already in use. What is the likely cause, and what are two ways to fix it?
Show answer
The public key is already registered on another GitHub account, and a key can belong to only one account. Either remove it from the other account, or generate a new key pair and register the new public key.