温馨提示×

Ubuntu FTP Server带宽限制设置

小樊
43
2025-11-15 15:14:37
栏目: 智能运维

Ubuntu FTP Server带宽限制设置

一、使用 vsftpd 内置限速(推荐)

  • 安装与启用
    • 安装:sudo apt update && sudo apt install vsftpd
    • 启用开机自启:sudo systemctl enable --now vsftpd
  • 配置限速参数(/etc/vsftpd.conf)
    • 单位说明:local_max_rate / anon_max_rate 的单位是字节/秒(B/s)。换算示例:50 KB/s = 50 × 1024 = 51200 B/s1 MB/s = 1048576 B/s
    • 常用配置示例:
      # 本地用户(系统用户)限速:100 KB/s(上传+下载均受限)
      local_max_rate=102400
      
      # 匿名用户限速:50 KB/s
      anon_max_rate=51200
      
      # 允许写(如需要上传)
      write_enable=YES
      
  • 使配置生效
    • 检查语法:sudo vsftpd -t
    • 重启服务:sudo systemctl restart vsftpd
  • 验证
    • 本机测试(示例):ftp localhost,登录后执行 get/put 大文件,配合系统监控观察速率是否接近设定值(如 50 KB/s100 KB/s)。

二、使用 ProFTPD 限速(可选)

  • 安装:sudo apt update && sudo apt install proftpd
  • 启用模块与配置(/etc/proftpd/proftpd.conf)
    • 确保加载带宽模块(常见为 mod_bandwidth.c),并设定默认限速(单位通常为字节/秒):
      <IfModule mod_bandwidth.c>
          BandwidthModule on
          DefaultUserLimit 102400   # 默认每用户 100 KB/s
      </IfModule>
      
  • 使配置生效
    • 检查语法:sudo proftpd -t
    • 重启服务:sudo systemctl restart proftpd
  • 说明
    • 可按用户、组、虚拟主机或目录进一步细化限速策略,具体请参考模块文档与配置示例。

三、进阶方式(按主机或进程限速)

  • 使用 trickle 限制客户端进程带宽(用户态,灵活)
    • 安装:sudo apt install trickle
    • 示例:限制某次 FTP 会话的上传 100 KB/s、下载 50 KB/s
      trickle -u 100 -d 50 ftp
      
    • 适用场景:临时测试、对单个客户端/进程限速;不适合长期、系统级统一策略。
  • 使用 tc 进行系统级流量整形(按接口/源IP等)
    • 示例:将主机 192.168.1.100 的出站带宽限制为 10 Mbit/s
      sudo tc qdisc add dev eth0 root handle 1: htb
      sudo tc class add dev eth0 parent 1: classid 1:1 htb rate 100mbit ceil 100mbit
      sudo tc class add dev eth0 parent 1:1 classid 1:10 htb rate 10mbit ceil 10mbit
      sudo iptables -t mangle -A OUTPUT -s 192.168.1.100 -j MARK --set-mark 10
      sudo tc filter add dev eth0 parent 1: protocol ip prio 1 handle 10 fw flowid 1:10
      
    • 提示:上述规则默认重启后丢失,需持久化(如写入网络配置脚本/服务)。

四、常见问题与建议

  • 单位换算易错:vsftpd 的 local_max_rate / anon_max_rate 为 B/s,设置 50 KB/s 请写 51200,设置 1 MB/s 请写 1048576
  • 限速方向与生效范围:上述参数对上传与下载均生效;若需区分方向或做更细粒度控制,请考虑 ProFTPD 模块tc/trickle 方案。
  • 配置生效与回滚:修改后先执行配置检查(如 vsftpd -tproftpd -t),再重启服务;变更前建议备份配置文件,便于快速回滚。
  • 持久化与排障:使用 tc 做系统级限速时请做好持久化与日志记录;测试时建议用大文件与多客户端并发,观察是否达到预期带宽上限。

0