Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Security and hardening

ssh ignores your EC2 .pem file because its permissions are too open

Error@ WARNING: UNPROTECTED PRIVATE KEY FILE! @

Short answer

OpenSSH refuses a private key that other users can read or write, then falls back to Permission denied (publickey). The fix is to make the .pem file readable only by you: chmod 400 on Linux and macOS, or icacls with inheritance removed on Windows. Keys kept on a shared or Windows-mounted drive need to move first.

  • Applies to OpenSSH 9.x and 10.x client, macOS 15 and 26, Ubuntu 24.04, Windows 11 OpenSSH
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Look at the mode of the key file. Anything other than -r-------- or -rw------- (owner only) triggers the warning.

    ls -l ~/Downloads/my-key.pem
  2. 2Match what you see, then jump to its fix

    1. 1Permissions 0644 (or 0664, 0777) for 'my-key.pem' are too open.The key file is readable by group or others (Linux and macOS)Fix
    2. 2chmod 400 runs without an error, but ls -l still shows -rwxrwxrwx and the warning staysThe key sits on a Windows-style file system or a shared folder (WSL, external drive)Fix
    3. 3Permissions for 'my-key.pem' are too open (from the Windows OpenSSH client in PowerShell or cmd)Windows file ACL gives other users access to the keyFix

What does the error look like?

  • @ WARNING: UNPROTECTED PRIVATE KEY FILE! @
  • Permissions 0644 for 'my-key.pem' are too open.
  • It is required that your private key files are NOT accessible by others.
  • This private key will be ignored.
  • Permission denied (publickey).

Cause 1 of 3

The key file is readable by group or others (Linux and macOS)

1Check

The browser or a copy command created the file with a default mode such as 0644, which lets other accounts read it. ssh checks this mode before it uses the key.

stat -c '%a %U %n' ~/Downloads/my-key.pem   # on macOS: stat -f '%Lp %Su %N' ~/Downloads/my-key.pem

2Fix

Remove all access for group and others. AWS documents chmod 0400, which leaves the file read-only for you.

chmod 400 ~/Downloads/my-key.pem

3Verify

Connect again with the key and the default user name for your AMI. The warning should be gone.

ssh -i ~/Downloads/my-key.pem ec2-user@ec2-203-0-113-25.compute-1.amazonaws.com

Confirmed by docs.aws.amazon.com

Did this fix it?

Cause 2 of 3

The key sits on a Windows-style file system or a shared folder (WSL, external drive)

1Check

Drives mounted from Windows into WSL, and some external or network drives, ignore chmod and report one fixed mode for every file. The mode you set never sticks.

ls -l /mnt/c/Users/me/Downloads/my-key.pem
mount | grep -E "/mnt/c|drvfs"

2Fix

Copy the key into your Linux home folder, which supports modes, and protect the copy. Use that copy from now on.

mkdir -p ~/.ssh
cp /mnt/c/Users/me/Downloads/my-key.pem ~/.ssh/
chmod 400 ~/.ssh/my-key.pem

3Verify

The listing now shows -r-------- and ssh stops warning.

ls -l ~/.ssh/my-key.pem

Confirmed by docs.aws.amazon.com

Did this fix it?

Cause 3 of 3

Windows file ACL gives other users access to the key

1Check

On Windows, inherited permissions from the Downloads folder usually include other accounts or groups. Show the ACL on the file.

icacls.exe "$env:USERPROFILE\Downloads\my-key.pem"

2Fix

Reset the ACL, grant only read to your own account, then remove inherited permissions. These are the three icacls commands AWS lists.

$path = "$env:USERPROFILE\Downloads\my-key.pem"
icacls.exe $path /reset
icacls.exe $path /GRANT:R "$($env:USERNAME):(R)"
icacls.exe $path /inheritance:r

3Verify

The ACL lists only your account, and ssh connects.

icacls.exe $path
ssh -i $path ec2-user@ec2-203-0-113-25.compute-1.amazonaws.com

Confirmed by docs.aws.amazon.com

Did this fix it?

How do I stop it coming back?

  • Move a downloaded .pem file to ~/.ssh and run chmod 400 on it before the first connection.
  • Add an IdentityFile line to ~/.ssh/config so you never pass a path from a loose Downloads folder.
  • Do not store keys on shared, synced or Windows-mounted folders; use your Linux home folder.

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.

Which stack is behind this page?

Related fixes

Next step

Want this handled for good?

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