在 Ubuntu 上,可以使用 iptables 配合 流量控制(tc / htb) 或 iptables limit / quota 模块 来限制流量。下面按常见需求分类说明。
iptables -A INPUT -p tcp --dport 80 -m limit --limit 10/s --limit-burst 20 -j ACCEPT
iptables -A INPUT -p tcp --dport 80 -j DROP
说明:
--limit 10/s:每秒最多 10 个包--limit-burst 20:突发最多 20 个⚠️ 这只是“速率限制”,不是带宽限制。
iptables -A INPUT -s 192.168.1.100 -m quota --quota 104857600 -j ACCEPT
iptables -A INPUT -s 192.168.1.100 -j DROP
说明:
查看剩余:
iptables -L -v -n
⚠️ 重启 iptables 后配额会清零。
iptables 本身不能直接限速带宽,需要配合 tc(Traffic Control)。
tc qdisc add dev eth0 root handle 1: htb
tc class add dev eth0 parent 1: classid 1:1 htb rate 1mbit
iptables -t mangle -A POSTROUTING -s 192.168.1.100 -j MARK --set-mark 10
tc filter add dev eth0 protocol ip parent 1:0 handle 10 fw flowid 1:1
modprobe ifb
ip link set ifb0 up
tc qdisc add dev eth0 ingress
tc filter add dev eth0 parent ffff: protocol ip u32 match ip src 192.168.1.100 flowid 1:1
apt install wondershaper
wondershaper eth0 1024 512
上行 1Mbps,下行 512Kbps
nftables(新系统推荐)
tc + htb(最专业)
| 需求 | 推荐方式 |
|---|---|
| 防刷接口 | iptables limit |
| 限制总流量 | iptables quota |
| 限制带宽 | tc + iptables MARK |
| 快速限速 | wondershaper |
如果你能说明:
我可以给你一条可直接用的完整命令。