EC2 – Permission denied(public key) while the same key works for other instance and other user on the instance?

Everything worked great till yesterday. Today I am unable to ssh to my ec2 instance.

The settings are saved in .ssh/config.

The error I get, when using test username:

.us-east-2.compute.amazonaws.com: Permission denied (publickey).

I went ahead and double checked the permission on the .pem file:

-r--------

I have another ec2 running with the same .pem so I connected to it. No problem, login done.

I then tried to ssh into the problematic ec2 with ubuntu user and same .pem file. It worked I was able to login.

I then switched users and the test user exists.

I am not sure about what the issue might be.

Exited the ssh session and tried connection again using the test user and got the same error.

I found an answer here:

Same problem

But the answer is not very helpful.

>Solution :

It might be permission issue on the .ssh directory of the test user on the remote box

  • Connect the same way with ubuntu user.
  • Compare the permissions on ssh directory of test user and all its content (authorized keys, known host, config) with the one of ubuntu user.
  • Check content of authorized_keys file in test ssh directory and make sure it matches the public key of the private key you are connecting with.
  • You can use ssh -vvv while connecting, to check if it is using the right key while connecting.
  • Final resort: tail the ssh logs while you are trying to connect with test user.

Leave a Reply