10

I am working with some Amazon EC2 servers that are up and running, and I need to SSH into the servers. I don't have any keys that were generated when the servers were first set up (someone else did it long before I got here). Can I still get into the servers without the key files?

FWIW I've tried a lot of things to SSH into the box so far, including generating new key pairs in the EC2 dashboard, and nothing seems to be working. This Amazon AWS support post and this answer seem to indicate that I'm out of luck unless I want to make an AMI of my current server and then use it to instantiate a whole new EC2 server instance (just to get the .pem file generated at that time). Is that really the only way I can get into the box at this point?!

Eric
  • 1,087
  • 2
  • 12
  • 24

2 Answers2

26

In short: Yes, you can, but not without some work.

You'll need to do the following:

(For these steps, assume that the machine you're having trouble connecting to is called server-01.)

First, before starting these steps, take a snapshot of your server.

  1. Start a new, temporary instance. Call it server-02.
  2. Stop server-01. Don't terminate it, just stop it.
  3. Un-attach the root (/) EBS volume from server-01, and attach it to server-02 as, say /dev/sdb.
  4. Sign into server-02, and run: $ mkdir /mnt/temp && mount /dev/sdb /mnt/temp. This will mount server-01's root partition within the (temporary) server-02.
  5. Now you should be able to: $ vi /home/<user>/.ssh/authorized_keys and copy/paste in your public key. When you've done that, save and close the file.
  6. Now run: $ cd / && umount /mnt/temp to umount server-01's root partition from server-02.
  7. Now, just un-attach that volume from server-02, attach it back to server-01, and then start server-01. When it starts up, you should be able to ssh in again.
Justin Ohms
  • 103
  • 4
EEAA
  • 108,414
  • 18
  • 172
  • 242
  • 2
    Just to add to this excellent answer, I found this blog post which walks through doing it from the command line. http://alestic.com/2011/02/ec2-fix-ebs-root – Eric Feb 26 '13 at 05:15
0

Just a tip before anyone tries EEAA's answer,

If you have not run the iptables save or iptables-save command to save your configurations, then just restart the server. Citing this answer on AskUbuntu, I learnt that iptables saves your configurations in the RAM itself unless you explicitly save it to the configuration files.

EC2 instances can be easily rebooted from the EC2 Dashboard or aws-cli

retr0
  • 109
  • 3
  • Umm, it looks like an answer to another question mistakenly posted here. There is nothing about firewall/iptables in this question. – kubanczyk Jul 02 '19 at 21:12
  • My bad, this answer is for when all traffic gets blocked to a server if all IP addresses are blocked. Let me know if it needs to be taken down. – retr0 Jul 03 '19 at 11:59