Get AWS EC2 root and Change Login Way

Instroduce

When you launch a EC2 instance, the default:

And ofcourse you have only user permission.


STEP by STEP

  1. Login to EC2 by default:

    $ chmod 400 my-key-pair.pem
    $ ssh -i my-key-pair.pem centos@{public ip}
    
  2. Switch permission from user to root:

    $ sudo su
    Then you are able to change root password and do any root things.

  3. Modify ssh to allow root login:

    # vi /etc/ssh/sshd_config
    Chenge:
    PermitRootLogin yes
    Save and Exit.

  4. Modify ssh allow password login:

    # vi /etc/ssh/sshd_config
    Change:
    PasswordAuthentication yes
    Save and Exit.

  5. Modify allow use .pem file for root login:

    vi /root/.ssh/authorized_keys
    Remove below section:
    command="echo 'Please login as the ec2-user user rather than root user.';echo;sleep 10"
    Save and Exit.

  6. Restart sshd:

    # service sshd restart

You are now get root permission and able to login by password and .pem file.