首页 > 解决方案 > 如果我使用 ufw 关闭端口 22,如何连接到 ec2?

问题描述

我无法连接到我在 ec2 上的实例。一切都很好,我认为现在ufw有问题。

当我配置我的服务器时,我为 80 端口启用了 ufw,也许它禁用了 22 端口。因为早期一切都很好,但现在我无法通过 ssh 连接。

如何连接到我的 ec2 实例并再次允许 22 端口?

标签: amazon-ec2sshufw

解决方案


您可以使用以下最简单的方法(用户数据)关闭 ufw,然后访问实例并编辑防火墙。

  1. 停止实例
  2. 编辑用户数据以禁用 ufw 一次并让您访问实例
  3. 启动实例

以下是用户数据:

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
--//

在此处查看我的答案 在禁用 ec2 服务器后启用防火墙端口 22


推荐阅读