iptables 中的源 IP 速率限制:hashlimit 与最近的

Rap*_*sey 9 linux iptables

我想在 iptables 中对每个源 IP 执行速率限制。例如,将主机建立新 SSH 连接的速率限制为每分钟 5 个。据我所知,有两种方法可以做到这一点:

使用 hashlimit 模块

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
Run Code Online (Sandbox Code Playgroud)

随着最近的模块

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
Run Code Online (Sandbox Code Playgroud)

我的问题:

  • 这两者的行为方式有什么不同吗?
  • 强调性能,哪个更可取?
  • 使用这两个模块是否有明显的缺点?

use*_*571 4

Is there any difference in how these two will behave?
Run Code Online (Sandbox Code Playgroud)

不,你写的东西在功能上会做同样的事情。

With an emphasis on performance, which one is preferable?
Run Code Online (Sandbox Code Playgroud)

可以说,recent 具有更好的性能,因为它维护一个表但不使用哈希桶。

Is there a significant downside to using both modules?
Run Code Online (Sandbox Code Playgroud)

我不确定你为什么要同时使用两者。当您只需要一个模块时,使用这两个模块将会对性能产生影响。