enable firewall port 22 on ec2 server after disable it
Asked Answered
D

3

6

Step error: 1. sudo ufw enable 2. Command may disrupt existing ssh connections. Proceed with operation (y|n)? y 3. exit Before ssh again and can not access. On Security Group, port 22 is opening But firewall disable port 22. How to access server again?

Deanndeanna answered 11/8, 2016 at 4:9 Comment(2)
Reboot it and hope that it does not auto start the firewall. askubuntu.com/questions/545330/ufw-not-enabled-at-startupCram
i have rebooted. But can not access server.Deanndeanna
S
20

You could use the following Simplest way (user-data) to turn off the ufw.

  1. Stop the instance
  2. In Instance Settings, View/Change User Data
  3. Copy and Set the below user data as plain text and save
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:
- [scripts-user, once]

--//
Content-Type: text/x-shellscript; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="userdata.txt"

#!/bin/bash
sudo ufw disable
--//
  1. Start the instance

You will be able to SSH into your server now as ufw is disabled. You might want to stop the instance and remove user data and start it again.

Scrawl answered 3/10, 2017 at 14:3 Comment(3)
You're a lifesaver!Bohi
Nice method AwesomePicturize
This is great. Works perfectly, and is easy to do it.Fatso
H
0

At this point, there is nothing you can do to get back into the machine. It seems (from your comments) that ufw automatically starts and you have not added a rule to it to allow SSH traffic.

What you can do to recover data is

  • shut down the EC2 instance,
  • detach the EBS volume,
  • launch a different EC2 instance,
  • log into it,
  • mount the EBS volume and
  • copy the data to the new instance
Hooper answered 12/8, 2016 at 8:20 Comment(0)
T
0

Simple step to step working Solution:

This error comes when you allow ufw and enable it on ec2.

  1. Stop the Instance

  2. Go to Action > Instance Setting > Edit User Data

And make sure to choose As Text in Edit User Data Screen

  1. Add below commands
#!/bin/bash
sudo ufw allow ssh
sudo ufw allow 22
sudo ufw allow 443
sudo ufw allow 8080
sudo ufw allow 80
sudo ufw status 
sudo ufw enable
  1. Now start the Instance And check that it is on Running State

  2. check your public IP (This has been changed after restart)

  3. Now finally connect to the instance using ssh : ssh -i <username>@<ip-address>

Tholos answered 22/4, 2022 at 9:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.