ubuntu 在EC2 AWS中使用UFW将自己锁定在SSH之外

owfi6suc  于 2022-11-02  发布在  其他
关注(0)|答案(6)|浏览(139)

我有一个Ubuntu的EC2示例。我使用了sudo ufw enable,之后只允许mongodb端口

sudo ufw allow 27017

当ssh连接中断时,我无法重新连接

ergxz8rk

ergxz8rk1#

#更新

最简单的方法是更新示例的用户数据

  • 停止示例
  • 右键单击(windows)或按住ctrl并单击(Mac)示例以打开上下文菜单,然后转到Instance Settings-〉Edit User Data或选择示例并转到Actions-〉Instance Settings-〉Edit User Data

如果您仍在旧版AWS控制台上,请选择示例,然后转到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应该可以工作了。

源代码here

#旧答案

使用另一个示例分离并修复问题示例的卷

  • 启动新示例(恢复示例)。
  • 停止原始示例(请勿终止)
  • 从原始示例分离卷(问题卷)
  • 将其作为/dev/sdf附加到恢复示例。
  • 通过ssh/putty登录到恢复示例
  • 运行sudo lsblk以显示连接的卷,并确认问题卷的名称。
  • 装入有问题的卷。
$ sudo mount /dev/xvdf1 /mnt
  $ cd /mnt/etc/ufw
  • 打开ufw配置文件
$ sudo vim ufw.conf
  • 按i键编辑文件。
  • ENABLED=yes更改为ENABLED=no
  • 键入Ctrl-C,然后键入:wq以保存文件。
  • 使用下面的命令显示ufw conf文件的内容,并确保ENABLED=yes已更改为ENABLED=no
$ sudo cat ufw.conf
  • 卸载卷
$ cd ~
  $ sudo umount /mnt
  • 从恢复示例分离问题卷,并将其作为/dev/sda 1重新附加到原始示例。
  • 启动原始示例,您应该能够重新登录。

来源:here

crcmnpdw

crcmnpdw2#

我有同样的问题,并发现这个步骤的作品:
1-停止示例
2-转到Instance Settings-〉View/Change user Data

更新:新AWS控制台UI上的路径

右键单击Stopped示例-〉Instance Settings-〉Edit User Data
3-将其粘贴到选项Modify user data as text上并保存

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

4-启动示例
希望它对你有用!

lrpiutwd

lrpiutwd3#

  • 启动另一个EC2服务器示例完成此操作的最佳方法是使用EC2的“启动更多类似的示例”功能。这将确保操作系统类型、安全组和其他属性相同,从而节省一些设置时间。
  • 停止问题示例
  • 从问题示例分离卷
  • 将卷附加到新示例

注意:较新的Linux内核可能会在内部将您的设备重命名为/dev/xvdf到/dev/xvdp,即使输入的设备名称是/dev/sdf到/dev/sdp。

  • 装载卷
cd ~
mkdir lnx1
sudo mount /dev/xvdf ./lnx1
  • 禁用UFW
cd lnx1
sudo vim ufw.conf

现在找到ENABLED=yes并将其更改为ENABLED= no。

  • 分离卷

请务必先卸载卷:

sudo umount ./lnx1/
  • 将卷重新挂接到问题示例上的/dev/sda 1
  • Boot 问题示例
  • 必要时重新分配弹性IP地址
  • 删除临时示例及其关联的卷

你好!2!3你做得很好。

b5lpy0ml

b5lpy0ml4#

其他方法对我不起作用。我的EC2示例是基于Bitnami映像的。由于市场锁定,将卷附加到另一个示例不起作用。
因此,请停止问题示例,并将此脚本粘贴到instanceSettings〉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。

nhaq1z21

nhaq1z215#

我知道这是一个老问题,但我使用bootcmd在“查看/更改用户数据”中添加了一个命令,从而修复了我的问题
我首先停止了示例
然后我在用户数据中添加了这个


# cloud-config

bootcmd:
 - cloud-init-per always fix_broken_ufw_1 sh -xc "/usr/sbin/service ufw stop >> /var/tmp/svc_$INSTANCE_ID 2>&1 || true" 
 - cloud-init-per always fix_broken_ufw_2 sh -xc "/usr/sbin/ufw disable>> /var/tmp/ufw_$INSTANCE_ID 2>&1 || true"

#注意:我的示例是Ubuntu

qcuzuvrc

qcuzuvrc6#

下面是用户数据脚本的 * 扩展版本 *:

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

set -x
USERNAME="ubuntu"
ls -Al
ls -Al /home
ls -Al /home/${USERNAME}
ls -Al /home/${USERNAME}/.ssh
sudo cat /home/${USERNAME}/.ssh/authorized_keys
ls -Al /etc/ssh
ls -ld /etc/ssh

sudo grep -vE '^$|^#' /etc/hosts.*
sudo sed -i -e 's/^\([^#].*\)/# \1/g' /etc/hosts.deny
sudo sed -i -e 's/^\([^#].*\)/# \1/g' /etc/hosts.allow
sudo grep -vE '^$|^#' /etc/hosts.*
sed '/^$\|^#/d' /etc/ssh/sshd_config

chown -v root:root /home
chmod -v 755 /home
chown -v ${USERNAME}:${USERNAME} /home/${USERNAME} -R
chmod -v 700 /home/${USERNAME}
chmod -v 700 /home/${USERNAME}/.ssh
chmod -v 600 /home/${USERNAME}/.ssh/authorized_keys

sudo tail /var/log/auth.log
sudo ufw status numbered
sudo ufw disable
sudo iptables -F
sudo service iptables stop
sudo service sshd restart
sudo service sshd status -l
--//

相关问题