How to ssh to your Amazon EC2 instance using by root?

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.

  1. Edit the /etc/ssh/sshd_config file and change the line that says “PermitRootLogin forced-commands-only” to “PermitRootLogin without-password”.
  2. Restart the sshd server with “service sshd restart”.
  3. 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”.
  4. SSH your machine using root with your keypair
Disclaimer: I used the ideas in another post “” Thanks to its author!

One comment on “How to ssh to your Amazon EC2 instance using by root?

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s