需要用到两个工具wondershaper、speedtest.py:

1、wondershaper、speedtest.py安装

下载安装:

1
git clone https://github.com/magnific0/wondershaper.git
1
git clone https://github.com/sivel/speedtest-cli.git

2、限速

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
cd wondershaper
./wondershaper -h
Running as non root / sudo user
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/systemd/wondershaper.conf"
   -f <file>    Use alternative preset file
   -c           Clear the limits from adapter
   -s           Show the current status of adapter
   -v           Show the current version
   Configure HIPRIODST in "/etc/systemd/wondershaper.conf" for hosts
   requiring high priority i.e. in case ssh uses dport 443.
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
   wondershaper -p -f foo.conf

可以看到,限速单位是Kbps.

限速命令: sudo ./wondershaper -a enp5s0f0 -d 16384 -u 16384 限制上传速度和下载速度都为16Mbp/s,也就是2MBytes/s

3、测速

cd speedtest-cli

未限速前:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
python speedtest.py 
Retrieving speedtest.net configuration...
Testing from China Unicom (xxx.xxx.xxx.xx)...
Retrieving speedtest.net server list...
Selecting best server based on ping...
Hosted by xx (xx) [9.32 km]: 25.042 ms
Testing download speed................................................................................
Download: 47.82 Mbit/s
Testing upload speed................................................................................................
Upload: 91.86 Mbit/s

限速后:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
python speedtest.py 
Retrieving speedtest.net configuration...
Testing from China Unicom (xxx.xxx.xxx.xxx)...
Retrieving speedtest.net server list...
Selecting best server based on ping...
Hosted by xxx hello 5G (xxx) [468.85 km]: 27.891 ms
Testing download speed................................................................................
Download: 15.43 Mbit/s
Testing upload speed................................................................................................
Upload: 14.80 Mbit/s

4、取消限速

1
sudo ./wondershaper -c -a enp5s0f0