在 CentOS 上给 SFTP 限速,常见、稳定的做法有两种:基于 SSH 内置限制(推荐) 和 基于 tc 流量控制。下面分别说明。
SFTP 基于 SSH,可通过 sshd_config 限制 单个连接带宽。
编辑配置文件:
vim /etc/ssh/sshd_config
添加或修改:
Match User sftpuser
ForceCommand internal-sftp
ChrootDirectory /data/sftp
# 限制上传/下载速度(单位:字节/秒)
# 例如限制 1MB/s = 1048576
# 旧版本 SSH 不支持,CentOS 7+ 一般支持
⚠️ 注意:
OpenSSH 本身不直接支持限速参数(如 BandwidthLimit),所以单纯 sshd_config 并不能限速。
✅ 正确方式:通过 rsync / scp / sftp 客户端限速,或结合 tc
tc(Linux 流量控制,最可靠)适合:限制整个网卡 / 某个 IP / 某个端口的 SFTP 速度
ip addr
假设是 eth0
# 清除旧规则
tc qdisc del dev eth0 root
# 创建根队列
tc qdisc add dev eth0 root handle 1: htb default 30
# 创建总带宽
tc class add dev eth0 parent 1: classid 1:1 htb rate 1000mbit
# 创建 SFTP 限速类(1Mbps)
tc class add dev eth0 parent 1:1 classid 1:30 htb rate 1mbit ceil 1mbit
# 使用 iptables 标记 SFTP 流量
iptables -t mangle -A OUTPUT -p tcp --sport 22 -j MARK --set-mark 30
# 将标记流量绑定到限速类
tc filter add dev eth0 parent 1: protocol ip handle 30 fw flowid 1:30
tc -s qdisc show dev eth0
适合多用户服务器:
iptables 按源 IP 标记tc 限速systemd + cgroups 限制 IO如果你能控制客户端:
# scp 限速(单位 Kbit/s)
scp -l 8000 file user@host:/path
# rsync 限速(KB/s)
rsync --bwlimit=1000 file user@host:/path
| 场景 | 推荐方式 |
|---|---|
| 临时限速 | 客户端 --bwlimit |
| 服务器统一限速 | tc + iptables |
| 单用户 SFTP | tc 按 IP |
| 高安全隔离 | chroot + tc |
如果你告诉我:
我可以直接给你一份可复制的配置脚本。