【问题标题】:Can't add rules to iptables, nothing gets committed无法向 iptables 添加规则,没有提交任何内容
【发布时间】:2018-01-16 04:04:12
【问题描述】:

根据documentation:rules,执行以下操作应该会在 iptables 规则列表中添加一个简单的规则:

rule = iptc.Rule()
rule.src = "127.0.0.1"
rule.protocol = "udp"
rule.target = rule.create_target("ACCEPT")
match = rule.create_match("comment")
match.comment = "this is a test comment"
chain = iptc.Chain(iptc.Table(iptc.Table.FILTER), "INPUT")
chain.insert_rule(rule)

但是,运行此示例会产生绝对为零的新规则。
我正在通过以下方式验证这一点:

iptables -L --line-number

在我提交错误问题之前,我想知道是否有其他人遇到过这个问题,如果遇到过,您是如何解决的。

为了安全起见,我以 root 身份运行所有内容,我还尝试通过运行文档同一部分的另一个示例代码来验证规则:

table = iptc.Table(iptc.Table.FILTER)
for chain in table.chains:
    print ("=======================")
    print ("Chain ", chain.name)
    for rule in chain.rules:
        print ("Rule", "proto:", rule.protocol, "src:", rule.src, "dst:", \
                      rule.dst, "in:", rule.in_interface, "out:", rule.out_interface,)
        print ("Matches:")
        for match in rule.matches:
            print (match.name)
        print ("Target:"),
        print (rule.target.name)
print ("=======================")

(稍作修改以使用 Python3)。

这是为了确保自动提交没有问题,但结果仍然相同。 我还要指出它确实工作了一小段时间,大约为 iptables 添加了 3 个功能。做一个systemctl restart iptables 可能会起作用,但如果可能的话,我想 - 在我做经典的旧“Windows 技巧”重新启动东西之前弄清楚为什么会出错。 (在 journald/systemd 中没有提到任何关于 iptables 的内容)

【问题讨论】:

  • 我无法重现您的问题。调用chain.insert_rule(rule) 后,我立即在filter 表的INPUT 链中看到了新规则。
  • 感谢@larsks 提供这些信息。我怀疑这对我来说真的是本地的。

标签: python linux python-3.x iptables python-iptables


【解决方案1】:

看到@larsks 无法重现我进一步挖掘的问题。 似乎已执行系统更新(经典错误,我深表歉意)。

这会导致加载的内核版本与 iptables 的内核模块不同,有一些修复可以使用 iptables 命令解决此问题,以便您仍然可以添加规则。

但是,使用 lib python-iptables 不起作用。
实际差异超出了我的范围,我挖了一点,但找不到会导致问题的地方。

不幸的是,在这种情况下重启机器是唯一(据我所知)解决此问题的方法。这是为了使加载的内核模块和安装的工具与它们正在使用的版本相匹配。

(另一种解决方案是保留旧的iptables 命令和库,这意味着备份它们并将库指向备份的版本,直到可以重新启动)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-26
    • 1970-01-01
    • 2020-06-12
    • 2018-03-29
    • 2018-10-09
    • 1970-01-01
    相关资源
    最近更新 更多