iptables多个源IP

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

这是唯一可能的,如果你可以聚合你想要的源IP到一个连续的范围。 例如

iptables -A INPUT -s 192.168.0.0/24 -d 192.168.0.5 -p tcp -j ACCEPT 

如果你找不到一个覆盖你想要的IP的通用networking掩码,你将不得不写几个相同的规则来做你想做的事情。

有几个iptables框架可以处理低水平的iptables规则,使您可以在更具同构性的层次上定义规则。 Shorewall是目前大多数Linux发行版中常见的一种。

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

 iptables -t filter -A INPUT -s 192.168.1.1,2.2.2.2,10.10.10.10 -j ACCEPT 

iptables会自动将其转换成多个规则

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

 -A INPUT -i eth0 -m iprange --src-range 192.168.1.90-192.168.1.101 -j ACCEPT 

来源: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. 

(我知道这就像一个4岁的问题,但只是为了答复谁在网上寻找这个问题)

您可以定义多个链,以便可以组合独立的需求列表。 我怀疑这正是你想要的,但它仍然非常方便。 我们用这个来定义IP地址的有效用户types列表,然后将端口限制应用到源networking。 所以,例如:

 # Allow SMTP from anywhere -A tcp_inbound -p tcp -m tcp -s 0/0 --dport 25 -j allowed # # Define the set of IP ranges we'll send to the tcp_user_inbound chain -A tcp_inbound -p tcp -m tcp -s 172.19.1.0/24 -j tcp_user_inbound -A tcp_inbound -p tcp -m tcp -s 172.19.6.0/23 -j tcp_user_inbound -A tcp_inbound -p tcp -m tcp -s 172.19.8.0/24 -j tcp_user_inbound -A tcp_inbound -p tcp -m tcp -s 172.19.10.0/23 -j tcp_user_inbound -A tcp_inbound -p tcp -m tcp -s 172.19.12.0/23 -j tcp_user_inbound -A tcp_inbound -p tcp -m tcp -s 172.19.4.0/23 -j tcp_user_inbound # # Ports we allow access to based on a source-address prereq. # SSH -A tcp_user_inbound -p tcp -m tcp --dport 22 -j allowed # VNC -A tcp_user_inbound -p tcp -m tcp --dport 5950:5958 -j allowed # https -A tcp_user_inbound -p tcp -m tcp --dport 443 -j allowed 

除了BòssKing的评论之外,您还可以简单地指定几个用逗号分隔的地址:

 [!] -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). 

原来的问题是从2009年5月份开始,但自2011年5月以来,Linux内核已经有了一个function来解决这个需求,称为ipset

以下是创build一个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 

请参阅man iptablesman ipset了解更多信息。

比方说,您只想接受来自10.0.0.2或192.168.1.2的SMTP数据包。 您可以使用以下规则:

  # create a new chain iptables --new-chain multiple_sources_smtp # send all SMTP connections to the new chain iptables --append INPUT --protocol tcp --dport 25 --jump multiple_sources_smtp # use the default INPUT rules for packets coming from allowed sources iptables --append multiple_sources_smtp --source 10.0.0.2 --jump RETURN iptables --append multiple_sources_smtp --source 192.168.1.2 --jump RETURN # drop packets from anywhere else iptables --append multiple_sources_smtp -j DROP 

或者作为iptables-save的输出

  # Generated by iptables-save v1.4.14 on Sat Dec 6 09:17:11 2014 *filter :INPUT ACCEPT [32:13325] :FORWARD ACCEPT [0:0] :OUTPUT ACCEPT [25:3084] :multiple_sources_smtp - [0:0] -A INPUT -p tcp -m tcp --dport 25 -j multiple_sources_smtp -A multiple_sources_smtp -s 10.0.0.2/32 -j RETURN -A multiple_sources_smtp -s 192.168.1.2/32 -j RETURN -A multiple_sources_smtp -j DROP COMMIT # Completed on Sat Dec 6 09:17:11 2014