Linux VPS 使用 tc 实现端口限速教程(支持动态解除限速)

详细讲解 Linux VPS 使用 tc 实现端口限速、流量控制、自动限速与解除限速的方法,支持 Xray、Hysteria、Docker 等服务。

很多时候,我们会遇到这些需求:

  • 某个节点被刷流量
  • 某个端口占满带宽
  • 想限制单个服务速度
  • 流量达到一定值后自动限速
  • 高峰期自动限速保护 VPS

Linux 自带的 tc(Traffic Control)就可以实现这些功能。

相比 wondershaper 这种整网卡限速工具,tc 更强:

  • 支持端口限速
  • 支持 IP 限速
  • 支持协议限速
  • 支持动态控制
  • 性能损耗极低

这篇文章记录一下 VPS 上常用的 tc 限速玩法。

一、什么是 tc

tc 是 Linux 内核自带的流量控制工具。

它属于:

iproute2

套件的一部分。

Debian / Ubuntu 一般默认自带。

检查:

tc -V

如果没安装:

apt update
apt install iproute2 -y

二、tc 基本原理

tc 的工作方式:

iptables 标记流量
        ↓
tc 根据 mark 分类
        ↓
限制对应流量速度

简单理解:

  • iptables 负责“识别流量”
  • tc 负责“限速”

三、限制单个端口速度

这里以:

3001 端口
限制为 5Mbps

为例。

1. 添加根队列

tc qdisc add dev eth0 root handle 1: htb default 30

说明:

  • eth0 为网卡
  • htb 为限速队列
  • 1: 为主队列 ID

2. 设置总带宽

tc class add dev eth0 parent 1: classid 1:1 htb rate 1000mbit

这里:

1000mbit

只是总带宽上限。

可以改成你 VPS 实际带宽。

3. 创建限速规则

tc class add dev eth0 parent 1:1 classid 1:10 htb rate 5mbit ceil 5mbit

说明:

  • 1:10 为限速分类
  • 5mbit 为最大速度
  • rate(保证带宽)
  • ceil(峰值带宽)

4. 使用 iptables 标记端口

ipv4标记

iptables -t mangle -A OUTPUT -p tcp --sport 3001 -j MARK --set-mark 10

ipv6标记

ip6tables -t mangle -A OUTPUT -p tcp --sport 3001 -j MARK --set-mark 10

这里:

--sport 3001

表示:

限制 VPS 发出的 3001 端口流量。

适用于:

  • 节点下载限速
  • Web 服务限速
  • 用户下行限速

设置某个端口ip连接数

如果你的 24014 是 TCP 端口,可以直接用 iptables 限制:

iptables -I INPUT -p tcp --syn --dport 24014 -m connlimit --connlimit-above 20 --connlimit-mask 32 -j REJECT

含义:

  • 24014端口
  • 每个来源 IP 最多 20 个并发连接
  • 超过立即拒绝

这是 connlimit 模块的标准用法。

按规则编号删除

先查看规则编号:

iptables -L INPUT --line-numbers -n

例如看到:

num  target     prot opt source      destination
1    REJECT     tcp  --  0.0.0.0/0   0.0.0.0/0   tcp dpt:24014 connlimit above 3 reject-with icmp-port-unreachable

删除第 1 条:

iptables -D INPUT 1

确认是否删除成功

iptables -L INPUT -n

5. tc 绑定流量标记

ipv4

tc filter add dev eth0 parent 1: protocol ip handle 10 fw flowid 1:10

ipv6

tc filter add dev eth0 parent 1: protocol ipv6 handle 10 fw flowid 1:10

到这里限速已经生效。

6. 更改限速规则

tc class change dev eth0 parent 1:1 classid 1:10 htb rate 100mbit ceil 100mbit
  • rate(保证带宽)更改为100mbit
  • ceil(峰值带宽)更改为100mbit

7. 更改iptables 标记端口

如果想先删除旧规则再添加新规则:

ipv4更改新端口

  1. 查看规则编号
iptables -t mangle -L OUTPUT --line-numbers

会看到类似:

num  target     prot opt source    destination
1    MARK       tcp  --  anywhere  anywhere tcp spt:3001 MARK set 0xa
  1. 删除旧规则 比如编号是 1:
iptables -t mangle -D OUTPUT 1

或者直接按内容删除:

iptables -t mangle -D OUTPUT -p tcp --sport 3001 -j MARK --set-mark 1
  1. 添加新端口规则

例如改成 443:

iptables -t mangle -A OUTPUT -p tcp --sport 443 -j MARK --set-mark 10

ipv6更改新端口

  1. 查看ipv6规则
ip6tables -t mangle -L OUTPUT --line-numbers
  1. 删除旧规则
ip6tables -t mangle -D OUTPUT 1
  1. 添加新端口
ip6tables -t mangle -A OUTPUT -p tcp --sport 3001 -j MARK --set-mark 10

四、查看限速状态

使用以下命令查看当前限速

tc class show dev eth0

输出示例解读:

class htb 1:10 parent 1:1 prio 0 rate 100Mbit ceil 100Mbit burst 1600b cburst 1600b
  • rate 100Mbit保证带宽(最低保障)
  • ceil 100Mbit峰值带宽(最大上限)
  • burst突发允许字节数
  • cburstceil 突发允许字节数

查看 iptables 命中:

iptables -t mangle -L -n -v

五、解除限速

删除 tc 限速

tc qdisc del dev eth0 root

删除 iptables 标记

iptables -t mangle -F

六、流量达到一定值后自动限速

很多人真正需要的是:

平时不限速
流量达到一定值后自动限速

例如:

  • 月流量超过 1TB
  • 自动限制为 10Mbps

这种通常配合:

vnstat + shell

实现。

七、安装 vnStat

安装:

apt install vnstat -y
systemctl enable vnstat
systemctl start vnstat

查看月流量:

vnstat -m

八、自动限速脚本

创建:

nano /root/limit.sh

写入:

#!/bin/bash

LIMIT="1000"   # 1000GB
USED=$(vnstat --json m | jq '.interfaces[0].traffic.month[0].tx + .interfaces[0].traffic.month[0].rx')

USED_GB=$((USED / 1024))

if [ "$USED_GB" -ge "$LIMIT" ]; then
    tc qdisc replace dev eth0 root tbf rate 10mbit burst 32kbit latency 400ms
fi

赋予权限:

chmod +x /root/limit.sh

九、定时检测

编辑 crontab:

crontab -e

每 5 分钟检测:

*/5 * * * * /root/limit.sh

十、恢复不限速

删除限速:

# 删除 tc
tc qdisc del dev eth0 root

# 清空 mangle 表
iptables -t mangle -F
ip6tables -t mangle -F

# 删除自定义链(如果有)
iptables -t mangle -X
ip6tables -t mangle -X

即可恢复满速。

十一、常见问题

原因:

没有创建 root qdisc。

先执行:

tc qdisc add dev eth0 root handle 1: htb default 30

2. 限速不生效

检查:

iptables -t mangle -L -n -v

看看 MARK 是否命中。

3. VPS 网卡不是 eth0

查看:

ip addr

有些系统可能是:

ens3
enp1s0

十二、推荐用途

tc 很适合:

  • Xray / Hysteria 节点限速
  • Docker 容器限速
  • 下载服务限速
  • 防止单用户跑满带宽
  • 高峰期自动限速
  • 月流量保护

十三、总结

相比传统限速工具:

工具 端口限速 灵活度 推荐
wondershaper 一般
trickle ⚠️ 一般
tc 极高 推荐

如果你只是:

整个 VPS 限速

wondershaper 足够。

但如果你想:

精准控制某个服务

tc + iptables 才是 Linux 最强方案。

免责声明
⏱ 已运行: 加载中...