linux – 如何使用“tc”命令对特定端口进行简单的流量控制
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了linux – 如何使用“tc”命令对特定端口进行简单的流量控制,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3217字,纯文字阅读大概需要5分钟。
内容图文
![linux – 如何使用“tc”命令对特定端口进行简单的流量控制](/upload/InfoBanner/zyjiaocheng/964/af99bce9b8724fd0a26bb1898a58a134.jpg)
我是Linux的新手,我的目标是使用tc命令(或ifconfig或iptables等其他命令,但我不认为我需要它们)为“eth0”或“lo”创建一个简单的流量控制.
我的内核是2.6.18-238.el5 GNU / Linux,我正在使用redhat.
我的脚本是:
tc qdisc del dev $DEV root
tc qdisc add dev $DEV root handle 1: htb default 10
tc class add dev $DEV parent 1: classid 1:10 htb rate $DNLD
tc filter add dev $DEV parent 1: protocol ip u32 match ip dport $input_port 0xffff flowid 1:10
虽然$DNLD是带宽限制,$DEV是eth0或lo,$input_port是我想要限制的端口.
我已经在互联网上阅读了很多页面,并了解这是应该如何编写的,但这一行并不限制特定端口,而是所有端口.
我也尝试使用“运动”,但它也不起作用.
所以我不明白问题是什么.
另一个奇怪的事情是带宽限制似乎正在起作用,并且重置线似乎也在起作用,(这是第一行:
tc qdisc del dev $DEV root)
但是,我写完之后的输出行是:
“RTNETLINK回答:没有这样的文件或目录”
我不知道为什么以及它是否造成任何损害.
如果您需要任何其他信息,请与我们联系.
如果有人能够帮助我,我会很高兴.
谢谢你.
解决方法:
我不知道这是否正是您正在寻找的内容,但这是一个您可以编辑和运行的脚本.我用它来限制我的连接并测试网络应用程序.
#!/bin/bash
#
# tc uses the following units when passed as a parameter.
# kbps: Kilobytes per second
# mbps: Megabytes per second
# kbit: Kilobits per second
# mbit: Megabits per second
# bps: Bytes per second
# Amounts of data can be specified in:
# kb or k: Kilobytes
# mb or m: Megabytes
# mbit: Megabits
# kbit: Kilobits
# To get the byte figure from bits, divide the number by 8 bit
#
#
# Name of the traffic control command.
TC=/sbin/tc
# The network interface we're planning on limiting bandwidth.
IF=`ip addr | grep 2: | cut -d' ' -f2 | cut -d: -f1`
# Latency
LAT_1=200ms # Base latency
LAT_2=50ms # Plus or minus
LAT_3=25% # Based on previous packet %
# Dropping packets
DROP_1=5% # Base probability
DROP_2=25% # Based on previous packet %
# Bandwidth
#DNLD=33kbps # DOWNLOAD Limit
#UPLD=33kbps # UPLOAD Limit
DNLD=1Mbps # DOWNLOAD Limit
UPLD=1Mbps # UPLOAD Limit
# IP address of the machine we are controlling
IP=`ip addr | grep "inet " | tail -1 | cut -d' ' -f6 | cut -d/ -f1`
# Filter options for limiting the intended interface.
U32="$TC filter add dev $IF protocol ip parent 1:0 prio 1 u32"
# We'll use Hierarchical Token Bucket (HTB) to shape bandwidth.
# For detailed configuration options, please consult Linux man
# page.
#$TC qdisc add dev $IF root handle 2: netem delay $LAT_1 $LAT_2 $LAT_3 loss $DROP_1 $DROP_2
$TC qdisc add dev $IF root handle 2: netem delay $LAT_1 $LAT_2 $LAT_3 loss $DROP_1 $DROP_2
$TC qdisc add dev $IF parent 2: handle 1: htb default 30
$TC class add dev $IF parent 1: classid 1:1 htb rate $DNLD
$TC class add dev $IF parent 1: classid 1:2 htb rate $UPLD
$U32 match ip dst $IP/32 flowid 1:1
$U32 match ip src $IP/32 flowid 1:2
# The first line creates the root qdisc, and the next three lines
# create three child qdisc that are to be used to shape download
# and upload bandwidth.
#
# The 5th and 6th line creates the filter to match the interface.
# The 'dst' IP address is used to limit download speed, and the
# 'src' IP address is used to limit upload speed.
echo Limit to $DNLD on $IF for $IP
内容总结
以上是互联网集市为您收集整理的linux – 如何使用“tc”命令对特定端口进行简单的流量控制全部内容,希望文章能够帮你解决linux – 如何使用“tc”命令对特定端口进行简单的流量控制所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。