Advertisement
BaSs_HaXoR

AmazonAWS Fix EC2 Disconnected SSH RDP NO ACCESS!

Dec 29th, 2023 (edited)
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.41 KB | None | 0 0
  1. ######################################################################
  2. Getting access to misconfigured AmazonAws Instance...
  3. For my case it was the misconfigured Ubuntu /etc/ssh/sshd_config
  4. ######################################################################
  5. Video I used for help:
  6. Troubleshooting an RDP Connection with the EC2 Rescue Tool for Windows
  7. https://www.youtube.com/watch?v=WFsWQrm1qi0&ab_channel=EricintheCloud
  8.  
  9. EC2Rescue for EC2 Windows is a troubleshooting tool that you can run on your Amazon EC2 Windows Server instances. You can use the tool to troubleshoot OS-level issues and collect advanced logs and configuration files for further analysis.
  10. PowerShell Command to Download the EC2 Rescue tool:
  11. https://s3.amazonaws.com/ec2rescue/windows/EC2Rescue_latest.zip?x-download-source=docs
  12.  
  13. (Except I didn't use Windows Nor the Tool)
  14. ----------------------------------------------------------------------
  15.  
  16. Website I used for help:
  17. How to access files of my First Ubuntu from my Second Ubuntu by adumbprogrammer:
  18. https://askubuntu.com/questions/1367142/how-to-access-files-of-my-first-ubuntu-from-my-second-ubuntu
  19. ######################################################################
  20. ######################################################################
  21. #0) First try accessing AmazonAWS Cloud Shell... If to no avail...
  22.  
  23. #0.5) This requires 2 instances. Using parition access...
  24. So what we will do for sake of clarity call EC0 the misconfigured Server
  25. and we will call EC1 the newly setup server we will create later...
  26.  
  27. #1) Go to failed AmazonAWS Server in AWS Control Panel EC2 Instance...
  28.  
  29. #2) Click on Instance checkbox of Name:
  30. In details tab below:
  31. Copy to clipboard: instance-ID: i-###x###x##x#x##x# and paste somewhere for later
  32. and also: "Subnet-ID": subnet-0xx###x#xx###xx## and paste somewhere for later
  33.  
  34. #3) Go to Instance State and "Stop Instance".
  35.  
  36. #) Wait for it in "Instance State" to say "Stopped", then click on storage tab below.
  37. Click on hyperlinked blue Volume-ID of volume in "Block Devices" drop-down tab
  38. Volume-ID Device-Name Vol.size Attachment-status
  39. (vol-0xx##x##xx####x## /dev/sda1 8 Attached
  40.  
  41. A new page will popup, with more details...
  42. Click on hyperlinked vol-0xx##x##xx####x## again.
  43. Click "Actions" drop-down-tab and select "Detach Volume"
  44. Wait till it says "Volume State" is "available" rather than "in use"
  45. For the sakes of being careful copy and paste the vol-0xx... ID for safe keeping
  46. as to not mix them up with the new-server vol-0xx ID.
  47.  
  48. #4) Create a new (Ubuntu) "Launch an " Instance in EC2.
  49. Name it whatever you want, we'll call it EC1.
  50. Quick-Start tab > Ubuntu (Server 22.04 LTS (HVM), SSD, (FREE)(64-bit))
  51. (I already had an ssh putty.ppk key pair, so i used it, otherwise create one...
  52. and after creating the new server, download the ssh-key-pair, we'll need it for putty access later.
  53.  
  54. In Network settings, click on Subnet, where it says "No Preference" tab,
  55. and click it, and paste the "Subnet-ID": subnet-0xx###x#xx###xx## from earlier from EC0.
  56.  
  57. Once you have selected the Subnet-ID for the same subnet as your EC0 for EC1...
  58. Continue to scroll to bottom of page and select the button "Launch instance".
  59.  
  60. #5) You should now have 2 instances. Make sure both Instance States are "Stopped".
  61. Once Stopped, copy instance-ID of EC1 for later...
  62.  
  63. checkbox <check> the EC1 Name of instance.
  64. Click on "Storage" tab below by details.
  65. As before, click "Volume-ID" of any "vol-0xx##x##xx####x##"
  66.  
  67. #6) Once in Volumes window, click the "x" to close selected volume to display all volumes.
  68. Now there should be 2 volume-ids:
  69. "vol-0xx##x##xx####x#0" //EC0
  70. "vol-0xx##x##xx####x#1" //EC1
  71.  
  72. Since we detached EC0's "vol-0xx##x##xx####x#0" Volume-ID, we should be able to use it on EC1.
  73. To do this, click on EC0's "vol-0xx##x##xx####x##".
  74. Then click on "Actions" tab and "Attach Volume".
  75. In the new popup window, for Instance, paste EC1's Instance-ID.
  76. Leave Device name AS-IS. No need to change.
  77. Volume should now be connected to the EC2 instance...
  78. to verify this go to Server2 Instance Details, Storage, and it should be so.
  79.  
  80. Now click on "Connect" Tab in EC2>Instances>instance-id>Connect to instance and choose SSH client.
  81.  
  82. Open putty and connect with: ubuntu@ec2-#-###-###-###.us-east-2.compute.amazonaws.com with SSH-KEY putty.apk create/used earlier, when creating EC1 Instance.
  83.  
  84. #7 Once Connected to EC1 instance, with volume of EC0 on EC1 attached from the same subnet, we can run these commands in ssh (putty) terminal...
  85.  
  86. ---------------------------------------------
  87. In putty ssh of EC1:
  88. lsblk (THIS WILL SHOW ALL AVAILABLE DRIVES)
  89. NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
  90. loop0 7:0 0 24.9M 1 loop /snap/amazon-ssm-agent/7628
  91. loop1 7:1 0 55.7M 1 loop /snap/core18/2812
  92. loop2 7:2 0 63.5M 1 loop /snap/core20/2015
  93. loop3 7:3 0 111.9M 1 loop /snap/lxd/24322
  94. loop4 7:4 0 40.9M 1 loop /snap/snapd/20290
  95. xvda 202:0 0 8G 0 disk
  96. ├─xvda1 202:1 0 7.9G 0 part /
  97. ├─xvda14 202:14 0 4M 0 part
  98. └─xvda15 202:15 0 106M 0 part /boot/efi
  99. xvdf 202:80 0 8G 0 disk
  100. ├─xvdf1 202:81 0 7.9G 0 part
  101. ├─xvdf14 202:94 0 4M 0 part
  102. └─xvdf15 202:95 0 106M 0 part
  103.  
  104.  
  105. sudo mount /dev/xvdf1 /mnt (MOUNT THE DRIVE WE WANT TO FIX)
  106. sudo nano /mnt/etc/ssh/sshd_config (MODIFY FROM /MNT/ DIR THE FILES WE NEED TO FIX)
  107. <modify contents to fix ssh permission problem>
  108. <save>
  109. ---------------------------------------------
  110. shutdown the server instance till it says "Stopped"
  111. Detach Volume from EC1, using the same as before on EC0, but for EC1... (!!!) Make sure you detach the same volume as before! For EC0!.
  112. Reattach (THE CORRECT) volume to EC0 (!!!!)Make sure that you add the right VOLUME!
  113. ALSO!!! Make sure you choose Instance-ID of server1, and CHANGE "DEVICE NAME" TO: /dev/sda1 and NOT: xvdf !!!!
  114.  
  115. Relaunch EC0 (previously misconfigured instance).
  116. Once Running, it should be accessible once again (via ssh in my case).
  117.  
  118. Note** My ipaddress changed for my server after fixing ssh and had to
  119. recopy and paste from amazonAWS "Public IPv4 DNS":
  120.  
  121. ubuntu@ec2-#-###-###-###.us-east-2.compute.amazonaws.com
  122. and in putty-tab, SSH/Auth/Credentials/Private Key File For Authentication: <putty.ppk>
  123. Back in Session "Save" session with "Saved Sessions" id of your choosing.
  124.  
  125. Viola! Back to front, ready to go!
  126. ######################################################################
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement