Amazon Ubuntu Instance Suffers `Permission Denied(publickey).`

N.B. There are dozens of reasons that can lead to this problem, such as incorrect permissions on your .pem file, an incorrect username such as ec2-user or ubuntu, a typo in your command, and so on.

In my case, if I am right, the problem was caused by running sudo chmod -R ./ in the wrong directory, namely my home folder.

The solution is to set the home folder permissions back.

  1. Stop the problematic instance.
  2. Create a new instance, then stop the new problem-free instance.
  3. Make sure the newly created instance is in the same Availability Zone, such as us-west-2c. This can be set in the Network step under the Subnet menu.
  4. Detach the EBS volume from the problematic instance and attach it to the new problem-free instance.
  5. Enter your instance ID and the mount point, which looks like /dev/sda2.
  6. Start the new instance and mount the second drive that you just attached.
[ubuntu ~]$ lsblk
NAME  MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
xvdf  202:80   0  100G  0 disk
xvda1 202:1    0    8G  0 disk /
[ubuntu ~]$ sudo mount /dev/xvdf /mnt
  1. Change directory to the mounted path and restore the permission of your home directory to 755.
[ubuntu ~]$ cd /mnt/home/
[ubuntu ~]$ chmod 755 yourusername
  1. Stop the new instance and detach the volume owned by the problematic instance.
  2. Reattach the just-detached volume to the original problematic instance.

Leave a Reply