Permissions 0664 for '/home/user/.ssh/file.pem' are too open for ssh

Hey

Started a new AWS Linux server and created ssh key. I downloaded that file in my ~/.ssh/ directory. I try:

ssh -i ~/.ssh/file.pem ec2-user@my-ec2-ip

And I got

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@         WARNING: UNPROTECTED PRIVATE KEY FILE!          @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Permissions 0664 for '/home/user/.ssh/file.pem' are too open.
It is required that your private key files are NOT accessible by others.
This private key will be ignored.
Load key "/home/user/.ssh/file.pem": bad permissions
ec2-user@xxx.yyy.zzz.ttt: Permission denied (publickey).

How do I fix this?

You need to fix file permission for ssh session as “Permissions 0664” are too open and dangerous.

How to fix permissions 0664 error with ssh command on Linux or Unix or macOS:

  1. Just set up correct permissions using the chmod command:
    chmod 0600 ~/.ssh/file.pem
  2. Make sure correct user ownership is also set using chown command:
    chown $USER:USER ~/.ssh/file.pem
  3. Now login using the ssh:
    ssh ~/.ssh/file.pem ec2-user@ec2-vm-ip-address-here

Linux sysadmin blog - Linux/Unix Howtos and Tutorials - Linux bash shell scripting wiki