【问题标题】:List of loaded iptables modules已加载的 iptables 模块列表
【发布时间】:2011-01-02 16:10:56
【问题描述】:

有没有什么方便的方法可以显示加载的 iptables 模块列表?我可以通过列出/lib/iptables/(或/lib64/iptables/)目录来显示已安装的模块,但我需要活动模块列表。

【问题讨论】:

    标签: linux networking iptables


    【解决方案1】:

    加载的 iptables 模块可以在 /proc/net/ip_tables_matches proc 文件系统条目中找到。

    cat /proc/net/ip_tables_matches
    

    在 PHP 中,我可以通过加载和分解文件内容来访问加载的 iptables 模块:

    $content = file_get_contents('/proc/net/ip_tables_matches');
    $modules = explode("\n", $content);
    

    当然需要挂载 proc 文件系统(大多数 GNU Linux 发行版默认挂载它)

    【讨论】:

    • 看来应该检查file_get_contents()的结果,因为它可以返回false
    【解决方案2】:

    这是一篇很老的帖子,但我们开始吧:

    # lsmod | grep ip
    

    显示加载模块的列表,我认为大多数与 iptables 相关... /proc/net/ip_tables_matches 不显示模块(至少在 RHEL 6 中不显示)

    【讨论】:

      【解决方案3】:

      查看以下目录(根据您的内核版本替换):

      ls /lib/modules/2.6.32-504.8.1.el6.x86_64/kernel/net/netfilter/
      

      您可以使用(删除目录中列出的.ko)加载模块:

      modprobe nf_conntrack_ftp
      

      或者,您可以通过将其添加到以下位置来确保它在启动时加载:

      /etc/sysconfig/iptables-config (RHEL/CENTOS) 
      
      IPTABLES_MODULES="nf_conntrack_ftp"
      

      这似乎没有很好的记录。

      【讨论】:

      • 我非常感谢关于如何在启动时加载它的说明。哇哦。花了太多时间搜索这个。
      【解决方案4】:

      正如 Gonio 建议的 lsmod 列出所有已加载的内核模块,但 grepping "ip" 不会为您提供所有 iptables 模块。

      我宁愿使用

      lsmod|grep -E "nf_|xt_|ip"
      

      不过,我不确定列表是否完整。

      【讨论】:

        【解决方案5】:

        试试这个以快速了解系统中存在的 netfilter 模块,这里是用于粘贴的单行:

        for i in /lib/modules/$(uname -r)/kernel/net/netfilter/*; do echo -e "\e[33;1m$(basename "$i")\e[0m"; strings "$i" | \grep -e description -e depends| sed -e 's/Xtables: //g' -e 's/=/: /g' -e 's/depends=/depends on: /g'; echo; done
        

        再次为了可读性,添加了换行符:

        #!/bin/bash
        for i in /lib/modules/$(uname -r)/kernel/net/netfilter/*
        do 
            echo -e "\e[33;1m$(basename "$i")\e[0m"
            strings "$i" | \grep -e description -e depends | sed -e 's/Xtables: //g' -e 's/=/: /g' -e 's/depends=/depends on: /g'
            echo
        done
        

        文件名将显示为黄色,您可以从中猜测相关模块是否存在。描述和依赖关系是下面接下来的两行。

        这不会涵盖所有内容(因为这太容易了,ofc)。只有手动查找模块,看看它们是否存在,才能为您提供 100% 准确的信息。

        iptables -m <match/module name> --help
        

        如果您的系统中存在模块,在帮助文本的末尾您将获得一些有关如何使用它的信息:

        ctr-014# iptables -m limit --help
        iptables v1.4.14
        
        Usage: iptables -[ACD] chain rule-specification [options]
               iptables -I chain [rulenum] rule-specification [options]
          
        
        ...
        
        
        [!] --version   -V              print package version.
        
        limit match options:
        --limit avg                     max average match rate: default 3/hour
                                        [Packets per second unless followed by 
                                        /sec /minute /hour /day postfixes]
        --limit-burst number            number to match in a burst, default 5
        ctr-014# 
        

        如果您的系统上不存在该模块:

        ctr-014# iptables -m iplimit --help
        iptables v1.4.14: Couldn't load match `iplimit':No such file or directory
        
        Try `iptables -h' or 'iptables --help' for more information.
        ctr-014#
        

        【讨论】:

          【解决方案6】:

          作为一种替代方法,这也可以使用 Python 脚本来完成。

          首先确保您拥有 iptc 库。 sudo pip install --upgrade python-iptables

          (假设 Python3 是你的版本)

          import iptc
          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("------------------------------------------")
          

          【讨论】:

            猜你喜欢
            • 2023-03-25
            • 1970-01-01
            • 2023-04-02
            • 2012-04-05
            • 2016-11-05
            • 2018-10-18
            • 2017-02-24
            • 2014-03-22
            • 1970-01-01
            相关资源
            最近更新 更多