Linux下使用Wondershaper限制应用的带宽和流量|CentOS7为例

文章目录[隐藏]

Wondershaper简介

Wondershaper是一个小的bash脚本,使您可以限制Linux中的网络带宽。 它使用tc命令行程序作为配置流量控制的后端。 它是用于控制Linux服务器上带宽的便捷工具。
它允许您设置最大下载速率和/或最大上传速率。 此外,它还允许您清除已设置的限制,并可以从命令行显示接口的当前状态。 除了使用CLI选项,您还可以在systemd下将其作为服务持久运行。

安装

支持各种发行版,对应的安装方法:



$ sudo apt install wondershaper  [On Debian/Ubuntu]

$ sudo yum install wondershaper  [On CentOS/RHEL]

$ sudo dnf install wondershaper  [On Fedora 22+]

如果想安装最新版,可以自己编译安装:



$ cd /bin

$ git clone https://github.com/magnific0/wondershaper.git

$ cd wondershaper

$ sudo make install

使用

首先ip addr查看网卡名称,这里是eth0



[root@stroj wondershaper]# ip addr

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000

    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

    inet 127.0.0.1/8 scope host lo

       valid_lft forever preferred_lft forever

    inet6 ::1/128 scope host 

       valid_lft forever preferred_lft forever

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000

    link/ether 00:16:3e:93:d0:a9 brd ff:ff:ff:ff:ff:ff

    inet 5.9.199.194/24 brd 5.9.199.255 scope global eth0

       valid_lft forever preferred_lft forever

    inet6 fe80::216:3eff:fe93:d0a9/64 scope link 

       valid_lft forever preferred_lft forever

然后运行命令./wondershaper即可,如限制eth0的速度到12.5Mb:



[root@stroj wondershaper]# sudo ./wondershaper -a eth0 -u 12800

以下是帮助文档:



[root@stroj wondershaper]# sudo ./wondershaper --help

./wondershaper: illegal option -- -

USAGE: ./wondershaper [-hcs] [-a <adapter>] [-d <rate>] [-u <rate>]

Limit the bandwidth of an adapter

OPTIONS: -h Show this message -a <adapter> Set the adapter -d <rate> Set maximum download rate (in Kbps) and/or -u <rate> Set maximum upload rate (in Kbps) -p Use presets in /etc/conf.d/wondershaper.conf -c Clear the limits from adapter -s Show the current status of adapter -v Show the current version

MODES: wondershaper -a <adapter> -d <rate> -u <rate> wondershaper -c -a <adapter> wondershaper -s -a <adapter>

EXAMPLES: wondershaper -a eth0 -d 1024 -u 512 wondershaper -a eth0 -u 512 wondershaper -c -a eth0

使用systemctl守护进程使其长久生效

之前的一行代码在重启后会自动失效,因此这里使用systemctl守护进程,并设置开机启动。
新建并编辑//etc/conf.d/wondershaper



sudo nano /etc/conf.d/wondershaper

根据自己的情况设置限制参数,然后保存:



[wondershaper]

# Adapter

IFACE="eth0"

Download rate in Kbps

DSPEED=“128000”

Upload rate in Kbps

USPEED=“12800”

开启服务并设置开机启动



sudo systemctl start wondershaper

sudo systemctl enable wondershaper


This article is under CC BY-NC-SA 4.0 license.
Please quote the original link:https://www.liujason.com/article/572.html
comments powered by Disqus
Built with Hugo
Theme Stack designed by Jimmy