在这个问题中,我看到一个这样的行,可以让我说“允许这些IP地址连接”
iptables -A INPUT -m iprange --src-range 10.50.10.20-80 -j ACCEPT
现在,我想进一步确保这个规则只适用于特定的端口。 我一直在使用像这样的命令我的常规端口:
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
我可以把这两个结合起来,使一个特定的端口只允许一个范围,就像这样
iptables -A INPUT -m iprange --src-range 10.50.10.20-80 --dport 12345 -j ACCEPT
显然,我很犹豫,只是使iptables的电话。 :) 谢谢!
你在那里的最后一行应该可以工作,你只需要确保你有一个-p协议,因为–dport不能自己作为一个选项工作。
iptables -A INPUT -m iprange --src-range 10.50.10.20-80 -p tcp --dport 12345 -j ACCEPT
或者,安装ipset ,你将能够改变IP地址列表而不会搞乱你的iptables规则:
ipset -N AllowedSources ipmap --network 10.50.10.0/24 for i in $LIST_OF_ALLOWED_SOURCES; do ipset -A AllowedSources $i; done iptables -A INPUT -m set --match-set AllowedSources src -p tcp --dport 12345 -j ACCEPT
现在,如果您需要添加其他允许的来源:
ipset -A AllowedSources abcd
或者,您需要从允许的来源“删除”主机:
ipset -D AllowedSources efgh
你可以保存你的设置:
ipset --save > /etc/ipset.conf
你可以在启动过程中恢复你的iptables (否则,iptables会报错!):
ipset --restore < /etc/ipset.conf
你甚至可以创build一个IP地址来匹配源IP 和目的端口,例如:
ipset -N AllowedAccess ipporthash --network 10.50.0.0/16 # These hosts may access port 12345 for i in $LIST_OF_ALLOWED_TO_12345; do ipset -A AllowedAccess $i,12345; done # These hosts may access port 23456 for i in $LIST_OF_ALLOWED_TO_23456; do ipset -A AllowedAccess $i,23456; done # These hosts may access port 34567 for i in $LIST_OF_ALLOWED_TO_34567; do ipset -A AllowedAccess $i,34567; done # Now that the IP set has been created, we can use it in iptables iptables -A INPUT -m set --match-set AllowedAccess src,dst -j ACCEPT # Note that we use "src,dst", meaning that we want to match source IP, but # destination port # Also note, if you need to match against a single port, the ipmap method # will be slightly faster.
更多关于ipset : http : //ipset.netfilter.org/
如果您使用Ubuntu ,则无法从其repo安装ipset软件包。 使用我的提示: http : //pepoluan.posterous.com/powertip-howto-install-ipset-on-ubuntu
你有正确的基本想法,你可以把它们合并成一个这样的规则。
然而,尽pipe有些答案说,你不应该使用像10.50.10.20-80这样的范围(它将扩展到10.50.10.20-80.0.0.0 – 使用iptables命令来检查)。 您需要使用范围内的完整IP地址,例如10.50.10.20-10.50.10.80。
另外,如果你指定一个端口号,你需要声明一个支持端口的协议,所以修改后的规则是:
iptables -A INPUT -p tcp -m iprange --src-range 10.50.10.20-10.50.10.80 --dport 12345 -j ACCEPT
关于iprange的文件: https : //www.frozentux.net/iptables-tutorial/chunkyhtml/x2702.html#TABLE.IPRANGEMATCH