ssh ignores your EC2 .pem file because its permissions are too open
@ 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.
Fix it fast
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.pem2Match what you see, then jump to its fix
- 1
Permissions 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
chmod 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
Permissions 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
- 1
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.pem2Fix
Remove all access for group and others. AWS documents chmod 0400, which leaves the file read-only for you.
chmod 400 ~/Downloads/my-key.pem3Verify
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.comConfirmed by docs.aws.amazon.com
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.pem3Verify
The listing now shows -r-------- and ssh stops warning.
ls -l ~/.ssh/my-key.pemConfirmed by docs.aws.amazon.com
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:r3Verify
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.comConfirmed by docs.aws.amazon.com
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
Security
SSH refuses your key and ends with Permission denied (publickey)
user@203.0.113.10: Permission denied (publickey,password).
The server accepted the connection but found no key it would take.
5 causes, each with a fixSecurity
AuthenticationException: your certificate validation callback returned false
System.Security.Authentication.AuthenticationException
This message means a validation callback was installed, yours or one a library added, and it returned false.
3 causes, each with a fixSecurity
SSH to an EC2 instance fails with Permission denied (publickey,gssapi-keyex,gssapi-with-mic)
ec2-user@ec2-203-0-113-25.compute-1.amazonaws.com: Permission denied (publickey).
On EC2 this error almost always means the instance did not accept your key for that user.
3 causes, each with a fixSecurity
gcloud init cannot finish the sign-in step
Enter authorization code
gcloud init signs in through a browser.
3 causes, each with a fixSecurity
A GitHub Actions SSH deploy step cannot log in to the server
ssh: handshake failed: ssh: unable to authenticate
The runner reached your server but the server refused the login.
4 causes, each with a fixSecurity
SSH to GitHub asks to confirm the host, or hangs on that prompt
ED25519 key fingerprint is SHA256:
SSH has never seen github.com from this machine, so it asks you to trust the server key.
2 causes, each with a fix