Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to recover ssh access to amazon EC2 instance after ufw firewall activation by accident

I have activated ufw firewall in an EC2 ubuntu server 12.04 instance, but I forgot to allow access to port 22. Now I can't connect to the instance via ssh. This instance is EBS backed.

like image 309
mnesarco Avatar asked Jan 30 '14 16:01

mnesarco


People also ask

How do I recover SSH in AWS instance?

If your instance is a managed instance in AWS Systems Manager, then use the AWSSupport-ResetAccess document to recover your lost key pair. AWSSupportResetAccess automatically generates and adds a new SSH (public/private) key pair using the EC2 Rescue for Linux tool on the specified EC2 instance.

How do I regain access to EC2 instance?

When you use EC2Config or EC2Launch to reset a lost password, you must use its key pair to retrieve the administrator password. If you've lost the key pair, you can create an AMI of the existing instance, and then launch a new instance. You can then select a new key pair by following the instance launch wizard.

How can we recover lost EC2 SSH key?

Amazon EC2 doesn't keep a copy of your private key; therefore, if you lose a private key, there is no way to recover it. If you lose the private key for an instance store-backed instance, you can't access the instance; you should terminate the instance and launch another instance using a new key pair.


1 Answers

Well, thanks to EBS there is a solution.

  1. Stop your instance
  2. Attach your EBS volume to another instance. if you don't have one, create a micro instance.
  3. Mount yor EBS volume somewhere ie. /opt/recover
  4. List item
  5. Edit {your-ebs-mount}/etc/ufw/ufw.conf and change enabled=yes to enabled=no
  6. Umount the EBS
  7. Detach from the temp instance
  8. Reattach to the original instance. (make sure to attach as root)
  9. Restart the instance

Now you firewall is disbled in your instance, so you can access it via ssh.

like image 152
mnesarco Avatar answered Sep 30 '22 03:09

mnesarco