Load key invalid format: SSH Private Key Fix (2026)

Load key invalid format or error in libcrypto? The private key file is damaged. Find which of 8 causes it is with tested checks, and fix it without a new key.

14 min read

You point ssh, git or ssh-add at a private key and get this:

bash

Load key "/home/you/.ssh/id_ed25519": invalid format
[email protected]: Permission denied (publickey).

or, on a newer Linux box, the same failure with a stranger wording:

bash

Load key "/home/you/.ssh/id_ed25519": error in libcrypto
[email protected]: Permission denied (publickey).

Both say one thing: the client opened the key file and could not parse it. The server never saw the key. The Permission denied line is a consequence, not the cause: ssh skipped the broken key, ran out of other keys to try, and gave up.

The good news is that the key itself is usually fine. In most cases the file was damaged on the way: a lost newline, Windows line endings, a text encoding change, or a CI secret that flattened it. All of those are fixable in one command, without generating a new key.

Quick Summary: What You’ll Learn

  • What “Load key: invalid format” means and why it ends in “Permission denied (publickey)”
  • Why the same broken file says “invalid format” on one machine and “error in libcrypto” on another
  • Four commands that tell you what is wrong with the file in under a minute
  • The eight causes we reproduced on OpenSSH 9.6, each with its fix
  • How to store an SSH key in a CI secret so it survives
  • How to tell this error apart from “bad permissions” and passphrase errors

What does “Load key: invalid format” mean?

It means the OpenSSH client could not read the private key file in any format it knows. OpenSSH understands its own format (-----BEGIN OPENSSH PRIVATE KEY-----), which ssh-keygen writes by default, plus the older PEM and PKCS#8 formats (-----BEGIN RSA PRIVATE KEY-----, -----BEGIN PRIVATE KEY-----). If the file is none of those, or one of those with damage inside, loading fails.

This happens on your machine, before any authentication. You can see the order in ssh -v:

yaml

debug1: Will attempt key: /home/you/.ssh/id_ed25519  explicit
debug1: Trying private key: /home/you/.ssh/id_ed25519
Load key "/home/you/.ssh/id_ed25519": error in libcrypto
debug1: No more authentication methods to try.
[email protected]: Permission denied (publickey).

That is why changing authorized_keys on the server, restarting sshd, or adding the key on GitHub again does nothing. The file on the client is the problem.

ssh-add reports the same failure as Error loading key "...": invalid format (or error in libcrypto), and ssh-keygen -y -f prints the same Load key line.

Why does it say “error in libcrypto” instead of “invalid format”?

The wording depends on which crypto library your OpenSSH build uses and which format the damaged key was in. OpenSSH first tries its own parser. When that fails, it hands the file to the crypto library’s PEM parser, and the error you see is whatever came out last.

We broke the same keys in many ways and loaded them with OpenSSH 9.6 on Ubuntu 24.04, which is built against OpenSSL 3:

  • Damaged OpenSSH-format key (the default since OpenSSH 7.8): every variant printed error in libcrypto.
  • Damaged PEM key (BEGIN RSA PRIVATE KEY, a truncated or edited body): invalid format.
  • Wrong PEM header (body of one format, header of another): error in libcrypto.

On builds that use LibreSSL (macOS) or OpenSSL 1.1, the same damage is reported as invalid format. So if a teammate on a Mac and you on Ubuntu see different messages for the same key, it is the same problem. Treat the two strings as one error and use the checks below.

One more finding from the lab: the PEM parser is far more forgiving than OpenSSH’s own. A PEM RSA key with Windows line endings, no final newline, or its body joined into one line still loaded. The same damage to an OpenSSH-format key broke it. That is why “it worked with my old RSA key” is a common story when an Ed25519 key fails after the same copy-paste.

How do you find what is wrong with the key file?

Four read-only commands cover almost every case. None of them print the secret part of the key.

bash

KEY=~/.ssh/id_ed25519

head -1 "$KEY"              # which format, if any
file "$KEY"                 # encoding: ASCII, UTF-8 with BOM, UTF-16...
grep -c $'\r' "$KEY"        # number of lines with Windows line endings (want 0)
tail -c 1 "$KEY" | od -c    # last byte, want \n

Then the real test:

bash

ssh-keygen -y -f "$KEY"

-y reads a private key and prints its public key. If it prints an ssh-ed25519 AAAA... line, the file is valid and your problem is elsewhere (wrong key for that server, or a different error). If it prints Load key ..., keep going: the output of the four checks above points at the cause.

For a passphrase-protected key, ssh-keygen -y asks for the passphrase. A wrong passphrase gives incorrect passphrase supplied to decrypt private key, which is a different problem, covered in why your SSH key passphrase won’t authenticate.

Is the file missing its final newline?

This is the classic one, and the lab confirmed it: an OpenSSH-format key with the last \n removed fails with error in libcrypto on 9.6. Nothing else in the file changed.

Check with tail -c 1 "$KEY" | od -c. A healthy key ends in \n. If you see - (the last dash of the footer), fix it:

bash

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

This mostly happens when a key is pasted into a web form, a password manager note, or a CI secret, then written back to disk with a tool that does not add a trailing newline.

Did Windows line endings (CRLF) get into the key?

If a key file was edited or created on Windows and then used on Linux, macOS, WSL or inside a container, each line may end in \r\n. grep -c $'\r' "$KEY" returns a non-zero count, and file may still call it an “OpenSSH private key”, which is misleading. On 9.6 the CRLF version of a working key failed every time.

Strip the carriage returns into a new file, check it, then swap it in:

bash

tr -d '\r' < ~/.ssh/id_ed25519 > ~/.ssh/id_ed25519.fixed
chmod 600 ~/.ssh/id_ed25519.fixed
ssh-keygen -y -f ~/.ssh/id_ed25519.fixed && mv ~/.ssh/id_ed25519.fixed ~/.ssh/id_ed25519

dos2unix does the same job if it is installed. For Git, check that the key is not inside a repository with core.autocrlf rewriting it. Private keys should not be in a repository anyway.

Did PowerShell or an editor change the encoding?

file "$KEY" says UTF-8 (with BOM) or UTF-16, little-endian? Then the bytes at the start of the file are not -----BEGIN, and both failed in the lab.

The usual source on Windows is Windows PowerShell 5.1: per Microsoft’s documentation, Out-File and the redirection operators > and >> write UTF-16LE with a byte order mark. So this innocent-looking line produces a key file OpenSSH cannot read:

PowerShell

# Windows PowerShell 5.1: writes UTF-16LE, breaks the key
Get-Content id_ed25519 > id_ed25519.copy

Copy key files as files (Copy-Item, scp, a file manager) instead of piping their text. To repair a file that is already UTF-16 or has a BOM, from WSL, Git Bash, Linux or macOS:

bash

# UTF-16 to UTF-8, drop CRs and any BOM
iconv -f UTF-16 -t UTF-8 key | tr -d '\r' | sed '1s/^\xEF\xBB\xBF//' > key.fixed

# UTF-8 with BOM only
sed '1s/^\xEF\xBB\xBF//' key > key.fixed

Then chmod 600 key.fixed and test it with ssh-keygen -y -f key.fixed.

Was the key flattened, indented, or full of literal “\n”?

Keys that pass through YAML, JSON, environment variables or chat apps get reshaped. We tested four shapes of the same OpenSSH-format key on 9.6:

  • Body lines joined into one line, header and footer on their own lines: loaded fine. OpenSSH does not care how the base64 body is wrapped.
  • Everything on one line, newlines turned into spaces: failed.
  • Literal backslash-n characters instead of real newlines (what you get from a JSON string written to disk raw): failed.
  • Every line indented by two spaces (a key pasted under a YAML key without a block scalar): failed.

So the rule is: -----BEGIN ...----- must be the first line and start at column one, -----END ...----- must be on its own line, and the file must end with a newline. For an indented copy, sed 's/^[[:space:]]*//' key > key.fixed fixed it in the lab. For literal \n, convert them back with printf '%b\n' "$(cat key)" > key.fixed, or better, go back to the original file.

Are you pointing ssh at the .pub file?

ssh -i ~/.ssh/id_ed25519.pub fails with the same Load key line, because -i wants the private half. file calls it an “OpenSSH ED25519 public key” and head -1 shows ssh-ed25519 AAAA... instead of a BEGIN line.

The same happens with IdentityFile in ~/.ssh/config, in Git’s core.sshCommand, or in a deploy tool’s “SSH key” field. Point it at the file without .pub. If you only have the .pub file, you do not have the key; generate a new pair and install the new public key.

If you juggle several keys, a per-host IdentityFile with IdentitiesOnly yes keeps ssh from guessing. See managing multiple SSH keys across devices.

Is it a PuTTY .ppk file?

head -1 shows PuTTY-User-Key-File-2: or PuTTY-User-Key-File-3:. OpenSSH does not read PuTTY’s format at all, so this fails on every platform. The key is fine; convert it once with PuTTYgen. The full walkthrough for Windows, macOS and Linux, including PPK v3 and passphrases, is in convert PPK to OpenSSH.

Is the key truncated or edited?

A key copied out of a terminal window that cut a line, a cloud sync that stopped halfway, or a stray character typed into the body. In the lab, deleting the last body line gave error in libcrypto for an OpenSSH-format key and invalid format for a PEM key. Changing a single character in a PEM body gave invalid format.

There is no repair for missing bytes. Get the original file again from wherever it came from, or generate a new key. If you rebuild it, our ssh-keygen best practices cover algorithm, rounds and passphrase choices.

How do you put an SSH key in a CI secret without breaking it?

CI is where most of these causes meet. Secrets are stored as strings, shells strip trailing newlines in command substitution, and some UIs convert line endings. In the lab, with the key text in a variable (KEY="$(cat id_ed25519)", which drops the final newline the way many secret stores do):

  • printf '%s' "$KEY" > key failed (no final newline)
  • printf '%s\n' "$KEY" > key worked
  • echo "$KEY" > key worked

A defensive version of the step that writes the key:

bash

install -m 600 /dev/null ~/.ssh/id_ci
printf '%s\n' "$SSH_PRIVATE_KEY" | tr -d '\r' > ~/.ssh/id_ci
ssh-keygen -y -f ~/.ssh/id_ci > /dev/null   # fails the job early with a clear error

The ssh-keygen -y line turns a confusing Permission denied three steps later into a failed check right where the key is written. It prints only the public key, which it sends to /dev/null here.

If your CI system mangles multi-line secrets no matter what, store the key base64-encoded and decode it in the job. That round trip loaded fine in the lab:

bash

# once, on your machine (GNU base64; on macOS use: base64 -i key)
base64 -w0 ~/.ssh/id_ci

# in the job
printf '%s' "$SSH_PRIVATE_KEY_B64" | base64 -d > ~/.ssh/id_ci
chmod 600 ~/.ssh/id_ci

Use a dedicated key for CI, scoped to what the job needs, so a leaked secret is cheap to revoke.

How do you convert an older key to the OpenSSH format?

If the key loads but is in PEM or PKCS#8 format and you want the OpenSSH format (some tools only show public keys or comments for it), ssh-keygen -p rewrites the file. The man page describes -p as changing the passphrase; since OpenSSH 7.8 the rewritten file is saved in the OpenSSH format by default. Back up first:

bash

cp ~/.ssh/id_rsa ~/.ssh/id_rsa.bak
ssh-keygen -p -f ~/.ssh/id_rsa
head -1 ~/.ssh/id_rsa   # -----BEGIN OPENSSH PRIVATE KEY-----

It asks for the old passphrase and a new one; entering the same passphrase keeps it. The key material does not change, so nothing needs updating on servers. Confirm with ssh-keygen -l -f, which shows the same fingerprint before and after.

Which similar errors are not “invalid format”?

Three messages look related but have different fixes:

  • Load key "...": bad permissions, after a WARNING: UNPROTECTED PRIVATE KEY FILE! banner. The file parses; it is readable by other users and ssh refuses it. Fix with chmod 600 (or icacls on Windows).
  • incorrect passphrase supplied to decrypt private key. The file parses; the passphrase is wrong or the client cannot prompt for it. See the passphrase post.
  • Warning: Identity file ... not accessible: No such file or directory. Wrong path in -i or IdentityFile. Nothing was loaded at all.

And if you see Could not open a connection to your authentication agent from ssh-add, that is the agent, not the key. Our ssh-agent guide covers starting it on each platform.

A quick checklist

  • ssh-keygen -y -f <key>: prints a public key? The file is fine, look elsewhere.
  • head -1: BEGIN OPENSSH, BEGIN RSA or BEGIN PRIVATE KEY? PuTTY-User-Key-File? ssh-ed25519 AAAA?
  • file: plain ASCII, or BOM / UTF-16?
  • grep -c $'\r': zero?
  • tail -c 1 | od -c: ends in \n?
  • Header at column one, footer on its own line, no literal \n?
  • Still failing with no visible damage: the body is truncated or edited. Get the original or make a new key.

Where does termique fit?

termique will not fix a key file that ssh rejects on your laptop. It changes where the key lives, which removes most of the copy-paste damage above.

When you import a key in termique (paste it or load the file), the import sheet checks the key, and its passphrase if it has one, as you type, and shows Valid … private key once the key parses. Per the docs, a key whose line breaks were lost in copying is reformatted automatically. Keys are encrypted on your device, and if you use sync they travel to your other devices, iOS included, as end-to-end encrypted ciphertext, so you are not moving key files through chat or email to get them onto a second machine.

termique shows public keys only for keys in the OpenSSH format. For an older PEM key, the docs suggest the same ssh-keygen -p conversion shown above before importing.

termique is a free, native ssh manager we build, with end-to-end encrypted credentials. It runs on macOS, Windows, Linux and iOS. Download termique.

FAQ

Why does “Load key: invalid format” end with “Permission denied (publickey)”?

ssh could not read the key, so it skipped it and tried other keys or methods. When none worked, the server refused the login. Fix the key file and the permission error usually goes away with it.

Is “error in libcrypto” a problem with OpenSSL on my system?

Almost never. On OpenSSH builds that use OpenSSL 3, a key file that OpenSSH’s own parser rejects is passed to OpenSSL, and its failure is what you see. The file is damaged or in a format OpenSSH does not read. Run the checks above.

Why does git say “Load key …: invalid format”?

Git runs ssh underneath, so the message is ssh’s. Check the key named in ~/.ssh/config, GIT_SSH_COMMAND or core.sshCommand, then run ssh -vT [email protected] (or your Git host) to see which file it tries to load.

Can I fix “invalid format” without generating a new key?

Yes, unless bytes are missing. Line endings, encoding, a missing final newline, indentation and PuTTY’s format are all repairable, and the public key on your servers stays valid. Only a truncated or edited body needs the original file or a new key.

Does the key type matter, Ed25519 vs RSA?

For this error, mostly the file format matters. In our tests, PEM-format RSA keys tolerated CRLF line endings and a missing final newline, while OpenSSH-format keys (which all Ed25519 keys are) failed on both. That is why an old RSA key can survive a copy-paste that breaks a new Ed25519 key.