温馨提示×

debian iptables怎样限速

小樊
43
2026-10-10 14:26:16
栏目: 智能运维

在 Debian 下使用 iptables 限速,通常要结合 tc(Traffic Control) 或使用 iptables 的 limit / hashlimit 模块。下面按常见场景说明。


一、使用 iptables + limit(简单限速,适合限制连接/包速率)

1️⃣ 限制 ICMP(ping)速率(防 ping 洪水)

iptables -A INPUT -p icmp --icmp-type echo-request \
  -m limit --limit 1/s --limit-burst 5 \
  -j ACCEPT

iptables -A INPUT -p icmp --icmp-type echo-request -j DROP

说明:

  • --limit 1/s:每秒最多 1 个包
  • --limit-burst 5:突发最多 5 个

2️⃣ 限制 SSH 登录频率(防暴力破解)

iptables -A INPUT -p tcp --dport 22 \
  -m conntrack --ctstate NEW \
  -m limit --limit 3/min --limit-burst 3 \
  -j ACCEPT

iptables -A INPUT -p tcp --dport 22 -j DROP

二、使用 iptables hashlimit(按 IP 限速,更实用)

限制每个 IP 的 HTTP 请求速率

iptables -A INPUT -p tcp --dport 80 \
  -m hashlimit \
  --hashlimit-name http_limit \
  --hashlimit-mode srcip \
  --hashlimit-above 10/min \
  --hashlimit-burst 5 \
  -j DROP

参数说明:

  • srcip:按源 IP 限速
  • 10/min:每分钟最多 10 个新连接
  • burst:突发允许数量

✅ 推荐用于 单 IP 限速


三、真正“带宽限速”(如 1Mbps)——必须用 tc

iptables 不能直接限制带宽大小,只能限制包/连接速率。

示例:限制 eth0 出方向为 1Mbit

tc qdisc add dev eth0 root tbf rate 1mbit burst 32kbit latency 400ms

删除限速

tc qdisc del dev eth0 root

四、iptables + tc 组合(推荐做法)

  1. iptables 做 标记
iptables -A OUTPUT -t mangle -p tcp --dport 80 -j MARK --set-mark 10
  1. tc 按 mark 限速
tc qdisc add dev eth0 root handle 1: htb
tc class add dev eth0 parent 1: classid 1:10 htb rate 1mbit
tc filter add dev eth0 parent 1: protocol ip handle 10 fw classid 1:10

五、推荐方案总结

需求 推荐方式
防 ping / 防爆破 iptables limit
单 IP 限速 iptables hashlimit
带宽限制 tc
精细控制 iptables + tc

如果你能说明:

  • 限速对象(IP / 端口 / 协议)
  • 入方向还是出方向
  • 目标速率(如 10Mbps / 1000 包/s)

我可以给你 直接可用的完整规则。

0 踩