在 EC2 AWS 中使用 UFW 将自己锁定在 SSH 之外 [英] Locked myself out of SSH with UFW in EC2 AWS
问题描述
我有一个带有 Ubuntu 的 EC2 实例.我使用了 sudo ufw enable
之后只允许 mongodb 端口
I have an EC2 Instance with Ubuntu.
I used sudo ufw enable
and after only allow the mongodb port
sudo ufw allow 27017
ssh 连接断开后,我无法重新连接
When the ssh connection broke, I can´t reconnect
推荐答案
#更新
最简单的方法是更新实例的用户数据
Easiest way is to update the instance's user data
停止您的实例
Stop your instance
右键单击 (windows) 或 ctrl + 单击 (Mac) 以打开上下文菜单,然后转到 Instance Settings
->Edit User Data
或选择实例并转到Actions
->实例设置
->编辑用户数据
Right click (windows) or ctrl + click (Mac) on the instance to open context menu, then go to Instance Settings
-> Edit User Data
or select the instance and go to Actions
-> Instance Settings
-> Edit User Data
如果您仍在使用旧的 AWS 控制台,请选择实例,转到 Actions
->实例设置
->查看/更改用户数据
If you're still on the old AWS console, select the instance, go to Actions
-> Instance Settings
-> View/Change User Data
并粘贴这个
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, always]
--//
Content-Type: text/x-shellscript; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="userdata.txt"
#!/bin/bash
ufw disable
iptables -L
iptables -F
--//
- 添加后,重新启动实例,ssh 应该可以工作了.如果启用,用户数据会禁用 ufw,并且还会刷新任何阻止 ssh 访问的 iptable 规则
启动一个新实例(恢复实例).
Launch a new instance (recovery instance).
来源 这里
# 旧答案
使用另一个实例分离并修复问题实例的体积
Detach and fix the volume of the problem instance using another instance
停止原始实例(不要终止)
Stop the original instance (DO NOT TERMINATE)
从原始实例中分离卷(问题卷)
Detach the volume (problem volume) from the original instance
将其作为/dev/sdf 附加到恢复实例.
Attached it to the recovery instance as /dev/sdf.
通过 ssh/putty 登录恢复实例
Login to the recovery instance via ssh/putty
运行 sudo lsblk
以显示附加卷并确认问题卷的名称.它通常以 /dev/xvdf
开头.我的是 /dev/xvdf1
Run sudo lsblk
to display attached volumes and confirm the name of the problem volume. It usually begins with /dev/xvdf
. Mine is /dev/xvdf1
安装问题卷.
$ sudo mount /dev/xvdf1 /mnt
$ cd /mnt/etc/ufw
打开ufw
配置文件
$ sudo vim ufw.conf
按 i 编辑文件.
Press i to edit the file.
将 ENABLED=yes
更改为 ENABLED=no
键入 Ctrl-C 并键入 :wq 以保存文件.
Type Ctrl-C and type :wq to save the file.
使用以下命令显示 ufw conf 文件的内容,并确保 ENABLED=yes
已更改为 ENABLED=no
Display content of ufw conf file using the command below and ensure that ENABLED=yes
has been changed to ENABLED=no
$ sudo cat ufw.conf
卸载卷
Unmount volume
$ cd ~
$ sudo umount /mnt
从恢复实例中分离问题卷,然后将其作为/dev/sda1 重新附加到原始实例.
Detach problem volume from recovery instance and re-attach it to the original instance as /dev/sda1.
启动原始实例,您应该可以重新登录.
Start the original instance and you should be able to log back in.
来源:这里
这篇关于在 EC2 AWS 中使用 UFW 将自己锁定在 SSH 之外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!