Locked myself out of SSH with UFW in EC2 AWS
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Locking yourself out of SSH access in an Amazon Web Services (AWS) EC2 instance is a common mistake among system administrators, especially when working with firewall configurations like Uncomplicated Firewall (UFW). This can be a concerning situation given the importance of SSH in managing and configuring instances. However, there is a systematic approach to resolving this issue and regaining access without causing data loss.
Understanding UFW
Uncomplicated Firewall (UFW) is a frontend to `iptables` and is designed to simplify the process of managing a firewall server. It is specifically geared to allow uncomplicated, easy deployments in network security. UFW uses a command-line interface for managing firewall rules and is popular among systems running Ubuntu.
How UFW Interacts with SSH
SSH runs on port 22 by default. When UFW is enabled, all incoming connections are blocked unless a rule explicitly allows them. You might inadvertently lock yourself out if you forget to allow SSH before enabling UFW.
Example of Locking Yourself Out
- Initial SSH Access:
- Navigate to the EC2 dashboard.
- Select the locked-out instance.
- Choose Instance State > Stop.
- Go to Volumes under the EC2 section.
- Find the root volume (commonly `/dev/xvda`).
- Detach the volume.
- Launch a new temporary instance in the same Availability Zone.
- Attach the detached volume to this instance as a non-root volume (e.g., `/dev/xvdf`).
- SSH into the rescue instance.
- Mount the volume:
- Allow SSH:
- Unmount the volume.
- Detach and reattach the volume to the original instance as the root volume.
- Start your original instance and ensure SSH access is restored.
- Ensure that the IAM role attached to your instance has permissions for SSM.
- The instance must be in a VPC and security group that allows SSM access.
- Go to AWS Systems Manager > Run Command.
- Select `AWS-RunShellScript`.
- Choose target instance(s).
Related reading
- log to specific logstream in cloudwatch from lambda
- Logs complaining extensions/v1beta1 Ingress is deprecated
- Make a bucket public in Amazon S3
- Make Flask's url_for use the 'https' scheme in an AWS load balancer without messing with SSLify
- Log4j2 - Unrecognized conversion specifier xwEx starting at position 160 in conversion pattern
- Log4j2 including library name in stacktrace
- Making AWS MSK public using NLB and IAM authentication - Hostname verification failed
- Making calls to AWS api gateway endpoint with api key using rest client POSTMAN

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.