Linux VPS 限速神器:WonderShaper 实战指南

869 字
4 分钟
Linux VPS 限速神器:WonderShaper 实战指南

**说明:**最近看有人问博主如何限制服务器的上传带宽,所以就分享下此前经常用的一个限速脚本WonderShaper,原理的话,网上比较详细的解释是WonderShaper使用tc来定义流量调整命令,使用QoS来处理特定的网络接口。外发流量通过放在不同优先级的队列中,达到限制传出流量速率的目的;而传入流量通过丢包的方式来达到速率限制的目的。用起来挺方便的,有需求的可以了解下。

Linux_WonderShaper.jpg
Linux_WonderShaper.jpg

使用#

Github地址: https://github.com/magnific0/wondershaper

安装的话是可以直接用软件包安装,不过版本都不太新,所以这里直接从Github拉取最新源码。

1、安装依赖

#Debian/Ubuntu系统
apt install -y make git
#CentOS系统
yum install make git -y

2、安装WonderShaper

git clone https://github.com/magnific0/wondershaper.git
cd wondershaper
make install

3、设置限速

#使用命令
USAGE: wondershaper [-hcs] [-a <adapter>] [-d <rate>] [-u <rate>]
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/conf.d/wondershaper.conf
-c Clear the limits from adapter
-s Show the current status of adapter
-v Show the current version

首先查看网卡:

#这里提供三个可以查看网卡的命令,建议使用第一个
ifconfig
ip addr
route

比如我要限制eth0网卡速度,使用命令:

#限制上传带宽为10M
wondershaper -a eth0 -u 10240
#限制下载带宽为10M
wondershaper -a eth0 -d 10240
#限制上传和上传均10M
wondershaper -a eth0 -d 10240 -u 10240
#清除网卡限速规则
wondershaper -c -a eth0

然后我们可以测一下速,使用命令:

wget -O speedtest-cli https://raw.githubusercontent.com/sivel/speedtest-cli/master/speedtest.py
chmod +x speedtest-cli
./speedtest-cli

这是没限速前的测速:

1.jpg
1.jpg

上传/下载限速10M后的测速:

2.jpg
2.jpg

开机自启#

一般设置限速规则后,服务器重启的话,限速规则会自动失效,所以这里需要稍微设置一下,使其开机也自动生效,这里就说2种方法。

1、使用rc.local 这是最简单的设置自启方法,不过Debian 9Ubuntu 17+是没有rc.local文件的,所以使用该系统的需要先配置一下。

1、添加rc-local.service,以下为一整条命令,一起复制运行
cat > /etc/systemd/system/rc-local.service <<EOF
[Unit]
Description=/etc/rc.local
ConditionPathExists=/etc/rc.local
[Service]
Type=forking
ExecStart=/etc/rc.local start
TimeoutSec=0
StandardOutput=tty
RemainAfterExit=yes
SysVStartPriority=99
[Install]
WantedBy=multi-user.target
EOF
2、新建rc-local文件,以下为一整条命令,一起复制运行
cat > /etc/rc.local <<EOF
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
EOF
3、添加权限并设置开机自启
chmod +x /etc/rc.local
systemctl start rc-local
systemctl enable rc-local

最后将启动命令加入rc.local文件,使用命令:

#CentOS 7系统
echo "wondershaper -a eth0 -d 10240 -u 10240" >> /etc/rc.d/rc.local
chmod +x /etc/rc.d/rc.local
#CentOS 6、Debian、Ubuntu系统
echo "wondershaper -a eth0 -d 10240 -u 10240" >> /etc/rc.local
chmod +x /etc/rc.local

这里限速命令自行修改。

2、使用Systemd 由于安装的时候,Systemd配置文件也给你了,所以就方便使用了,不过该方法只适用于CentOS 7Debian 8+Ubuntu 16+等。

由于启动时,默认调用的配置文件为/etc/conf.d/wondershaper.conf,所以先编辑该文件:

nano /etc/conf.d/wondershaper.conf

大致如下:

[wondershaper]
# Adapter
IFACE="eth0"
# Download rate in Kbps
DSPEED="10240"
# Upload rate in Kbps
USPEED="10240"

参数依次为网卡、下载、上传限制,修改好了后,使用Ctrl+xy保存退出。

再启动并开机自启:

systemctl start wondershaper
systemctl enable wondershaper

支持与分享

如果这篇文章对你有帮助,欢迎分享给更多人或赞助支持!

赞助
Linux VPS 限速神器:WonderShaper 实战指南
https://blog.moewah.com/posts/1650/
作者
MoeWah
发布于
2019-03-11
许可协议
CC BY-NC-SA 4.0
相关文章 智能推荐
1
Linux病毒扫描工具ClamAV配置使用教程
虚拟化与运维 如何在Linux系统中安装和配置ClamAV进行病毒扫描?本文详细讲解ClamAV的安装、数据库更新及命令行使用方法,提供从基础配置到高级扫描技巧的完整解决方案,助你有效防御恶意软件。
2
GhostVM:给 AI Agent 一个完整的 Mac,而不是容器!
虚拟化与运维 介绍 GhostVM——基于 Apple Virtualization.framework 的 macOS 虚拟机工具。支持近原生性能、自包含 bundle、秒级克隆,提供完整的 CLI (vmctl) 覆盖 GUI 功能,适合 AI agent 隔离环境和多项目开发者。
3
纳瓦尔的人生智慧:关于财富、幸福与自由的 18 条原则
认知与成长 硅谷投资人纳瓦尔·拉维坎特的人生洞见浓缩:从财富创造、幸福修炼到心智模型,18 条可执行原则帮你在长期主义中找到自由。
4
AI搜索吃掉一半流量?2026 你必须调整的内容推广方法
增长与SEO 2026年AI搜索覆盖48%查询,传统社群转发式推广失效。梳理五个按意图优先级排列的分发渠道——GEO优化、Reddit投放、战略合作、员工倡导、直接外联——附带推广时间线和内容再创作框架。
5
有了 tmux 还需要 herdr 吗?一个给 AI Agent 用的「终端管家」
AI实验室 herdr 是一个终端原生的 Agent 多路复用器,专为同时运行多个 AI Agent 的开发者设计。它解决了 Agent 管理混乱、会话丢失、远程协作等痛点,不替换终端、不依赖 Electron。本文从实际使用体验出发,带你了解它的核心功能和上手方法。
随机文章 随机推荐

评论区

Profile Image of the Author
MoeWah
Hello, I'm MoeWah.
专题文章
分类
站点统计
文章
198
分类
9
标签
434
总字数
373,761
运行时长
0
最后活动
0 天前

目录