SSH private key permissions (local machine, windows)

0

Dears, I can't connect with vscode (from win) remotely on ec2 linux because I always get the same error WARNING: UNPROTECTED PRIVATE KEY FILE!

Permissions for '.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 "
.pem": bad permissions

this post talked about the same problem 12 years ago https://stackoverflow.com/questions/8193768/unprotected-private-key-file-error-using-ssh-into-amazon-ec2-instance-aws

I already tried changing the permissions on my local .pem file with GitBash (chmod 600 mykey.pem) and then I also tried the following procedure: "For Windows, run the following command in PowerShell to grant explicit read access to your username: icacls "***.pem" /grant :R Then navigate to the private key file in Windows Explorer, right-click and select Properties. Select the Security tab > Advanced > Disable inheritance > Remove all inherited permissions from this object." But it keeps giving me the same error about permissions. Last night I lost a few hours on SSH.. ..

Yumin
asked a month ago413 views
4 Answers
1

Maybe the problem is that I had created a new key, and the system automatically assigned it to my instance. Then I deleted the old key. So I have to copy the new public key into the ssh dir, but I don't know how to do it. I only have the key.pem file locally. Thank you

Yumin
answered a month ago
0
Accepted Answer

Hello.

The answers below may be helpful.
Maybe you need to set full control over your SSH keys.
There are several other answers listed, so I recommend checking them all out.
https://superuser.com/a/1296046

Change the owner to you, disable inheritance and delete all permissions. Then grant yourself "Full control" and save the permissions. Now SSH won't complain about file permission too open anymore.

profile picture
EXPERT
answered a month ago
profile picture
EXPERT
reviewed a month ago
  • Hello, thanks, in this way I solved the problem of permissions on the local .pem key. Now I have the other error:

    ec2-user@****: Permission denied (publickey,gssapi-keyex,gssapi-with-mic)

    I have changed the permissions of the linux dir (superuser): chmod 755 <mount_point>/home chmod 700 <mount_point>/home/ec2-user chmod 700 <mount_point>/home/ec2-user/.ssh chmod 600 <mount_point>/home/ec2-user/.ssh/authorized_keys

    but the error in connection still remains.

  • Session Manager may be available. Try connecting with Session Manager and setting an SSH key for the required Linux user. https://docs.aws.amazon.com/systems-manager/latest/userguide/session-manager-getting-started.html

0

Hi Yumin,

For the authorized keys, the permissions need to be a little more permissive.

Set the permissions to 644 and it should solve the issue.

Let me know if it worked.

Sincerely, Mukul Dharwadkar

profile picture
answered a month ago
0

SSH Authentication Error: Permission Denied

It seems like you've addressed the permissions for the .pem key successfully, but you're encountering another error related to SSH authentication. The error message Permission denied (publickey,gssapi-keyex,gssapi-with-mic) typically indicates that the SSH client is unable to authenticate using the public key.

Things to Check:

  1. SSH Key Pair: Ensure that you're using the correct SSH key pair to connect to the EC2 instance. The public key (<key_name>.pub) should be added to the authorized_keys file on the server.

  2. Permissions on SSH Directory and Files: Verify the permissions for the .ssh directory and authorized_keys file. Ensure that the ownership and permissions are correctly set for the entire .ssh directory and its contents.

  3. SSH Agent: If you're using an SSH agent to manage your keys, make sure that the correct key is added to the agent (ssh-add <path_to_private_key>).

  4. SSH Configuration: Check for any custom SSH configurations (~/.ssh/config) that might interfere with the authentication process.

  5. Logs: Review the server-side logs (/var/log/auth.log or /var/log/secure) for clues about why the authentication is failing.

  6. Instance Configuration: Ensure that the instance itself allows SSH connections and that any firewall rules (such as security groups or network ACLs) aren't blocking the connection.

SSH Permissions Setup

To avoid SSH permission denied issues, ensure correct file permissions for SSH-related files:

  1. ~/.ssh directory: Set permissions to 700 (drwx------) to allow only the owner (ec2-user in your case) full access:

chmod 700 ~/.ssh

  1. ~/.ssh/authorized_keys file: Set permissions to 600 (-rw-------) to restrict access to only the owner (ec2-user):

chmod 600 ~/.ssh/authorized_keys

These permissions prevent unauthorized access to your SSH keys while allowing authentication. Replace ~ with the actual home directory path of the ec2-user.

After setting permissions, attempt to connect to the EC2 instance again. If issues persist, check SSH server logs for detailed error messages.

profile picture
answered a month ago
profile picture
EXPERT
reviewed a month ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions