Answers:
仅当您可以将所需的源IP聚合到一个连续范围内时,才有可能。例如
iptables -A INPUT -s 192.168.0.0/24 -d 192.168.0.5 -p tcp -j ACCEPT
如果找不到覆盖所需IP的通用网络掩码,则必须编写一些相同的规则来执行所需的操作。
围绕着iptables框架,有几个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的会自动将其转化为多个规则。
-m multiport --dports 123,456,789
多个端口
iptables v1.3.7
Given命令iptables -I FORWARD -s 5.188.206.14,193.238.47.5 -j DROP
返回错误“ host/network '5.188.206.14,193.238.47.5' not found
”。
您可以将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年历史的问题,但仅供在网上寻求答案的人回答)
最初的问题是从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
请参阅man iptables
和man ipset
以获取更多信息。
除了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).
bash
,我必须使用反斜杠来避免反转:\! -s 192.168.1.3 ...
iptables v1.6.1: ! not allowed with multiple source or destination IP addresses
:-(
您可以定义多个链,以便可以合并独立的需求列表。我怀疑这正是您想要的,但是仍然很方便。我们使用它来通过IP定义有效用户类型的列表,然后将端口限制应用于源网络。因此,例如:
# 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
例如,假设您只想接受来自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