Strovent SystemsSign in
Reference

SSH Load key "invalid format" and "error in libcrypto"

Load key "file": invalid format means ssh could not read the private key file: a .pub or .ppk file, lost line breaks, Windows line endings or no final newline.

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

The first line comes from the SSH client on your side. It opened the private key file and could not read it as a key. The server never saw the key, so the "Permission denied" that follows is only the result: the client skipped the key, tried any other login methods, and ran out of them.

On many Linux systems the same damaged file gives Load key "...": error in libcrypto instead. Treat the two as one problem.

Test the file without connecting

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

-y reads a private key and prints its public key. A good file prints a line starting with ssh-ed25519, ecdsa-sha2- or ssh-rsa, after asking for the passphrase if it has one. A damaged file fails with the same Load key message, so you can fix and retest it without touching the server.

Causes

  • It is the public key. A file that starts with ssh-ed25519 AAAA or ssh-rsa AAAA is the .pub half. Point -i or IdentityFile at the file without .pub.
  • It is a PuTTY key. A file that starts with PuTTY-User-Key-File- is in PuTTY's own format, which OpenSSH does not read. Convert it, as in Convert a PuTTY .ppk key to OpenSSH format, and PPK version 3.
  • Line breaks were lost. The whole key sits on one line, or the lines are joined with spaces. This is common after pasting a key into a chat, a web form or an unquoted shell variable. Copy it again from the original file.
  • Windows line endings. OpenSSH expects the -----BEGIN OPENSSH PRIVATE KEY----- line to end with a plain line feed. A carriage return there (CRLF, from a Windows editor or a copy through Windows) makes the file unreadable.
  • No newline after the last line. OpenSSH looks for the -----END OPENSSH PRIVATE KEY----- line including its line break. A file that stops right after the final dashes fails.
  • Something before the first line. A blank line, spaces or indentation in front of -----BEGIN also breaks the newer format. Keys pasted into configuration files often pick up indentation.
  • The copy is incomplete. The first line is not -----BEGIN ... or the last line is not -----END ....
  • A very old client. The OpenSSH key format, the one ssh-keygen has written by default since OpenSSH 7.8, arrived in OpenSSH 6.5. A client older than that cannot read it.

Repair a copied file

To check and fix the line endings and the final newline on Linux or macOS:

grep -c $'\r' key
tr -d '\r' < key > key.fixed
printf '\n' >> key.fixed
chmod 600 key.fixed
ssh-keygen -y -f key.fixed

The first command counts lines that end in a carriage return. If ssh-keygen -y now prints the public key, use key.fixed. An extra blank line after the END line does no harm. If the test still fails, the content itself is damaged, and the only fix is a fresh copy from where the key was made.

Do not paste the key into a shared log or screen while you inspect it. It is a secret.

Messages that look similar

Sources

Spotted a mistake? Email [email protected].