如何使用TC對特定端口進行限速


Linux下有個特別好用的工具叫TC(traffic control),主要用來在網卡或者是針對某個IP進行限速,時延處理。

下面將為你介紹如何在特定網卡上對特定端口配合iptables命令應用不同的限速規則進行限速。

前提條件

  1. root權限
  2. 獨立網卡(容器內不適用,VM或物理機可以)

清除所有配置

tc qdisc del dev ens3 root
iptables -t mangle -F

開始配置

tc qdisc add dev ens3 root handle 1: htb default 2
tc class add dev ens3 parent 1: classid 1:1 htb rate 40000mbit
#建立3個限速規則
tc class add dev ens3 parent 1:1 classid 1:10 htb rate 8kbit
tc class add dev ens3 parent 1:1 classid 1:20 htb rate 8kbit
tc class add dev ens3 parent 1:1 classid 1:30 htb rate 8kbit
tc filter add dev ens3 protocol ip parent 1:0 prio 1 handle 10 fw classid 1:10
tc filter add dev ens3 protocol ip parent 1:0 prio 1 handle 20 fw classid 1:20
tc filter add dev ens3 protocol ip parent 1:0 prio 1 handle 30 fw classid 1:30

#ipables針對source port使用不同的規則
iptables -A OUTPUT -t mangle -p tcp --sport 80 -j MARK --set-mark 10
iptables -A OUTPUT -t mangle -p tcp --sport 443 -j MARK --set-mark 20
iptables -A OUTPUT -t mangle -p udp --sport 451 -j MARK --set-mark 30


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM