Solving the error "Permissions for 'X.pem'​ are too open"​ while connecting to an EC2 instance.

Solving the error "Permissions for 'X.pem' are too open" while connecting to an EC2 instance.

In order to establish an SSH connection to our EC2 instance from Windows, we need a Key Pair (.pem file) that is going to be locally stored in our PC. AWS will give us the steps to get this file before we launch our EC2 instance. As soon as we open our CMD and paste the command to establish the SSH connection (ssh -i "YourKeyPair.pem" your-user@your-ec2-domain-name), we might get the following error:

No hay texto alternativo para esta imagen

The reason behind it, is that we need to place the .pem file on the path we are using to open the SSH connection. In other words, just place the .pem file on the right folder. However, sometimes we could face another issue. Let us say we try to establish the SSH connection again, this time with the .pem file properly located, and then we receive the following error:

No hay texto alternativo para esta imagen

This error means that the .pem file is accessible by other users and this is not supposed to be the case since the nature of the .pem file is to be a private key. If other users have access to it, is not considered private. To fix this, we are going to run the following commands using PowerShell, changing the name of your .pem file accordingly:

No hay texto alternativo para esta imagen

Once we finish these steps, we will be able to connect to our EC2 Instance using SSH.

No hay texto alternativo para esta imagen


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Commands:

$path = ".\TestingEC2.pem"

icacls.exe $path /reset

icacls.exe $path /GRANT:R "$($env:USERNAME):(R)"

icacls.exe $path /inheritance:r

Thank you Enrique Gabriel for the post. Changing the *.pem file location and giving the absolute path of .pem file to the ssh command worked for me. Though I changed the permissions to only read and read/execute for the user using which I logged into my local Windows machine. I tried it over Windows Command Prompt. Thank you. 😉

Like
Reply

Great post Enrique Gabriel, actually I use a Linux base OS due its facility to manage permissions. WSL on Windows is a good option to get it on

To view or add a comment, sign in

More articles by Enrique Gabriel

Explore content categories