Note: Recently, someone asked the author how to limit the upload bandwidth of a server, so the author decided to share a script called WonderShaper
that was frequently used before. In terms of principle, WonderShaper
uses tc
to define traffic adjustment commands and uses QoS
to handle specific network interfaces. Outgoing traffic is placed in queues of different priorities to achieve the goal of limiting the outgoing traffic rate, while incoming traffic achieves the goal of rate limiting through packet loss. It is quite convenient to use, so those who have a need can learn more about it.
Usage#
Project Address: Click to Visit
For installation, you can directly use the software package, but the versions are not very new, so here we will directly pull the latest source code from Github
.
1. Install Dependencies
# Debian/Ubuntu systems
apt install -y make git
# CentOS systems
yum install make git -y
2. Install WonderShaper
git clone https://github.com/magnific0/wondershaper.git
cd wondershaper
make install
3. Set the Speed Limit
# Usage command
USAGE: wondershaper [-hcs] [-a <adapter>] [-d <rate>] [-u <rate>]
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
First, check the network card:
# Here are three commands that can be used to check the network card, it is recommended to use the first one
ifconfig
ip addr
route
For example, if you want to limit the speed of the eth0
network card, use the following command:
# Limit the upload bandwidth to 10M
wondershaper -a eth0 -u 10240
# Limit the download bandwidth to 10M
wondershaper -a eth0 -d 10240
# Limit both upload and download to 10M
wondershaper -a eth0 -d 10240 -u 10240
# Clear the network card speed limit rules
wondershaper -c -a eth0
Then we can test the speed using the following command:
wget -O speedtest-cli https://raw.githubusercontent.com/sivel/speedtest-cli/master/speedtest.py
chmod +x speedtest-cli
./speedtest-cli
This is the speed test before limiting the speed:
Speed test after limiting the upload/download to 10M:
Auto Start on Boot#
Generally, after setting the speed limit rules, if the server restarts, the speed limit rules will be automatically invalidated. So here, we need to set it up a bit so that it can take effect automatically on boot. Here are two methods.
1. Using rc.local
This is the simplest method for setting up auto start, but Debian 9
and Ubuntu 17+
do not have the rc.local
file, so users of these systems need to configure it first.
1. Add rc-local.service. Run the following command as a whole.
cat > /etc/systemd/system/rc-local.service <<EOF
[Unit]
Description=/etc/rc.local
ConditionPathExists=/etc/rc.local
[Service]
Type=forking
ExecStart=/etc/rc.local start
TimeoutSec=0
StandardOutput=tty
RemainAfterExit=yes
SysVStartPriority=99
[Install]
WantedBy=multi-user.target
EOF
2. Create the rc-local file. Run the following command as a whole.
cat > /etc/rc.local <<EOF
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
EOF
3. Add permissions and set it to start on boot.
chmod +x /etc/rc.local
systemctl start rc-local
systemctl enable rc-local
Finally, add the startup command to the rc.local
file using the following command:
# CentOS 7 system
echo "wondershaper -a eth0 -d 10240 -u 10240" >> /etc/rc.d/rc.local
chmod +x /etc/rc.d/rc.local
# CentOS 6, Debian, Ubuntu systems
echo "wondershaper -a eth0 -d 10240 -u 10240" >> /etc/rc.local
chmod +x /etc/rc.local
Modify the speed limit command accordingly.
2. Using Systemd
Since the configuration file for Systemd
is provided during installation, it is convenient to use. However, this method is only applicable to CentOS 7
, Debian 8+
, Ubuntu 16+
, etc.
Since the default configuration file called during startup is /etc/conf.d/wondershaper.conf
, edit this file first:
nano /etc/conf.d/wondershaper.conf
It should look like this:
[wondershaper]
# Adapter
#
IFACE="eth0"
# Download rate in Kbps
#
DSPEED="10240"
# Upload rate in Kbps
#
USPEED="10240"
The parameters are the network card, download limit, and upload limit, respectively. After making the changes, press Ctrl+x
, y
to save and exit.
Then start and enable it on boot:
systemctl start wondershaper
systemctl enable wondershaper