V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Distributions
Ubuntu
Fedora
CentOS
中文资源站
网易开源镜像站
hyuhui
V2EX  ›  Linux

遇到一个神奇的 iptables 问题,求解!

  •  
  •   hyuhui · 2021-11-15 18:35:13 +08:00 · 3426 次点击
    这是一个创建于 864 天前的主题,其中的信息可能已经有所发展或是发生改变。

    背景

    1 、服务器为亚马逊的 aws lightsail ,今天新建的 2 、系统是 CentOS Linux release 7.9.2009 (Core) 3 、内核是 5.12.19-1.el7 4 、已安装 iptables-services ,iptables 的状态为运行中

    问题

    添加规则,service iptables save 保存,iptables -L 能看到规则,测试规则生效。

    但是,只要断开 ssh ,或者 reboot 服务器,重新 ssh 进去,iptables -L 看到规则清零了,测试规则失效了。vi /etc/sysconfig/iptables 看到规则是在里面的,service iptables restart 后规则重新生效,iptables -L 也能重新看到规则了。

    新建了几个实例也还是这样!我之前建的实例却不会出现这个情况。

    哪位大神知道这是为什么?

    第 1 条附言  ·  2021-11-16 13:13:06 +08:00
    今天尝试了一下,昨天建的实例,问题依旧,但是今天新建的实例已经没有问题了,估计是 AWS 自己的问题😓

    感谢各位的热心回复!🙏
    27 条回复    2021-11-17 17:00:35 +08:00
    defunct9
        1
    defunct9  
       2021-11-15 18:48:55 +08:00   ❤️ 3
    开 ssh ,让我上去看看
    harde
        2
    harde  
       2021-11-15 18:53:35 +08:00
    +1
    betteryjs
        3
    betteryjs  
       2021-11-15 18:53:39 +08:00
    加到 rc.local 试试?
    zhs227
        4
    zhs227  
       2021-11-15 18:55:15 +08:00
    没禁用 firewalld?
    hyuhui
        5
    hyuhui  
    OP
       2021-11-15 18:58:21 +08:00
    @zhs227 firewalld 已禁用

    systemctl stop firewalld.service

    systemctl disable firewalld.service
    zhangsanfeng2012
        6
    zhangsanfeng2012  
       2021-11-15 19:34:17 +08:00
    systemctl status iptables 是什么状态
    hyuhui
        7
    hyuhui  
    OP
       2021-11-15 19:56:04 +08:00
    @zhangsanfeng2012

    ● iptables.service - IPv4 firewall with iptables
    Loaded: loaded (/usr/lib/systemd/system/iptables.service; enabled; vendor preset: disabled)
    Active: active (exited) since Mon 2021-11-15 19:09:08 CST; 44min ago
    Process: 15421 ExecStop=/usr/libexec/iptables/iptables.init stop (code=exited, status=0/SUCCESS)
    Process: 15450 ExecStart=/usr/libexec/iptables/iptables.init start (code=exited, status=0/SUCCESS)
    Main PID: 15450 (code=exited, status=0/SUCCESS)

    Nov 15 19:09:08 ip.ap-northeast-1.compute.internal systemd[1]: Stopped IPv4 firewall...
    Nov 15 19:09:08 ip.ap-northeast-1.compute.internal systemd[1]: Starting IPv4 firewal...
    Nov 15 19:09:08 ip.ap-northeast-1.compute.internal iptables.init[15450]: iptables: A...
    Nov 15 19:09:08 ip.ap-northeast-1.compute.internal systemd[1]: Started IPv4 firewall...
    Hint: Some lines were ellipsized, use -l to show in full.
    ik
        8
    ik  
       2021-11-15 20:09:42 +08:00 via iPhone
    来两个终端,一个终端配了,另外一个终端能看到吗?
    hyuhui
        9
    hyuhui  
    OP
       2021-11-15 20:19:48 +08:00
    @ik 另一个终端,iptables -L 看不到,vi /etc/sysconfig/iptables 可以看到
    ik
        10
    ik  
       2021-11-15 20:33:39 +08:00 via iPhone
    @hyuhui 神奇我也没遇到过😢
    adoal
        11
    adoal  
       2021-11-15 20:45:00 +08:00 via iPhone
    看看是不是 shell 的某个 rc 里 flush 了
    nijux
        12
    nijux  
       2021-11-15 21:08:37 +08:00
    aws lightsail 防火墙是不是要在网页后台设置啊,不用在服务器上设置
    onetown
        13
    onetown  
       2021-11-15 22:22:55 +08:00
    iptables-persistent 服务
    另外 aws 上的安全组不是挺好用的吗
    ihipop
        15
    ihipop  
       2021-11-15 22:29:38 +08:00 via Android
    centos7 哪来的原装 5.x 内核
    yanqiyu
        16
    yanqiyu  
       2021-11-15 23:08:06 +08:00
    iptables 不会平白无故消失,所以肯定是什么东西给他 flush 掉了,

    加上 #9 说道“另一个终端,iptables -L 看不到”
    要不试一试两种情况,一种是先开两个终端,一个设置 iptables ,一个随后 iptables -L 看;另一种是开一个设置 iptables ,然后开第二个 iptables -L 看。后者就说明登陆过程中有什么东西给它 flush 掉了,前者说明更玄学的事情发生了(无意间设置了 namespace 之类的)

    既然“新建了几个实例也还是这样”并且这个问题没有被大规模报告。能不能细节的讲一下你在“新建了几个实例”之后进行的所有操作?
    Buges
        17
    Buges  
       2021-11-15 23:39:15 +08:00
    你既然是 systemd 的系统,为啥要用 service 呢。

    systemctl show iptables.service 输出里面找到 iptables-restore 命令的参数就是保存持久化 iptables 规则的地方。

    把你的 iptables 规则写到这个文件里,或者用 iptables-save 的输出保存到这个文件,然后 systemctl enable --now iptables.service;systemctl reload iptables.service 就可以了。
    TsukiMori
        18
    TsukiMori  
       2021-11-16 02:15:40 +08:00 via Android
    lightsail 不直接在 web 管理配置防火墙就可以嘛
    holinhot
        19
    holinhot  
       2021-11-16 03:56:03 +08:00
    AWS 自带防火墙网页管理。没有 VNC ,难道系统模版强刷了 iptables 防止你被锁在外面。
    hyuhui
        20
    hyuhui  
    OP
       2021-11-16 09:19:09 +08:00
    @yanqiyu 一种是先开两个终端,一个设置 iptables ,一个随后 iptables -L 看:这个通过 iptables -L 可以看到!

    报告给亚马逊了,还没有回复。

    试过 centos7 、centos8 、debian10 ,新建实例后只安装 iptables-services ( centos )或者 iptables-persistent ( debian ),然后也会出现这个情况。
    julyclyde
        21
    julyclyde  
       2021-11-16 11:00:09 +08:00
    @Buges redhat 系用 service 没啥毛病。systemd 时代的 service 和 sysvinit 时代的 service 并不是同样内容的脚本;再说 iptables 也不是个正经 systemd service
    yanqiyu
        22
    yanqiyu  
       2021-11-16 11:37:18 +08:00   ❤️ 1
    @hyuhui 那就说明登陆过程有啥东西 flush 了 iptables
    要不试一试 https://unix.stackexchange.com/questions/206891/audit-on-changes-to-the-running-iptables-configuration 里面的方法设置 audit 规则追踪一下
    snuglove
        23
    snuglove  
       2021-11-16 11:41:58 +08:00
    我都是使用 iptables-save >/etc/sysconfig/iptables 来保存,保存完了在重启 🤣🤣
    hyuhui
        24
    hyuhui  
    OP
       2021-11-16 13:03:15 +08:00
    报告各位

    今天尝试了一下,昨天建的实例,问题依旧,但是今天新建的实例已经没有问题了,估计是 AWS 自己的问题😓
    Buges
        25
    Buges  
       2021-11-16 14:01:38 +08:00 via Android
    @julyclyde systemd 虽然有 sysvcompat 兼容以前的脚本,但没理由去用它啊,没有 /etc/init.d ,service 的啥行为都不知道。在我这里 iptables.service 是个正经的 oneshot systemd service ,rh 系统上还有啥区别吗?
    julyclyde
        26
    julyclyde  
       2021-11-17 12:48:35 +08:00
    @Buges centos8 里是 oneshot 吧。centos7 我记得应该是 sysv service
    SkyHive
        27
    SkyHive  
       2021-11-17 17:00:35 +08:00
    看看 bashrc 或者 /etc/profile 里有没有奇怪的东西 flush 你得 iptables
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   980 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 20:30 · PVG 04:30 · LAX 13:30 · JFK 16:30
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.