skip to Main Content

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.

2

Answers


  1. Chosen as BEST ANSWER

    Abraam's answer is absolutely correct.

    I am detailing the steps I followed to check and change the authorized_keys in remote i.e. the EC2 instance.

    ssh using the ubuntu user:

    ssh -i "~/path/to/key-pair.pem" [email protected]
    

    see the entries in ubuntu authorized_keys:

    cat /home/ubuntu/.ssh/authorized_keys
    

    change user to test

    su test
    

    see the entries in test authorized_keys:

    cat /home/test/.ssh/authorized_keys
    

    if there is a difference and the key-pair is not present

    # copy the key entry from the ubuntu authorized_keys
    nano /home/test/.ssh/authorized_keys
    # new line, paste the copied authorized_key
    

    Save and close the file.

    ssh should work now.


  2. 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.
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search