4

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.

2
  • 2
    IP addresses on EC2 change every time you shutdown and restart the instance. That is by design.
    – Rico
    Feb 13, 2014 at 15:54
  • 2
    The guy who voted down can you please tell what made you to do so? Feb 14, 2014 at 5:06

4 Answers 4

8

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.

6
  • that might be specific to the AMI you used, that's why I asked you about what kind of Linux instance you launched
    – alkar
    Feb 13, 2014 at 11:48
  • Centos 6.2 version and 64 Bit Feb 13, 2014 at 12:02
  • Unfortunately I've not used CentOS before (and I can't find 6.2 on the Marketplace). It might be that after the initial login, it locks out root as a security measure.
    – alkar
    Feb 13, 2014 at 12:30
  • So, I used this iamge : vishal-Centos-6.2-64bit-sda1 - ami-06f85f6f Feb 13, 2014 at 13:01
  • 1
    @ankurverma: Using random AMIs you find on EC2 is highly discouraged. You should only use AMIs where you know and trust the person/company that created the AMI. Feb 14, 2014 at 22:44
1

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.

1

the IP gets changed every time I stops and starts the machine.

This is a normal behavior with an automatically-assigned IP. You lose the IP as soon as it is not being actively used, then Amazon assigns you a new one.

In order to keep your IP, you need to reserve an Elastic IP (EIP for short) and allocate it to your instance. This IP will not detach from your instance. It will remain in your account even if you destroy the instance. The EIP is yours until you release it. The first EIP per running instance is free; if the instance is not running, however, you pay a symbolic fee per hour of idle time (that is if you stop the instance or terminate it).

The EIP is reserved for you until you release it to Amazon.

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

PermitRootLogin 

in

/etc/ssh/sshd_config 

will fix it; then you also need to append the public key to the authorized_hosts file that is located in

$HOME/.ssd

of the root user.

This post explains how to do it: https://www.cloudinsidr.com/content/how-to-rsync-files-and-directories-between-remote-hosts/

I launched one EC2 instance and used cygwin to do the stuff

If I may make a suggestion: Don't use Cygwin. Use PuTTY: http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html

0

Per normal AMI structure "cloud-user" should be a NOPASSWD sudoer, meaning you can just prepend "sudo " to any commands you need.

I know this does not technically answer your question, but you didn't specify why actual root ssh is needed. You'll probably want to modify design/workflow so actual "root" usage is not needed.

Also make sure you have an EBS-backed instance. If you're using instance (ephemeral) stores I believe any file system modifications are lost at reboot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.