Ssh – root locked out of EC2

amazon ec2ssh

I was in the process of disabling root logins on an AWS EC2 instance. Right after setting PermitRootLogin no and restarting sshd, I closed the terminal on accident — before setting up users with sudo privileges. The result is that my key to get into the instance as root does not work (sshd forbids it) and when I log into the instance using my regular user I can't gain root privileges (the root password was never set). The instance is running ubuntu 8.10. Anyone have any idea how can I fix this?

Best Answer

No, don't terminate the instance, all is not lost!!

  1. boot another instance and shut down the bad instance.
  2. detach the EBS volume from the bad instance and attach it to the new instance.
  3. Mount it in the new instance (i.e., something like sudo mount /dev/xvdf1 /mnt/ )
  4. chroot into it (sudo chroot /mnt) and type passwd.
  5. reset the password or make any other changes you like (vi /etc/ssh/sshd_config, for example!)
  6. Press control-D or type exit to exit the chroot.
  7. umount /mnt
  8. detach the EBS volume from your temporary instance
  9. re-attach or take a snap and create a new AMI based on that snapshot
  10. Boot the fixed box back up!

P.S. next time try Userify to manage your users' keys :)

Related Topic