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
I have encountered a similar problem when setting up a hadoop cluster on Amazon ec2.
My head node needs to have root ssh access to each worker/slave nodes. I aliased the connects by adding each slave node's IP address, private address, and alias name to the /etc/hosts/
file. (I get that data by running the command echo -e "`hostname -i`\t`hostname -f`\talias-name"
where alias-name
is what I call each node (head
or n1
for example). Then I put that output for each node in every node's /etc/hosts
file.
The problem I have been encountering is that when I type ssh n1 while in my head node to ssh into my first slave node, I get that same error message: Please login as the use "ec2-user" rather than the user "root".
So after doing some research, I figured out how to fix it.
First:
- ssh into your server. non-root (ec2-user) access is fine here.
- Then
su -
your way into root. Nowvi /etc/ssh/sshd_config
and un-comment the linePermitRootLogin yes
. - Exit vi editor.
- Now restart ssh daemon by typing
service sshd stop
thenservice sshd start
.
Second:
- Now, here is the part I had to dig for,
- run
vi /root/.ssh/authorized_keys
- Comment out everything up to
ssh-rsa.
Just put a#
at the beginning of the file's content, beforeno-port-forwarding
... and hit enter onssh-rsa
to move it to the next line (this way you dont have to delete anything in case you want to backtrack). - exit vi editor
Now you should be able to login to root without that error message popping up.
Also, if you are using aliases for a cluster setup; Repeat the same steps on each node. First ssh in using ec2-user then follow the steps.
After adding the IP address, private address, and alias name info to your /etc/hosts
file you should be able to ssh into each node's root using the alias name for example ssh n1
.
The tutorial I followed is here: https://www.youtube.com/watch?v=xrxQXfE7t9A
But it didnt discuss the problem with root login.
Hope that helps! It worked for me.
*Keep in mind that I havnt taken any security into concern. This is simply a practice/dev setup.