温馨提示×

debian iptables如何限流速

小樊
56
2026-10-01 18:10:25
栏目: 智能运维

在 Debian 下使用 iptables 限制网络流速(带宽),通常不是由 iptables 自己直接“限速”,而是借助 iptables + tc(traffic control) 或 iptables limit/hashlimit 模块来实现。下面按常见场景说明。


一、用 iptables + tc 做“真正限速”(推荐)

这是最常用、最精确的方式。

1. 安装工具

apt update
apt install iproute2

2. 对出口流量限速(例如限制到 1Mbit)

假设网卡是 eth0:

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

说明:

  • rate 1mbit:平均速率
  • burst:突发流量
  • latency:最大延迟

3. 只限制某个 IP(结合 iptables 打标记)

iptables -A OUTPUT -t mangle -d 192.168.1.100 -j MARK --set-mark 10

然后用 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 protocol ip parent 1: prio 1 handle 10 fw flowid 1:10

二、用 iptables limit 模块(限制“包速率”)

适合限制连接频率,不是严格带宽限速。

限制每秒最多 10 个包

iptables -A INPUT -p tcp --dport 80 -m limit --limit 10/s -j ACCEPT
iptables -A INPUT -p tcp --dport 80 -j DROP

限制每秒 5 个新建连接

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

三、用 hashlimit(按 IP 限速连接数)

每个 IP 每秒最多 3 个新连接

iptables -A INPUT -p tcp --dport 80 -m hashlimit \
  --hashlimit-mode srcip \
  --hashlimit-above 3/s \
  --hashlimit-name http_limit \
  -j DROP

四、清理规则

iptables -F
tc qdisc del dev eth0 root

总结建议

需求 推荐方案
精确带宽限速 iptables + tc (HTB/TBF)
防 CC / 刷接口 hashlimit
简单包频率限制 limit

如果你能说明:

  • 限制 入口还是出口
  • 限制 整个机器还是某个 IP / 端口
  • 目标速率(如 10Mbit)

我可以给你一条可直接用的完整命令。

0 踩