1

I have mistakenly deleted the .ssh folder from my aws instance and now I am unable to ssh into it. The issue is that node is in my prod environment and I dont want to restart it. I have found the following solution -

Amazon AWS EC2 Deleted Key Locked out of Root

But it suggests that I need to attach my disk to another instance which will cause the node to shutdown and still the solution is not assured. Is there any solution which is provided at aws console which I can utilise. Any help will be appreciated.

Community
  • 1
  • 1
Y0gesh Gupta
  • 2,184
  • 5
  • 40
  • 56
  • Did you delete the .ssh folder from the AWS created user (i.e. "ubuntu" or "ec2-user")? Or from a personalized account made after logging in the first time? – Brooks Mar 18 '16 at 14:26
  • from the aws created user "ubuntu". – Y0gesh Gupta Mar 18 '16 at 15:06
  • Then sorry, you're out of luck. On the bright side, you've learned a lesson. Create a separate user. I've learned the hard way a few times :) – Brooks Mar 18 '16 at 15:08

2 Answers2

8

This is an old issue, but in case someone wonders in here with this problem, there is a quick and relatively painless fix that can be done today by following this procedure from AWS Knowledge Base.

Basic details are as follows:

  1. Generate a Private Key (I'm on Mac bash shell, so will use .pem format)
  2. Run ssh-keygen -yf <KEY-FILE>.pem to get Public Key
  3. Stop the instance.
  4. Actions -> Instance Settings -> View/Change User Data
    Content-Type: multipart/mixed; boundary="//"
    MIME-Version: 1.0
    
    --//
    Content-Type: text/cloud-config; charset="us-ascii"
    MIME-Version: 1.0
    Content-Transfer-Encoding: 7bit
    Content-Disposition: attachment; filename="cloud-config.txt"
    
    #cloud-config
    cloud_final_modules:
    - [users-groups, once]
    users:
      - name: USERNAME-REPLACE
        ssh-authorized-keys: 
        - PUBLIC-KEY-PAIR-REPLACE
    

    Replace USERNAME-REPLACE with instance username (e.g.: ubuntu, ec2-user, etc.)

    Replace PUBLIC-KEY-PAIR-REPLACE with what you printed in step 2, including ssh-rsa part.

  5. Save
  6. Start the instance
  7. Verify SSH works
  8. Stop the instance
  9. Remove User Data you entered
  10. Start the instance
Vlad
  • 3,866
  • 1
  • 24
  • 20
3

There is no solution that doesn't involve working with the EBS volume detached from the instance.

Perhaps you could launch a copy of this instance, fix it, and then move the elastic IP address to the fixed instance with minimal down time?

Mark B
  • 183,023
  • 24
  • 297
  • 295