在单个规则中 iptables 多个源 IP

Gle*_*rry 37 iptables

我想在 iptables(如果可能)中创建一个使用多个源 IP 地址的规则。这可能吗?

小智 133

要在单个命令中添加多个源,我会这样做:

iptables -t filter -A INPUT -s 192.168.1.1,2.2.2.2,10.10.10.10 -j ACCEPT
Run Code Online (Sandbox Code Playgroud)

iptables 会自动将其翻译成多个规则

  • 尽管缺乏选票,但这有效并且是问题的正确答案 (4认同)

Tob*_*bia 18

最初的问题来自 2009 年 5 月,但自 2011 年 5 月以来,Linux 内核有了一个功能来满足这种需求,称为ipset

这是一个创建 ipset、向其添加地址,然后在防火墙规则中使用它的示例:

ipset -N office365 iphash

ipset -A office365 132.245.228.194
ipset -A office365 132.245.77.34
ipset -A office365 132.245.48.34
ipset -A office365 132.245.68.242
ipset -A office365 132.245.55.2
ipset -A office365 40.101.17.98
ipset -A office365 132.245.48.18
ipset -A office365 132.245.229.114
ipset -A office365 132.245.196.34
ipset -A office365 132.245.56.114

iptables -A OUTPUT -m set --match-set office365 dst -j ACCEPT
Run Code Online (Sandbox Code Playgroud)

查看man iptablesman ipset了解更多信息。


小智 16

您可以将 iprange 模块与“--src-range”结合使用,例如:

-A INPUT -i eth0 -m iprange --src-range 192.168.1.90-192.168.1.101 -j ACCEPT
Run Code Online (Sandbox Code Playgroud)

来源:iptables 1.4.7 手册页

   iprange
   This matches on a given arbitrary range of IP addresses.

   [!] --src-range from[-to]
          Match source IP in the specified range.

   [!] --dst-range from[-to]
          Match destination IP in the specified range.
Run Code Online (Sandbox Code Playgroud)

(我知道这就像一个 4 岁的问题,但只是为了回答任何在网上寻求这个问题的人)


Dav*_*ney 14

仅当您可以将所需的源 IP 聚合到连续范围内时,才有可能做到这一点。例如

iptables -A INPUT -s 192.168.0.0/24 -d 192.168.0.5 -p tcp -j ACCEPT
Run Code Online (Sandbox Code Playgroud)

如果找不到覆盖所需 IP 的通用网络掩码,则必须编写几个相同的规则来执行所需的操作。

有几个 iptables 框架可以处理编写 iptables 规则的低级别,允许您在更符号级别定义规则。Shorewall是一种常见的,随大多数当前的 linux 发行版一起提供。


arj*_*arj 5

除了Bòss King的注释,你也可以简单的指定几个用逗号隔开的地址:

[!] -s, --source address[/mask][,...]
      Source specification. Address can be either a network name, a hostname, a network IP address (with /mask), or a plain IP address. Hostnames will be resolved once only, before the rule is submitted to the kernel.  Please note  that  specifying
      any  name  to  be resolved with a remote query such as DNS is a really bad idea.  The mask can be either a network mask or a plain number, specifying the number of 1's at the left side of the network mask.  Thus, a mask of 24 is equivalent to
      255.255.255.0.  A "!" argument before the address specification inverts the sense of the address. The flag --src is an alias for this option.  Multiple addresses can be specified, but this will expand to multiple rules (when adding with  -A),
      or will cause multiple rules to be deleted (with -D).
Run Code Online (Sandbox Code Playgroud)