SSHing into EC2 server via gives error Please login as the ec2-user user rather than root user

You log in as ec2-user as Klaus suggested:

ssh -i key.pem ec2-user@host

... and then you use sudo to run commands. E.g., to edit the /etc/hosts file which is owned by root and requires root privileges: sudo nano /etc/hosts.

Or you run sudo su to become the root user.


By default root user is not allowed to login but you can use ec2-user as indicated by others.

Once you login with ec2-user you switch to root and change the SSH configuration.

To become the root user you run:

sudo su -

Edit the SSH daemon configuration file /etc/ssh/sshd_config, e.g. by using vi, and replace the PermitRootLogin entry with the following:

PermitRootLogin without-password

Reload the SSH daemon configuration by running:

/etc/init.d/sshd reload

The message Please login as the ec2-user user rather than root user. is displayed because a command is executed when you login with the private key. To remove that command edit ~/.ssh/authorized_keys file and remove the command option. The line should start with the key type (Eg. ssh-rsa).

(*) Do at your own risk. I recommend you to leave always a console open just in case you're not able to login after you make the configuration changes.

For reference you can read the man pages:

man sshd_config
man sshd