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 AAAAorssh-rsa AAAAis the.pubhalf. Point-iorIdentityFileat 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
-----BEGINalso 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-keygenhas 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
Load key "...": bad permissionscomes after a warning about an unprotected file. The file is readable but others can read it too. See SSH "WARNING: UNPROTECTED PRIVATE KEY FILE!" and bad permissions.Load key "...": incorrect passphrase supplied to decrypt private keymeans the file is fine and the passphrase was wrong. To set a new passphrase once you have the old one, see Change or remove an SSH key passphrase with ssh-keygen -p.sign_and_send_pubkey: no mutual signature supportedmeans the key loaded, but the client and server could not agree on how to sign with it. See SSH "sign_and_send_pubkey: no mutual signature supported".
Related
- Key types and formats
- SSH error messages, what each one means
- SSH "WARNING: UNPROTECTED PRIVATE KEY FILE!" and bad permissions
- App guide: SSH key import errors: "Only OpenSSH-format keys are supported"
- Change or remove an SSH key passphrase with ssh-keygen -p