网站设计有限公司怎么样,wordpress建公司官网,低俗网站推广哪里找,湖州网站建设推广背景
近日做实验#xff0c;需要限制一些机子的带宽以达到模拟的效果。在网上搜索了一阵子#xff0c;结合自己实操的经验#xff0c;潦草写下这篇文章#xff0c;供自己与有需要的人参考。
环境#xff1a; Ubuntu 22.04.1 LTS
安装 wondershaper 和 speedtest-cli
w…背景
近日做实验需要限制一些机子的带宽以达到模拟的效果。在网上搜索了一阵子结合自己实操的经验潦草写下这篇文章供自己与有需要的人参考。
环境 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 这是我的结果
hhpc02:~$ 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 即可看到本机的网口从中选择你要设置的网口。
hhpc02:~$ ifconfig
eno1: flags4163UP,BROADCAST,RUNNING,MULTICAST mtu 1500inet 192.168.3.5 netmask 255.255.255.0 broadcast 192.168.3.255inet6 fe80::3eec:efff:feb1:9b40 prefixlen 64 scopeid 0x20linkether 3c:ec:ef:b1:9b:40 txqueuelen 1000 (以太网)RX packets 91440274 bytes 105110205902 (105.1 GB)RX errors 0 dropped 3063 overruns 0 frame 0TX packets 84440960 bytes 100752219041 (100.7 GB)TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0eno2: flags4099UP,BROADCAST,MULTICAST mtu 1500ether ac:1f:6b:87:59:3a txqueuelen 1000 (以太网)RX packets 0 bytes 0 (0.0 B)RX errors 0 dropped 0 overruns 0 frame 0TX packets 0 bytes 0 (0.0 B)TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0device interrupt 16 memory 0xaae00000-aae20000我选择对 eno1 网口进行设置。
配置 wondershaper
首先创建配置文件 sudo vim /etc/systemd/wondershaper.conf 在其中写入以下内容
[wondershaper]# Adapter
IFACEeno1# Download rate in Kbps
DSPEED5120# Upload rate in Kbps
USPEED1024其中三个参数分别是想要设置的网口名称、网口下载带宽和网口上传带宽。这里我将下载带宽设置为5Mbps上传带宽设置为1Mbps。 如果发现这个文件已经存在则直接修改这三个参数即可其它不用管。
然后创建以下文件 sudo vim /etc/systemd/system/wondershaper.service 在其中写入以下内容
[Unit]
DescriptionBandwidth shaper/Network rate limiter
Afternetwork-online.target
Wantsnetwork.target[Service]
Typeoneshot
RemainAfterExityes
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]
WantedBymulti-user.target注意ExecStart和ExecStop这两个命令用到了刚刚安装的wondershaper如果你的wondershaper不是安装在/usr/local/sbin/wondershaper这个目录则需要修改一下。可以通过 which wondershaper看看包所在的目录。
启动和关闭 wondershaper
完成上一步的配置后运行 sudo systemctl enable --now wondershaper.service以启动wondershaper服务开始对指定网口进行限速。 你可以通过 systemctl status wondershaper.service 查看该服务的启动情况以下是我的结果说明启动成功了。
hpc02:~/wondershaper$ systemctl status wondershaper.service
● wondershaper.service - Bandwidth shaper/Network rate limiterLoaded: loaded (/etc/systemd/system/wondershaper.service; enabled; vendor preset: enabled)Active: active (exited) since Thu 2023-11-09 20:50:47 CST; 1min 48s agoProcess: 738786 ExecStart/usr/local/sbin/wondershaper -a $IFACE -d $DSPEED -u $USPEED (codeexited, status0/SUCCESS)Main PID: 738786 (codeexited, status0/SUCCESS)
测试当前的网速 speedtest 可以看到网速成功降下来了
huanglabpc02:~/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 receiveriperf 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 receiveriperf Done.
可以看到网速明显地降下来了。
参考
https://averagelinuxuser.com/limit-bandwidth-linux/#install-wondershaper https://wangchujiang.com/linux-command/c/speedtest-cli.html