CentOS 6 Iptables关于--hitcount数值限制的问题

一直知道iptables 很强大,但是所知甚少,今天突然想做下iptables 限制连接数的问题,参照iptables man手册 recent 部分,在iptables 文件中写了两条规则,命令行写法如下:

    iptables -A INPUT -p tcp --dport 80 -m recent --name badguy --seconds 60 --update --hitcount 100 -j DROP
    iptables -A INPUT -p tcp --dport 80 -m recent --name badguy --set -j ACCEPT

重启iptables,但是奇妙的事情发生了:
iptables: Setting chains to policy ACCEPT: filter [ OK ]
iptables: Flushing firewall rules: [ OK ]
iptables: Unloading modules: [ OK ]
iptables: Applying firewall rules: iptables-restore: line 17 failed
立刻去查看iptables 文件,发现第17行的内容为 COMMIT!一定是规则有问题,把规则注释掉,再重启,OK了!然后用各种姿势google,无解。查到网友的写法跟我的写法一样一样的,正常工作!WTF的节奏!
继续调整规则,发现把–hitcount 100 去掉后就OK,加上就failed,回头继续看man 手册,关于–hitcount 的描述如下:
This option must be used in conjunction with one of –rcheck or –update. When used, this will narrow the match to only happen when the address is in the list and packets had been received greater han or equal to the given value. This option may be used along with –seconds to create an even narrower match requiring a certain number of hits within a specific time frame. * The maximum value for the hitcount parameter is given by the “ip_pkt_list_tot” parameter of the xt_recent kernel module.Exceeding this value on the command line will cause the rule to be rejected.
粗体这句解释了问题,然后尝试将–hitcount 调低,调到10正常,20正常,大于20,failed 了,好了,问题就在这了,要突破ip_pkt_list_tot 的默认限制了。
google了下ip_pkt_list_tot,发现在/sys/module/xt_recent/parameters目录下,cat 了下ip_pkt_list_tot的值,还真是20,按照以往情况,直接echo 一个值进入就可以了,但是“白手起家”这位网友说不可,要进入到/etc/modprobe.d/ 目录下新建配置文件:
vim xt_recent.conf
options xt_recent ip_list_tot=1024 ip_pkt_list_tot=200
备注:(原文的文件名中没有.conf,试验后有警告提示:WARNING: All config files need .conf: /etc/modprobe.d/xt_recent, it will be ignored in a future release. 因此正确的写法应该加上.conf)
保存退出,调大–hitcount 的值为200,重启iptables ,OK!
再去cat ip_pkt_list_tot 的值,也变成了200。

搞定,收工!

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wooowlili

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值