How to Fix ‘ssh-keygen: error loading key: invalid format’ in Linux

Managing remote Linux servers, deploying code to GitHub, or authenticating automated CI/CD pipelines relies heavily on OpenSSH cryptographic keys. However, when executing ssh, ssh-add, or ssh-keygen -y -f /path/to/key, you may encounter the terminal error: “Load key ‘/home/user/.ssh/id_rsa’: error loading key: invalid format”. This failure halts authentication immediately. The error occurs because the OpenSSH parser cannot interpret the structure of your private key file due to carriage return corruptions, missing newline terminators, passphrase encoding issues, or format incompatibilities between legacy PEM and modern OpenSSH keys.

Why Does OpenSSH Report ‘error loading key: invalid format’?

OpenSSH requires strict cryptographic header definitions, Base64 formatting, and POSIX line endings. Common causes for this error include:

  • Windows CRLF Line Ending Pollution: The private key file was edited in a Windows text editor (such as Notepad) or transferred via Git without core.autocrlf disabled, converting Unix line breaks (\n) to DOS line breaks (\r\n).
  • Missing Trailing Newline: The key file terminates abruptly at the end of the -----END OPENSSH PRIVATE KEY----- header without a concluding Unix newline character.
  • Truncated or Corrupted Header/Footer: Leading or trailing hyphens were accidentally deleted during copy-paste operations, causing ASN.1/DER decoding failures.
  • Legacy PEM / PKCS#8 Incompatibility: The key was generated via PuttyGen or older OpenSSL versions without converting to standard OpenSSH key formats.
  • Excessively Permissive File Permissions: Although usually accompanied by a permissions warning, permission issues on .ssh directories can cause the OpenSSH parser to abort early.

Step 1: Convert Windows CRLF Line Endings to Unix LF

DOS line ending conversion is the single most common cause of key format errors when keys are copied between Windows and Linux machines.

  1. Install the dos2unix utility if not already present on your system:
    sudo apt install dos2unix      # Ubuntu/Debian
    sudo dnf install dos2unix      # Fedora/RHEL
    sudo pacman -S dos2unix        # Arch Linux
  2. Run dos2unix directly on your private key:
    dos2unix ~/.ssh/id_rsa
  3. Alternatively, if you cannot install utilities, use sed to strip carriage returns:
    sed -i 's/\r$//' ~/.ssh/id_rsa
  4. Attempt your SSH connection or ssh-add command again.

Step 2: Inspect Headers and Ensure a Trailing Newline

Verify that your key headers match standard OpenSSH specifications and contain a clean trailing newline.

  1. Inspect the beginning and ending lines of your private key using head and tail:
    head -n 2 ~/.ssh/id_rsa
    tail -n 2 ~/.ssh/id_rsa
  2. Verify that the first line matches one of these valid formats exactly:
    • -----BEGIN OPENSSH PRIVATE KEY----- (modern OpenSSH format)
    • -----BEGIN RSA PRIVATE KEY----- (legacy PEM format)
  3. Verify that the final line matches the closing tag:
    • -----END OPENSSH PRIVATE KEY-----
    • -----END RSA PRIVATE KEY-----
  4. Append a trailing newline to the file to satisfy POSIX file requirements:
    echo "" >> ~/.ssh/id_rsa

Step 3: Convert Legacy PEM or PKCS#8 Keys to Modern OpenSSH Format

If your private key was generated with legacy tools, converting it to standard OpenSSH format resolves internal parsing mismatches.

  1. Create a backup copy of your key file:
    cp ~/.ssh/id_rsa ~/.ssh/id_rsa.bak
  2. Use ssh-keygen to re-encode the key in modern format:
    ssh-keygen -p -N "" -m PEM -f ~/.ssh/id_rsa
    (Enter your old passphrase if the key was password-protected.)
  3. To convert from PuTTY private keys (.ppk), use the putty-tools package:
    puttygen id_key.ppk -O private-openssh -o ~/.ssh/id_rsa

Step 4: Enforce Strict File Permissions

OpenSSH refuses to load private keys that are readable by other users on the operating system.

  1. Set directory permissions to 700:
    chmod 700 ~/.ssh
  2. Set private key permissions to 600:
    chmod 600 ~/.ssh/id_rsa
  3. Set public key permissions to 644:
    chmod 644 ~/.ssh/id_rsa.pub
  4. Test loading the key into the SSH authentication agent:
    ssh-add ~/.ssh/id_rsa

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.