当前位置:网站首页 / Linux / 正文

对centos6指定端口限速的方法

时间:2018年02月25日 | 作者 : liuhui | 分类 : Linux | 浏览: 11144次 | 评论 0

阿里云限量代金券 | 此广告位出租25元/月

本文就讲解利用 iptables  和 tc 对 CentOS 6+ X64 的某个或者某些端口做带宽限制。


系统环境

一般CentOS系统会自带 iptables 和 tc


没有那么就安装一下!


检查是否安装 , 如图显示说明已经安装!

iptables -V

tc -V

1.jpg

如果没有安装,请自行yum 安装即可。

查看网卡

我们限速,只是对外网的网卡限速。


所以,我们需要看看我的外网网卡是什么?


网卡查看命令

ifconfig

下图外网的网卡是 eth0

2.jpg

如下图,eth1 才是外网IP网卡哦。所以自己判断!!

3.jpg

知识普及:

1KB = 8Kbit


端口限速

我们只说端口限速。


演示的外网的网卡是 eth0 。


执行以下命令,请替换你自己的外网网卡名称。


1) 清理工作。


清理一下iptables Mangle规则。

iptables -t mangle -F


清理一下eth0上原有的队列类型。

tc qdisc del dev eth0 root


2) 限制上行网速


PS 本文演示只含一个规则的。


给eth0添加一个根规则。PS : 这里的root非root账号的意思。

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


创建根类。 100mbps 可以替换成你自己服务器的实际带宽。

tc class add dev eth0 parent 1: classid 1:1 htb rate 100mbps


创建支类限速。

tc class add dev eth0 parent 1:1 classid 1:5 htb rate 2500Kbit ceil 3000Kbit prio 1

rate 2500Kbit 代表最大带宽2500Kbit/s,改成你想要的带宽大小即可。


ceil 3000Kbit 代表突发带宽3000Kbit/s。


ceil >= rate


1:5 意思是每5秒钟检查一次。


创建过滤器。

tc filter add dev eth0 parent 1:0 prio 1 protocol ip handle 5 fw flowid 1:5

这里要注意的就是flowid 要和上一条的 classid对应起来。


借助iptables针对端口限速

iptables -A OUTPUT -t mangle -p tcp --sport 80 -j MARK --set-mark 5

80 就是 80 端口。可以替换其他端口。

这样就对 80 端口限制了速度了,当然你的iptables 得启动才行。

如何限制多个端口?

iptables -A OUTPUT -t mangle -p tcp --sport 80,25,443,8989 -j MARK --set-mark 5

如何限制10000-20000端口段?

iptables -A OUTPUT -t mangle -p tcp --sport 10000:20000 -j MARK --set-mark 5


3) 限制下行网速

这里不介绍。


如果不想限速了,只要执行步骤1)的iptables -t mangle -F命令即可

推荐您阅读更多有关于“”的文章

猜你喜欢

网站分类
关注电脑博士
友情链接

分享:

支付宝

微信