Wondershaper

Wondershaper 使用教程 #

Wondershaper 是一个用于 Linux 系统的简单网络流量整形(Traffic Shaping)脚本。它底层基于 tc (Traffic Control) 命令,但封装得更易用,主要用于限制特定网卡的上传和下载带宽

以下是详细的使用教程:


1. 安装 Wondershaper #

大多数 Linux 发行版的软件源中都包含 wondershaper。

Debian / Ubuntu / Kali #

sudo apt update
sudo apt install wondershaper

CentOS / RHEL / Fedora #

# Fedora
sudo dnf install wondershaper

# CentOS (可能需要 EPEL 源)
sudo yum install wondershaper

Arch Linux / Manjaro #

sudo pacman -S wondershaper

手动安装 (GitHub) #

如果软件源中没有,可以从 GitHub 下载脚本:

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

2. 查找网络接口名称 #

在使用之前,你需要知道你要限制哪个网卡(例如 eth0, ens33, wlan0 等)。

ip addr
# 或者
ifconfig

找到你正在使用的网络接口名称(假设为 eth0)。


3. 基本用法 #

语法格式 #

sudo wondershaper [网络接口] [下载速度] [上传速度]

注意: 速度单位通常是 Kbps (千比特每秒),而不是 KB/s (千字节每秒)。

  • 1 Byte = 8 bits
  • 1024 Kbps = 128 KB/s

示例命令 #

  1. 限制 eth0 网卡:

    • 下载限速:1024 Kbps (约 128 KB/s)
    • 上传限速:512 Kbps (约 64 KB/s)
    sudo wondershaper eth0 1024 512
    
  2. 限制无线网卡 wlan0:

    • 下载限速:2 Mbps (2048 Kbps)
    • 上传限速:1 Mbps (1024 Kbps)
    sudo wondershaper wlan0 2048 1024
    
  3. 查看当前限速状态:

    sudo wondershaper status eth0
    

    (注:部分旧版本可能不支持 status 参数,直接运行 wondershaper 可查看帮助)

  4. 清除限速(恢复原状):

    sudo wondershaper clear eth0
    
  5. 查看帮助信息:

    wondershaper -h
    

4. 关于速度单位的重要说明 #

这是用户最容易混淆的地方。Wondershaper 默认接受的参数单位是 Kbps (Kilobits per second)

你设定的值 (Kbps) 实际下载速度 (KB/s) 适用场景
512 64 KB/s 极低带宽限制
1024 128 KB/s 1 Mbps 宽带
2048 256 KB/s 2 Mbps 宽带
8192 1024 KB/s (1 MB/s) 8 Mbps 宽带
10240 1280 KB/s (1.25 MB/s) 10 Mbps 宽带

计算公式: 设定值 ÷ 8 = 实际每秒下载千字节数


5. 设置开机自启 (持久化) #

Wondershaper 的设置是临时的,重启网络或系统后会失效。如果需要永久生效,建议创建一个 systemd 服务。

  1. 创建服务文件:

    sudo nano /etc/systemd/system/wondershaper.service
    
  2. 填入以下内容 (以限制 eth0 为例):

    [Unit]
    Description=Limit bandwidth with wondershaper
    After=network.target
    
    [Service]
    Type=oneshot
    ExecStart=/usr/sbin/wondershaper eth0 1024 512
    ExecStop=/usr/sbin/wondershaper clear eth0
    RemainAfterExit=yes
    
    [Install]
    WantedBy=multi-user.target
    

    (注意:/usr/sbin/wondershaper 是脚本路径,可用 which wondershaper 确认)

  3. 启用服务:

    sudo systemctl daemon-reload
    sudo systemctl enable wondershaper.service
    sudo systemctl start wondershaper.service
    

6. 常见问题与注意事项 #

  1. 权限问题: 所有设置限速的命令都必须使用 sudo 或 root 权限执行。
  2. 内网接口: 不要对 lo (本地回环接口) 进行限速,否则可能导致本地服务异常。
  3. 版本差异: 存在 wondershaperwondershaper2 等不同版本,参数可能略有不同,请以 wondershaper -h 显示的帮助信息为准。
  4. 效果验证: 设置完成后,建议使用 speedtest 或下载大文件来验证限速是否生效。
  5. 冲突: 如果系统中已经使用了复杂的 tc 规则或其他防火墙流量控制,wondershaper 可能会与之冲突。

希望这份教程能帮助你顺利使用 Wondershaper!如果有具体报错,可以提供详细信息以便进一步排查。