Solve - Warning Unprotected Private Key File AWS EC2 Error

avatar

Borislav Hadzhiev

Sat Sep 25 20212 min read

Solve Unprotected Private Key File AWS Error #

The reason the "Warning: Unprotected Private Key File" AWS error occurs is because we're trying to SSH into an EC2 instance using a private key that allows read access to other users.

unprotected private key file error

A private key must only be readable by your user on the machine in order to allow you to SSH into an EC2 instance.

In order to solve the "Warning: Unprotected Private Key File" error in AWS EC2, update the permissions of the private key file to only allow read access from the current user.

Open your terminal in the directory where your private key is located and run the chmod command.

shell
chmod 600 ec2-private-key.pem

Try to ssh into the EC2 instance again, for amazon linux instances the command looks like:

shell
ssh -i "ec2-private-key.pem" ec2-user@YOUR_EC2_PUBLIC_DNS

If you are still getting the error try changing the permissions of the directory that contains the private key. For example if you store your keys in a directory called ec2, you would do:

shell
chmod 755 ./ec2
When trying to SSH into the instance, make sure your terminal is located in the directory where your ec2-private-key.pem file is stored, otherwise you might get a permissions denied error, because the file could not be found.

Lastly, make sure you're using the correct ssh command, the username varies between the different amazon machine images (e.g. ubuntu or ec2-user).

To get the correct ssh command for your EC2 instance:

  1. In the EC2 console, click on the checkbox next to your instance's name
  2. Click on Actions and select Connect
  3. Click on the SSH client Tab and copy the example ssh command

copy ssh command

Further Reading #

Join my newsletter

I'll send you 1 email a week with links to all of the articles I've written that week

Buy Me A Coffee