ubuntu 设置最大带宽

编程入门 行业动态 更新时间:2024-10-08 22:49:56

ubuntu 设置最大<a href=https://www.elefans.com/category/jswz/34/1767894.html style=带宽"/>

ubuntu 设置最大带宽

背景

近日做实验,需要限制一些机子的带宽以达到模拟的效果。在网上搜索了一阵子,结合自己实操的经验,潦草写下这篇文章,供自己与有需要的人参考。

环境: Ubuntu 22.04.1 LTS

安装 wondershaper 和 speedtest-cli

wondershaper 是一个用来管理带宽的工具,使用它可以快速地设置一台机子上某个网口的上行和下行最大带宽。

首先从github上拉取最新的包
git clone .git
然后安装
cd wondershaper
sudo make install

speedtest-cli 是一个测量网速的工具,可以用它来验证 wondershaper 是否设置成功。
sudo apt install speedtest-cli
安装完成后可以运行一下,测试本机当前的网速,测速过程有些慢,可能需要两三分钟。
speedtest
这是我的结果

hh@pc02:~$ speedtest
Retrieving speedtest configuration...
Testing from China Unicom (58.249.112.15)...
Retrieving speedtest 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 1500inet 192.168.3.5  netmask 255.255.255.0  broadcast 192.168.3.255inet6 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 0TX packets 84440960  bytes 100752219041 (100.7 GB)TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0eno2: flags=4099<UP,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
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 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 (code=exited, status=0/SUCCESS)Main PID: 738786 (code=exited, status=0/SUCCESS)

测试当前的网速
speedtest
可以看到网速成功降下来了

huanglab@pc02:~/wondershaper$ speedtest
Retrieving speedtest configuration...
Testing from China Unicom (58.249.112.15)...
Retrieving speedtest 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测量一下网速,应该可以恢复设置之前的样子了。

参考


.html

更多推荐

ubuntu 设置最大带宽

本文发布于:2023-11-15 17:49:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1603682.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:带宽   ubuntu

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!