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

SshAmazon Ec2

Ssh Problem Overview


Question as title. Why is this, I have used the ssh command: ssh -i mykey.pem [email protected] But i get that error, find nothing on google. What am I doing wrong?

Ssh Solutions


Solution 1 - Ssh

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.

Solution 2 - Ssh

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

Solution 3 - Ssh

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. Now vi /etc/ssh/sshd_config and un-comment the line PermitRootLogin yes.
  • Exit vi editor.
  • Now restart ssh daemon by typing service sshd stop then service 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, before no-port-forwarding... and hit enter on ssh-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.

Solution 4 - Ssh

I think it's just asking you to login with another username. Do you happen to have a user called ec2-user? If so, try this instead:

ssh -i mykey.pem ec2-user@xxx-xxx-xx-xx-xxx.compute-1.amazonaws.com

Solution 5 - Ssh

I have faced the same problem when I tried to access my EC2 instance as 'root' through Windows PuTTY client, this is how I solved problem.

  1. Access and edit SSH configuration file, to allow root login and password authentication.
  • Login as ec2-user (by default it is allowed)

  • Enter below command to open ssh config

    sudo vi /etc/ssh/sshd_config

  • Edit SSH configuration file as below using vi, how to use vi editor

    PermitRootLogin yes (remove # at begging if it present)

    PasswordAuthentication yes

  1. Restart SSH

    sudo /etc/init.d/sshd restart

  2. Change/set root password

    sudo passwd root

    type new password and re-enter it (at least 8 characters)

  3. Exit current session and close PuTTY

exit 5. Try again login as root and type previously set password.

Solution 6 - Ssh

Solved! Try compare root key file and user key file)

diff /root/.ssh/authorized_keys /home/user/.ssh/authorized_keys

...and see

Solution 7 - Ssh

For anyone like me that created a new user, copied root's .ssh dir to the new user, set ownership and STILL got this error - look at the new user's ~/.ssh/authorized_keys file. It has SSH params specified that force the prompt. Delete everything from that line up to the ssh-rsa and you'll be good to go.

Or - copy /home/ec2-user/.ssh to the new user homedir instead of /root/.ssh

Solution 8 - Ssh

Edit /etc/ssh/sshd_config, and make sure this is set:

PasswordAuthentication yes

Then reload SSH:

systemctl reload sshd.service

You can now log in as users other than ec2-user.

Solution 9 - Ssh

ssh -i mykey.pem root@xxx-xxx-xx-xx-xxx.compute-1.amazonaws.com

just replace above command to this

ssh -i mykey.pem ubuntu@xxx-xxx-xx-xx-xxx.compute-1.amazonaws.com

its working in my case

Solution 10 - Ssh

For those who are looking for a single, simple line:

sudo ssh -i ./mykey.pem ec2-user@ec2-x-xx-xxx-xxx.us-east-2.compute.amazonaws.com

Note that, you can get the line after the @ from the Public IPv4 DNS section in your instance summary page.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTomView Question on Stackoverflow
Solution 1 - SshTillView Answer on Stackoverflow
Solution 2 - SshJuan HernandezView Answer on Stackoverflow
Solution 3 - Sshbrno792View Answer on Stackoverflow
Solution 4 - SshKlaus Byskov PedersenView Answer on Stackoverflow
Solution 5 - SshAkhil MKView Answer on Stackoverflow
Solution 6 - SshVladimir PereskokovView Answer on Stackoverflow
Solution 7 - SshdelducraView Answer on Stackoverflow
Solution 8 - SshMike BobbittView Answer on Stackoverflow
Solution 9 - SshHammad AliView Answer on Stackoverflow
Solution 10 - SshMostafa WaelView Answer on Stackoverflow