大网数据广告活动
服务器运维问答

Source IP rate limiting in iptables: hashlimit vs recent

· 52CE 我爱测

I want to perform rate limiting per source IP in iptables. For example, limit the rate at which a host can establish new SSH connections to 5 per minute. To my knowledge there are two ways of doing this: With the hashlimit module…

问题原文

原帖链接 · Rapsey · CC BY-SA 3.0

I want to perform rate limiting per source IP in iptables. For example, limit the rate at which a host can establish new SSH connections to 5 per minute. To my knowledge there are two ways of doing this:

With the hashlimit module

iptables -A INPUT -p tcp --dport 22 -m state --state NEW \
  -m hashlimit --hashlimit-name SSH --hashlimit-above 5/min \
  --hashlimit-mode srcip -j REJECT
iptables -A INPUT -p tcp --dport 22 -m state --state NEW -j ACCEPT

With the recent module

iptables -A INPUT -p tcp --dport 22 -m state --state NEW -m recent \
  --rcheck --seconds 60 --hitcount 5 --name SSH --rsource -j REJECT
iptables -A INPUT -p tcp -m tcp --dport 22 -m state --state NEW \
  -m recent --set --name SSH --rsource -j ACCEPT

My questions:

  • Is there any difference in how these two will behave?
  • With an emphasis on performance, which one is preferable?
  • Is there a significant downside to using both modules?

社区高分解答

原帖链接 · user5870571 · CC BY-SA 3.0

Is there any difference in how these two will behave?

No, what you wrote will functionally do the same thing.

With an emphasis on performance, which one is preferable?

Arguably, recent has better performance because it maintains a table but does not use hash buckets.

Is there a significant downside to using both modules?

I'm not sure why you would use both. You would have the performance impact of using both modules when you only need one.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2015-04-11)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。