2 days ago I changed a few permissions on my Virtual Machine…. after that… disconnected and dead. I wanted to cry. No remote SSH access over public IP, no access via AWS command line, but the status shown was “healthy”. It was disconnected from the world but alive. Like the guy from Johnny Got His Gun.

Step 1: Breaking in from inside

My instance was a T2. AWS offers a EC2 Serial Console connections for a few instances types. So I had to turn off the instance and change the type.

After then Enable the Serial Connection for my IAM user.

Step 2: Login in as a root

After connecting to the instance a terminal appeared. However, the Linux based instances do not have a password for the root and admin users by default.

AWS give us a quick script to fix the boot issues and permissions. Here

I included the single line command to set the root password.

echo -e "PASSWORD\nPASSWORD" | passwd root

Also added the commands to restore the SSH permissions problem:

sudo chmod 755 /etc/ssh
sudo chmod 644 /etc/ssh/moduli
sudo chmod 644 /etc/ssh/ssh_config
sudo chmod 755 /etc/ssh/ssh_config.d
sudo chmod 600 /etc/ssh/ssh_host*
sudo chmod 644 /etc/ssh/ssh_host*.pub
sudo chmod 644 /etc/ssh/sshd_config
sudo chmod 755 /etc/ssh/sshd_config.d

After that, SSH connections were restored

🙂