惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

B
Blog RSS Feed
C
CERT Recently Published Vulnerability Notes
P
Proofpoint News Feed
Y
Y Combinator Blog
T
The Blog of Author Tim Ferriss
云风的 BLOG
云风的 BLOG
H
Help Net Security
Recorded Future
Recorded Future
The Register - Security
The Register - Security
F
Full Disclosure
N
Netflix TechBlog - Medium
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hackread – Cybersecurity News, Data Breaches, AI and More
爱范儿
爱范儿
Security Archives - TechRepublic
Security Archives - TechRepublic
Simon Willison's Weblog
Simon Willison's Weblog
Cisco Talos Blog
Cisco Talos Blog
I
InfoQ
T
Tenable Blog
T
Tor Project blog
人人都是产品经理
人人都是产品经理
D
DataBreaches.Net
NISL@THU
NISL@THU
Google DeepMind News
Google DeepMind News
博客园 - 叶小钗
B
Blog
V
V2EX
Jina AI
Jina AI
L
LangChain Blog
月光博客
月光博客
W
WeLiveSecurity
U
Unit 42
AWS News Blog
AWS News Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
博客园 - 聂微东
V
Visual Studio Blog
A
Arctic Wolf
T
Tailwind CSS Blog
The Cloudflare Blog
SecWiki News
SecWiki News
S
SegmentFault 最新的问题
Hacker News - Newest:
Hacker News - Newest: "LLM"
宝玉的分享
宝玉的分享
MyScale Blog
MyScale Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
S
Securelist
www.infosecurity-magazine.com
www.infosecurity-magazine.com
腾讯CDC
雷峰网
雷峰网

运维开发绿皮书

OBS虚拟摄像头重命名 Ubuntu备份为LiveOS 使用Powershell卸载windows默认程序 Cisco路由器OSPF配置 汽车的分类和特点 Windows11跳过TPM2.0 HTTPS 双向认证与 USB 加密锁配置实战 SSL 证书工具 字数统计 BMI 计算 颜色转换 Hash 生成器 JSON 格式化 JWT 解码 房贷计算 时间戳转换 URL 编码 UUID 生成 牛马时钟 二维码批量生成器 CKS Simulator Kubernetes 1.25 FRP TOTP验证码生成器 direct-ssh-passthrough-nat 脚本使用说明 Python软件授权 Linux命令行百度网盘 为 Containerd 配置 Harbor 无证书镜像站 Docker一键部署Meta和MetacubexD面板 必应搜索屏蔽垃圾网站 VMware的ubuntu完整安装vm-tools支持粘贴板 Docker Desktop 安装到其他位置 Dell EMC PowerEdge R740服务器内存插槽使用说明及正确安装方法 OpenStack排错 稳定币监管与投资指南 VMware ESXi 密码恢复指南 VMware 加密 state.tgz 文件解密教程 六百万数据 MySQL count(*) 优化 使用iptables禁止特定子网访问指定端口 解决 iptables DNAT 无法转发到 127.0.0.1 的问题及安全风险分析 Linux 一键测试脚本 Gitlab配置详解 Gitlab批量创建用户 Ubuntu安装VNC教程 Windows11跳过微软账号登录 vue配置Nginx伪静态 Linux下Ollama开放端口 模型详解配置 修改Git提交历史中的作者信息 在PowerShell中启动Git Bash的方法 一键安装OpenVPN 大语言模型 模型架构-Transformer模型 训练相关资料 GPT&DeepSeek模型 大模型技术基础 Git Bash 中一键安装 pacman Gitlab-Runner的一些问题 Docker运行kwaivgi-liveportrait Firefox密码提取 在x86-64上构建和运行多种架构的Docker容器 flutter配置镜像站 VMware最新下载地址(纪念碑) 一键安装Conda Docker老版本runc报错无法启动 清理Rancher节点 aapanel 7.x 中文语言配置指南 Ubuntu 22 安装 Kubernetes 1.29 集群指南 Ubuntu修复CNVD-2024-4920726 GRUB锁定密码 Grub2手动引导Linux Cron在线表达式生成器 GitLab一键设置镜像源 文本字符串倒序 Windows禁用任务管理器 UOS(1070a)服务器版本部署Kubernetes1.28 Windows11恢复Windows10右键菜单 Ubuntu一键设置镜像源 CentOS 7 一键设置Vault镜像 Base64在线编码解码 代码差异对比 为Docker分配物理网卡 解密CFSSL生成的CRL数据 已经在谷底了 怎么走都是向上 GitLab统计提交代码行数Python代码 Windows11的24H2出现扩展错误 Windows绕过MicroSoft Store直接下载应用 Fossy平台离线部署分析 在WSL中移除Windows环境变量 重装MicroSoft Store Sony Xperia 10 IV(pdx225)AOSP14编译和刷机方法 pnpm在DevContainer中存储出错解决方法 从SVN迁移仓库到Git Ubuntu中移除Snap软件包 使用Netcat检测UDP端口连通性 rke2集群命令行调试方法 Ubuntu隐私优化-关闭公共门户连接检查 Cisco路由器配置ipv4和ipv6的VTY账户密码 Python实现九九乘法表 Windows10中用多网卡链路聚合来解决网卡网速瓶颈 CentOS7 安装 OpenResty
tc常用命令总结
Paper-Dragon · 2025-09-18 · via 运维开发绿皮书

tc常用命令总结

适用系统:大多数 Linux(内核已启用 tcsch_*act_* 模块) 涉及组件:qdisc(队列规则)、class(类)、filter(过滤器)、action(动作)

快速认知:tc 能做什么

  • 带宽整形:限制速率、突发(burst)、峰值(peakrate)
  • 时延模拟:固定延迟、抖动(jitter)
  • 丢包/乱序/损坏:模拟弱网、移动网络
  • 队列管理:公平队列、主动队列管理(AQM)降低 bufferbloat
  • 按流分类:基于 ip/port/proto 等进行不同带宽/优先级

核心概念与术语速记

  • qdisc(queueing discipline):挂在某网卡的 egress/ingress 上的队列规则。
  • class:在 classful qdisc(如 HTB)下的子队列,便于做分层与分配。
  • filter:流量分类器,把包分到某个 class,或触发某个 action。
  • handle/parent:句柄与父子关系,如 1:1:10。根通常是 rootingress
  • egress/ingress:
    • egress(发出/上传)通常直接整形。
    • ingress(进入/下载)常用 policer 或配合 IFB 做“伪 egress”整形。
  • ifb(Intermediate Functional Block):虚拟网卡,用于把 ingress 重定向后作为 egress 整形。

在这里插入图片描述

基础命令速查

查看当前规则

# 查看网卡 egress 的 qdisc
sudo tc qdisc show dev eth0

# 查看 class(以 HTB 为例)
sudo tc class show dev eth0

# 查看 filter(包含 match 条件与去向)
sudo tc filter show dev eth0

清空/删除

# 删除根 qdisc(恢复内核默认)
sudo tc qdisc del dev eth0 root

# 删除 ingress qdisc
sudo tc qdisc del dev eth0 ingress

提示:若提示“没有此对象”,说明当前并未设置对应 qdisc。

常用 qdisc 与典型用法

1)netem:延迟/抖动/丢包

最适合模拟弱网,默认作用于 egress。

# 固定延迟 100ms
sudo tc qdisc add dev eth0 root netem delay 100ms

# 延迟 100ms,抖动 20ms,相关性 25%
sudo tc qdisc replace dev eth0 root netem delay 100ms 20ms 25%

# 丢包 2%
sudo tc qdisc replace dev eth0 root netem loss 2%

# 丢包 1%,相关性 25%
sudo tc qdisc replace dev eth0 root netem loss 1% 25%

# 组合:延迟 + 丢包 + 抖动
sudo tc qdisc replace dev eth0 root netem delay 80ms 10ms 20% loss 1%

# 取消
sudo tc qdisc del dev eth0 root

易错点:netem 并不做限速;它是时延/丢包/乱序等“质量”模拟。

2)tbf:令牌桶限速(单速率)

适合简单、稳定的限速。关键参数:rateburst(桶大小)、latency(排队上限)。

# 将上传(egress)限速到 10Mbit,桶 32KB,最大等待 400ms
sudo tc qdisc add dev eth0 root tbf rate 10mbit burst 32kbit latency 400ms

# 调整为 20Mbit
sudo tc qdisc replace dev eth0 root tbf rate 20mbit burst 64kbit latency 400ms

# 取消
sudo tc qdisc del dev eth0 root

技巧:burst 过小会导致速度“上不去”或抖动大;合理取值一般为几倍 MTU 到几十 KB。

3)htb:分层带宽管理

支持 class 树与最小/最大带宽(rate/ceil)。适合多业务/多 IP/端口的带宽分配。

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

# 根 class,带宽上限 100Mbit
sudo tc class add dev eth0 parent 1: classid 1:1 htb rate 100mbit ceil 100mbit

# 子类:视频流(保证 60M,上限 100M)
sudo tc class add dev eth0 parent 1:1 classid 1:10 htb rate 60mbit ceil 100mbit

# 子类:普通业务(保证 20M,上限 60M)
sudo tc class add dev eth0 parent 1:1 classid 1:20 htb rate 20mbit ceil 60mbit

# 默认类(兜底 5M,上限 20M)
sudo tc class add dev eth0 parent 1:1 classid 1:30 htb rate 5mbit ceil 20mbit

# 过滤:把目的端口 1935(RTMP)流量打到 1:10
sudo tc filter add dev eth0 parent 1: protocol ip prio 1 \
  u32 match ip dport 1935 0xffff flowid 1:10

# 过滤:把源网段 10.0.0.0/24 打到 1:20
sudo tc filter add dev eth0 parent 1: protocol ip prio 2 \
  u32 match ip src 10.0.0.0/24 flowid 1:20

# 查看
sudo tc -s class show dev eth0

备注:u32 过滤器简单直接;更复杂可用 flower(支持 IPv6/VLAN/TCP flags 等)。

Ingress 限速与 IFB(下载方向整形)

ingress 端无法像 egress 一样“排队后再发”,常见两种做法:

1)policer:超速即丢(或标记),实现简单但体验差。

# 对下载方向做 policer,超过 50Mbit 的直接丢弃
sudo tc qdisc add dev eth0 handle ffff: ingress
sudo tc filter add dev eth0 parent ffff: protocol ip prio 1 \
  u32 match u32 0 0 police rate 50mbit burst 100k drop flowid :1

2)IFB:把 ingress 重定向到 ifb0,再在 ifb0 的 egress 上用 tbf/htb 整形(用户体验更好)。

# 启动 IFB
action_ifb() {
  sudo modprobe ifb
  sudo ip link add ifb0 type ifb || true
  sudo ip link set ifb0 up
  # 把 eth0 的 ingress 重定向到 ifb0
  sudo tc qdisc add dev eth0 handle ffff: ingress 2>/dev/null || true
  sudo tc filter add dev eth0 parent ffff: protocol ip prio 1 \
    u32 match u32 0 0 action mirred egress redirect dev ifb0
}

# 对下载做 30M 限速(在 ifb0 的 egress 使用 tbf)
sudo tc qdisc add dev ifb0 root tbf rate 30mbit burst 64kbit latency 400ms

# 恢复
sudo tc qdisc del dev ifb0 root
sudo tc qdisc del dev eth0 ingress

实战案例

A. 一键模拟弱网(延迟 + 抖动 + 丢包)

sudo tc qdisc replace dev eth0 root netem delay 120ms 30ms 25% loss 1%

B. 限制上传到 8Mbit,并启用 fq_codel 保持低延迟

sudo tc qdisc replace dev eth0 root handle 1: htb default 20
sudo tc class replace dev eth0 parent 1: classid 1:1 htb rate 8mbit ceil 8mbit

C. 下载(ingress)限速到 20Mbit(IFB)

sudo modprobe ifb
sudo ip link add ifb0 type ifb || true
sudo ip link set ifb0 up
sudo tc qdisc add dev eth0 handle ffff: ingress 2>/dev/null || true
sudo tc filter add dev eth0 parent ffff: protocol ip prio 1 \
  u32 match u32 0 0 action mirred egress redirect dev ifb0
sudo tc qdisc add dev ifb0 root tbf rate 20mbit burst 64kbit latency 400ms

F. 一键清理(恢复默认)

sudo tc qdisc del dev eth0 root 2>/dev/null || true
sudo tc qdisc del dev eth0 ingress 2>/dev/null || true
sudo tc qdisc del dev ifb0 root 2>/dev/null || true

验证与排障技巧

  • tc -s qdisc show dev eth0 查看统计(-s 显示字节/包数与丢包统计)。
  • 配合 iperf3 验证吞吐、ping/mtr 验证时延、curl -w 观测首包时间。
  • tcpdump/wireshark 观察乱序、重传与 ECN 标记。
  • 常见问题:
    • 设了下载限速不生效:是否使用了 IFB?仅在 egress 生效会造成误判。
    • 速率跑不满:burst 太小或 CPU 不足;检查 latency、MTU 与网卡 offload。
    • 多次叠加 qdisc:注意 root/parent 层级是否正确,避免被后续 replace 覆盖。

速记清单(Cheat Sheet)

# 查看现状
sudo tc -s qdisc show dev eth0

# egress 限速:tbf
sudo tc qdisc replace dev eth0 root tbf rate 10mbit burst 32kbit latency 400ms

# 弱网模拟:netem(延迟+丢包)
sudo tc qdisc replace dev eth0 root netem delay 100ms 20ms 25% loss 1%

# Ingress policer(超速丢)
sudo tc qdisc add dev eth0 handle ffff: ingress
sudo tc filter add dev eth0 parent ffff: protocol ip prio 1 \
  u32 match u32 0 0 police rate 50mbit burst 100k drop flowid :1

# IFB 下载限速(重定向后整形)
sudo modprobe ifb && sudo ip link add ifb0 type ifb && sudo ip link set ifb0 up
sudo tc qdisc add dev eth0 handle ffff: ingress
sudo tc filter add dev eth0 parent ffff: protocol ip prio 1 \
  u32 match u32 0 0 action mirred egress redirect dev ifb0
sudo tc qdisc add dev ifb0 root tbf rate 20mbit burst 64kbit latency 400ms

# 清理
sudo tc qdisc del dev eth0 root; sudo tc qdisc del dev eth0 ingress; sudo tc qdisc del dev ifb0 root

结语

  • 开发/联调:netem 还原弱网;
  • 线上整形:tbf/htb 保证关键业务;
  • 降延迟:默认使用 fq_codel 优化排队时延;
  • 下载限速:优先选择 IFB 方案获得更好体验。