How to Solve “Disconnected: No supported authentication methods available (server sent: publickey)” with Ubuntu AWS EC2

How to Solve “Disconnected: No supported authentication methods available (server sent: publickey)” with Ubuntu AWS EC2

This error happened to me and cost me a lot of time to solve it, and couldn?t find a solution for it online, but after some help from AWS support, I managed to solve it, so I am sharing it hoping it will help others.

Background

I was trying to connect to Ubuntu AWS EC2 through PuTTy (I?ve tried other alternatives too) but when I do I get the error ?Disconnected: No supported authentication methods available (server sent: publickey)?

Image for postImage for post

Troubleshooting

This error can occur under the following circumstances:

  • You?re not connecting with the appropriate user name for your AMI when you negotiate an SSH session with an EC2 instance.
  • You?re using the wrong private key when you negotiate an SSH session with an EC2 instance.

If you?re connecting with an appropriate user name, verify that you are using the correct private key by following these steps:

Sign in to your AWS account and open the Amazon EC2 console. In the navigation pane, choose Instances. Find the EC2 instance you want to connect to using SSH. In the Key Name column, verify the name of the private key you?re using to connect through SSH.

If you?re using PuTTY:

Verify that the SSH private key matches the private key you see in the Key Name column for your EC2 instance in the console. If your instance is based out of the Ubuntu OS, the default user name should be ubuntu. Verify that your private key (.pem) file is converted to the format recognized by PuTTY (.ppk). For more information, see Converting your Private Key Using PuTTYgen.

  • permission issue with directory contains the ssh key (/home/ubuntu)

In my case, I have accidentally executed the command of sudo chmod -R 777 . for the EC2 directory ?/home/ubuntu?, so this resulted in denying the access to the EC2 and the wrong permission was like

Image for postEC2 wrong permisson

Meanwhile the right permission should be

Image for postEC2 right permission

To solve this problem I had to create recovery instance in the same A-Z as impacted instance (in case of other wrong A-Z is selected you will be not able to use + attach volume from impacted instance, which has to be attached + mounted on recovery instance to work on permissions).

  1. Create EC2 in the same Availability Zone of the impacted instance
  2. Stop the impacted instance.
  3. Detach the volume of the impacted instance
  4. Attach the volume to the new recovery instance
  5. Connect to recovery instance
  6. Mount the volume on the recovery instance as below

sudo mkdir /mountpointcd ../ lsblk # to know where the new volume is attached, in my case “/dev/xvdf1″sudo mount /dev/xvdf1/mountpoint # mount step/dev/xvdf1 # this gave me permission deniedcd /mountpoint/var/loglsnano auth.log

This will show you the cause of error which is ?Authentication refused: bad ownership or modes for file /home/ubuntu/.ssh/authorized_keys?

Image for post

ls -l /mountpoint/home/ubuntu/.ssh/authorized_keys

Image for post

sudo chmod 600 /mountpoint/home/ubuntu/.ssh/authorized_keysto make sure the permission has been updated executels -l /mountpoint/home/ubuntu/.ssh/authorized_keys

next step is to

ls -ld /mountpoint//home/ubuntu/.ssh/sudo chmod 700 /mountpoint/home/ubuntu/.ssh

ls -ld /mountpoint//home/ubuntusudo chmod 755 /mountpoint/home/ubuntu/

ls -ld /mountpoint//home/ubuntu

ls -ld /mountpoint//home/

Next:

  • Stop the recovery instance
  • Detach the volume of the impacted instance from the recovery instane
  • Attach the volume to the impacted instance

Image for post

  • Start the impacted Instance
  • Connect to the instance through PuTTY
  • Voila, now you can connect properly without errors.

References:

https://chmod-calculator.com/

http://catcode.com/teachmod/

https://www.linode.com/docs/tools-reference/tools/modify-file-permissions-with-chmod/

https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/TroubleshootingInstancesConnecting.html https://aws.amazon.com/premiumsupport/knowledge-center/linux-credentials-error/

1

No Responses

Write a response