Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restarting EC2 and login as root

I am new to this AWS.

I launched one EC2 instance and used cygwin to do the stuff, everything is working I launched the EC2 machine, I am able to :

  1. Login using ssh and pem file i.e key value pair name file using the command as : ssh -i pem.file root@ip, (this works and I gets login as a root user)
  2. Getting the root access using sudo -i
  3. Installing any thing, installing the servers like Apache, tomcat and all that

But once I stop the instance and starts it again, it does not allow me to login as root?

First of all, the IP gets changed every time I stops and starts the machine.

Secondly, why once machine starts again after being stopped, the command : ssh -i pem.file root@ip shows following message:

enter image description here

Please shed some light.

like image 556
Ankur Verma Avatar asked Dec 25 '22 13:12

Ankur Verma


2 Answers

It is because the AMI you used to launch the instance does not permit root login over SSH. What kind of Linux instance did you launch?

You can change that in /etc/ssh/sshd_config and the option would be PermitRootLogin yes (make sure to restart the sshd service). However, in order for the .pem key file to work, you'd have to setup the public key to work for root. The quick way to do that is to copy /home/cloud-user/.ssh/authorized_keys to /root/.ssh/authorized_keys. However, this is not a recommended practice.

The Please login as the user "cloud-user" rather than the user "root". message you are getting is because of the command option in the /root/.ssh/authorized_keys.

As for the public IP of the instance, that's because your instance is what's called EC2 Classic. If you want the public IP to persist you will have to launch the instance in a custom VPC with an Internet Gateway and attach an Elastic IP to it. This is a good guide.

like image 160
alkar Avatar answered Jan 13 '23 01:01

alkar


Did you install the cloud-init RPM? The cloud-init RPM for the CentOS AMI creates a user named cloud-user but without the requisite sudo privileges. So after a reboot there's no way to sudo or do anything meaningful.

To fix this issue, launch a vanilla CentOS instance, install the cloud-init RPM, but before rebooting, modify /etc/cloud/cloud.cfg to grant sudo privileges for cloud-user.

  default_user:
    name: cloud-user
    gecos: Cloud user
    groups: [wheel, adm]
    sudo: ["ALL=(ALL) NOPASSWD:ALL"]
    shell: /bin/bash

The sudo privileges of cloud-user will now survive a reboot. Alternatively you can retain root access as show here.

like image 37
toppur Avatar answered Jan 13 '23 00:01

toppur