Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Security and hardening

GitHub refuses the SSH key you pasted

ErrorKey is invalid. You must supply a key in OpenSSH public key format

Short answer

GitHub only accepts the public half of an SSH key, pasted as one clean line. The error usually means you pasted the private key, copied the wrong file, broke the line while copying, or exported the key in a different format. Rebuild the public key from your private key with ssh-keygen -y and paste that.

  • Applies to OpenSSH 9.x and 10.x, GitHub.com
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Print the file you are about to paste. A valid public key is a single line that starts with a key type such as ssh-ed25519 or ssh-rsa, and the file name ends in .pub.

    cat ~/.ssh/id_ed25519.pub
    ssh-keygen -l -f ~/.ssh/id_ed25519.pub
  2. 2Match what you see, then jump to its fix

    1. 1The text you pasted spans many lines or starts with a BEGIN ... PRIVATE KEY header, or the file has no .pub ending.You pasted the private key, or the wrong fileFix
    2. 2The key looks right in the terminal but GitHub rejects it after you paste it from a wrapped window or a chat message.The copy added newlines or spacesFix
    3. 3The file starts with a line of dashes and BEGIN SSH2 PUBLIC KEY instead of a key type.The key is in another format, such as RFC4716Fix
    4. 4The line starts with ssh-dss.The key type is DSAFix

What does the error look like?

  • Key is invalid. You must supply a key in OpenSSH public key format

Cause 1 of 4

You pasted the private key, or the wrong file

1Check

List the key files. The one without .pub is the private key and must never leave your machine. GitHub wants the one ending in .pub.

ls -l ~/.ssh
head -n 1 ~/.ssh/id_ed25519

2Fix

If you lost the .pub file, rebuild it from the private key. Paste only that output.

ssh-keygen -y -f ~/.ssh/id_ed25519 > ~/.ssh/id_ed25519.pub

3Verify

The file now holds one line. Copy it and add it under Settings, SSH and GPG keys.

cat ~/.ssh/id_ed25519.pub

Confirmed by docs.github.com

Did this fix it?

Cause 2 of 4

The copy added newlines or spaces

1Check

GitHub warns against adding newlines or whitespace when copying. Terminal selection, email clients and chat tools often wrap or pad long lines.

wc -l ~/.ssh/id_ed25519.pub

2Fix

Send the file to the clipboard with a command instead of selecting text by hand.

# macOS
pbcopy < ~/.ssh/id_ed25519.pub
# Windows PowerShell
cat ~/.ssh/id_ed25519.pub | clip

3Verify

Paste into the Key field and save. The key appears in the list with a fingerprint.

ssh-keygen -l -f ~/.ssh/id_ed25519.pub

Confirmed by docs.github.com

Did this fix it?

Cause 3 of 4

The key is in another format, such as RFC4716

1Check

ssh-keygen can import and export public keys in RFC4716, PKCS8 and PEM. RFC4716 is the default for both directions, and it is not what GitHub wants.

head -n 2 exported_key.pub

2Fix

Convert it to the OpenSSH line with ssh-keygen -i.

ssh-keygen -i -f exported_key.pub > id_converted.pub

3Verify

The output is one line starting with a key type, and ssh-keygen can read it.

ssh-keygen -l -f id_converted.pub

Confirmed by man.openbsd.org

Did this fix it?

Cause 4 of 4

The key type is DSA

1Check

GitHub no longer supports DSA keys, and new ones cannot be added.

cut -d ' ' -f 1 ~/.ssh/id_dsa.pub

2Fix

Generate a new key. Ed25519 is the type GitHub's own guide uses; RSA with 4096 bits is the option for old systems.

ssh-keygen -t ed25519 -C "your_email@example.com"
# old systems without Ed25519:
# ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

3Verify

Paste the new .pub file contents into GitHub.

cat ~/.ssh/id_ed25519.pub

Confirmed by docs.github.com

Did this fix it?

How do I stop it coming back?

  • Always paste from the file that ends in .pub, and keep the file without it private.
  • Use pbcopy, clip or the GitHub CLI command gh ssh-key add instead of selecting text by hand.
  • Generate Ed25519 keys on new machines.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.