ubuntu 设置最大带宽

背景

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

环境: 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

这是我的结果

bash 复制代码
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

即可看到本机的网口,从中选择你要设置的网口。

bash 复制代码
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

在其中写入以下内容

bash 复制代码
[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

在其中写入以下内容

bash 复制代码
[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 查看该服务的启动情况,以下是我的结果,说明启动成功了。

bash 复制代码
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

可以看到网速成功降下来了

bash 复制代码
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测量一下网速,应该可以恢复设置之前的样子了。

参考

https://averagelinuxuser.com/limit-bandwidth-linux/#install-wondershaper

https://wangchujiang.com/linux-command/c/speedtest-cli.html

相关推荐
橘子真甜~21 分钟前
C/C++ Linux网络编程2 - Socket编程与简单UDP服务器客户端
linux·运维·服务器·网络编程api·udp协议·udp通信
深圳市恒讯科技26 分钟前
服务器与普通个人电脑的主要区别是什么?
运维·服务器
qq_2813174735 分钟前
nginx安装配置、故障处置、性能优化
运维·nginx
未来之窗软件服务1 小时前
服务器运维(十一)SQLite3 php封装——东方仙盟炼气期
运维·服务器·sqlite·服务器运维·数据库驱动·东方仙盟
QT 小鲜肉2 小时前
【QT/C++】Qt样式设置之CSS知识(系统性概括)
linux·开发语言·css·c++·笔记·qt
yachuan_qiao2 小时前
专业的建筑设备监控管理系统选哪家
大数据·运维·python
Elias不吃糖2 小时前
NebulaChat 框架学习笔记:深入理解 Reactor 与多线程同步机制
linux·c++·笔记·多线程
洋哥网络科技2 小时前
centos 7.9搭建安装confluence7
linux·centos·知识图谱
李昊哲小课2 小时前
Ubuntu 24.04 安装开源WebRTC信令服务器
服务器·ubuntu·mediasoup·janus·信令服务器
LCG元3 小时前
Docker 入门实战:用10个案例带你玩转容器化
linux