背景
近日做实验,需要限制一些机子的带宽以达到模拟的效果。在网上搜索了一阵子,结合自己实操的经验,潦草写下这篇文章,供自己与有需要的人参考。
环境: Ubuntu 22.04.1 LTS
安装 wondershaper 和 speedtest-cli
wondershaper 是一个用来管理带宽的工具,使用它可以快速地设置一台机子上某个网口的上行和下行最大带宽。
首先从github上拉取最新的包
git clone https://github.com/magnific0/wondershaper.git
然后安装
cd wondershaper
sudo make install
speedtest-cli 是一个测量网速的工具,可以用它来验证 wondershaper 是否设置成功。
sudo apt install speedtest-cli
安装完成后可以运行一下,测试本机当前的网速,测速过程有些慢,可能需要两三分钟。
speedtest
这是我的结果
hh@pc02:~$ speedtest
Retrieving speedtest.net configuration...
Testing from China Unicom (58.249.112.15)...
Retrieving speedtest.net server list...
Selecting best server based on ping...
Hosted by Telstra International (Hong Kong) [60.86 km]: 26.282 ms
Testing download speed................................................................................
Download: 28.95 Mbit/s
Testing upload speed......................................................................................................
Upload: 35.80 Mbit/s
查看本机的网口
运行
ifconfig
即可看到本机的网口,从中选择你要设置的网口。
hh@pc02:~$ ifconfig
eno1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.3.5 netmask 255.255.255.0 broadcast 192.168.3.255
inet6 fe80::3eec:efff:feb1:9b40 prefixlen 64 scopeid 0x20<link>
ether 3c:ec:ef:b1:9b:40 txqueuelen 1000 (以太网)
RX packets 91440274 bytes 105110205902 (105.1 GB)
RX errors 0 dropped 3063 overruns 0 frame 0
TX packets 84440960 bytes 100752219041 (100.7 GB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
eno2: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
ether ac:1f:6b:87:59:3a txqueuelen 1000 (以太网)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
device interrupt 16 memory 0xaae00000-aae20000
我选择对 eno1
网口进行设置。
配置 wondershaper
首先创建配置文件
sudo vim /etc/systemd/wondershaper.conf
在其中写入以下内容
[wondershaper]
# Adapter
IFACE="eno1"
# Download rate in Kbps
DSPEED="5120"
# Upload rate in Kbps
USPEED="1024"
其中三个参数分别是想要设置的网口名称、网口下载带宽和网口上传带宽。这里我将下载带宽设置为5Mbps,上传带宽设置为1Mbps。
如果发现这个文件已经存在,则直接修改这三个参数即可,其它不用管。
然后创建以下文件
sudo vim /etc/systemd/system/wondershaper.service
在其中写入以下内容
[Unit]
Description=Bandwidth shaper/Network rate limiter
After=network-online.target
Wants=network.target
[Service]
Type=oneshot
RemainAfterExit=yes
EnvironmentFile=/etc/systemd/wondershaper.conf
ExecStart=/usr/local/sbin/wondershaper -a $IFACE -d $DSPEED -u $USPEED
ExecStop=/usr/local/sbin/wondershaper -c -a $IFACE
[Install]
WantedBy=multi-user.target
注意,ExecStart和ExecStop这两个命令用到了刚刚安装的wondershaper,如果你的wondershaper不是安装在/usr/local/sbin/wondershaper
这个目录,则需要修改一下。可以通过 which wondershaper
看看包所在的目录。
启动和关闭 wondershaper
完成上一步的配置后,运行
sudo systemctl enable --now wondershaper.service
以启动wondershaper服务,开始对指定网口进行限速。
你可以通过
systemctl status wondershaper.service
查看该服务的启动情况,以下是我的结果,说明启动成功了。
h@pc02:~/wondershaper$ systemctl status wondershaper.service
● wondershaper.service - Bandwidth shaper/Network rate limiter
Loaded: loaded (/etc/systemd/system/wondershaper.service; enabled; vendor preset: enabled)
Active: active (exited) since Thu 2023-11-09 20:50:47 CST; 1min 48s ago
Process: 738786 ExecStart=/usr/local/sbin/wondershaper -a $IFACE -d $DSPEED -u $USPEED (code=exited, status=0/SUCCESS)
Main PID: 738786 (code=exited, status=0/SUCCESS)
测试当前的网速
speedtest
可以看到网速成功降下来了
huanglab@pc02:~/wondershaper$ speedtest
Retrieving speedtest.net configuration...
Testing from China Unicom (58.249.112.15)...
Retrieving speedtest.net server list...
Selecting best server based on ping...
Hosted by Telstra International (Hong Kong) [60.86 km]: 21.886 ms
Testing download speed................................................................................
Download: 4.74 Mbit/s
Testing upload speed......................................................................................................
Upload: 1.38 Mbit/s
不过上传速度好像超出了我设置的1Mbps…
总之整个过程大概就是这样啦。
如果用完想恢复原来的带宽设置,只需取消wondershaper的服务即可,运行以下命令:
systemctl disable --now wondershaper.service
。
重新用speedtest测量一下网速,应该可以恢复设置之前的样子了。
更改带宽配置
完成第一次带宽配置后,如果之后想重新配置带宽,只需修改wondershaper.conf,并且
先disable
sudo systemctl disable --now wondershaper.service
再enable
sudo systemctl enable --now wondershaper.service
相当于重新启动该服务,使用新的参数。
更方便的测试方法
过了几天发现一个更方便地测试带宽的方法,介绍如下。
假设你已经按照前面的指引安装和配置了wondershaper,接下来只需安装 iperf3。
sudo apt-get install iperf3
iperf3是一个测试工具,可以测试两台机器之间的带宽。
将一台机器作为服务端,运行:
iperf3 -s
该机器会开始监听。
将另一台服务器作为客户端,运行:
iperf3 -c [服务器的ip]
hh@:~$ iperf3 -c 192.168.3.2
Connecting to host 192.168.3.2, port 5201
[ 5] local 192.168.3.4 port 41590 connected to 192.168.3.2 port 5201
[ ID] Interval Transfer Bitrate Retr Cwnd
[ 5] 0.00-1.00 sec 114 MBytes 958 Mbits/sec 0 393 KBytes
[ 5] 1.00-2.00 sec 112 MBytes 942 Mbits/sec 0 393 KBytes
[ 5] 2.00-3.00 sec 112 MBytes 943 Mbits/sec 0 411 KBytes
[ 5] 3.00-4.00 sec 112 MBytes 942 Mbits/sec 0 411 KBytes
[ 5] 4.00-5.00 sec 112 MBytes 942 Mbits/sec 0 430 KBytes
[ 5] 5.00-6.00 sec 112 MBytes 942 Mbits/sec 0 430 KBytes
[ 5] 6.00-7.00 sec 112 MBytes 941 Mbits/sec 0 430 KBytes
[ 5] 7.00-8.00 sec 112 MBytes 941 Mbits/sec 0 430 KBytes
[ 5] 8.00-9.00 sec 112 MBytes 936 Mbits/sec 0 430 KBytes
[ 5] 9.00-10.00 sec 112 MBytes 941 Mbits/sec 0 430 KBytes
- - - - - - - - - - - - - - - - - - - - - - - - -
[ ID] Interval Transfer Bitrate Retr
[ 5] 0.00-10.00 sec 1.10 GBytes 943 Mbits/sec 0 sender
[ 5] 0.00-10.00 sec 1.10 GBytes 941 Mbits/sec receiver
iperf Done.
测试大约10秒钟,这是没限制带宽之前的网速。
使用wondershaper限制客户端的带宽之后,再次测试,结果如下:
hh@:~$ iperf3 -c 192.168.3.2
Connecting to host 192.168.3.2, port 5201
[ 5] local 192.168.3.4 port 41594 connected to 192.168.3.2 port 5201
[ ID] Interval Transfer Bitrate Retr Cwnd
[ 5] 0.00-1.00 sec 1.00 MBytes 8.41 Mbits/sec 0 74.9 KBytes
[ 5] 1.00-2.00 sec 509 KBytes 4.17 Mbits/sec 0 74.9 KBytes
[ 5] 2.00-3.00 sec 509 KBytes 4.17 Mbits/sec 0 74.9 KBytes
[ 5] 3.00-4.00 sec 679 KBytes 5.56 Mbits/sec 0 74.9 KBytes
[ 5] 4.00-5.00 sec 509 KBytes 4.17 Mbits/sec 0 74.9 KBytes
[ 5] 5.00-6.00 sec 509 KBytes 4.17 Mbits/sec 0 74.9 KBytes
[ 5] 6.00-7.00 sec 679 KBytes 5.56 Mbits/sec 0 74.9 KBytes
[ 5] 7.00-8.00 sec 509 KBytes 4.17 Mbits/sec 0 74.9 KBytes
[ 5] 8.00-9.00 sec 509 KBytes 4.17 Mbits/sec 0 74.9 KBytes
[ 5] 9.00-10.00 sec 679 KBytes 5.56 Mbits/sec 0 74.9 KBytes
- - - - - - - - - - - - - - - - - - - - - - - - -
[ ID] Interval Transfer Bitrate Retr
[ 5] 0.00-10.00 sec 5.97 MBytes 5.01 Mbits/sec 0 sender
[ 5] 0.00-10.00 sec 5.55 MBytes 4.65 Mbits/sec receiver
iperf Done.
可以看到网速明显地降下来了。
参考
https://averagelinuxuser.com/limit-bandwidth-linux/#install-wondershaper
https://wangchujiang.com/linux-command/c/speedtest-cli.html