I used to get this error message recently when I connect to my EC2 instance “Please login as the ec2-user user rather than root user“. In the beginning, I followed their request and changed a few scripts to use ec2-user instead but later on I need to really do root-like actions. “sudo” did not help because I ran to an error message “sudo: sorry, you must have a tty to run sudo“. I decided to look for another solution. I really want my scripts to login and do actions using root.!
After googling the issue for sometime I found the solution. It is simple but I’m not sure about its security implications. Since I usually run EC2 images for short periods to perform my research experiments I did not care too much to investigate the security part. I welcome all comments and feedback, though.
- Edit the /etc/ssh/sshd_config file and change the line that says “PermitRootLogin forced-commands-only” to “PermitRootLogin without-password”.
- Restart the sshd server with “service sshd restart”.
- Edit the /root/.ssh/authorized_keys file. This file should have one line only. It starts by “echo” or “command … sleep 10;” then you can see “ssh-rsa [big long key]”. Remove the text from the beginning of the line until where it says “ssh-rsa”.
- SSH your machine using root with your keypair