在Debian系統中配置SFTP的帶寬限制,可以通過使用tc(Traffic Control)工具來實現。以下是一個基本的步驟指南:
首先,確保你已經安裝了iproute2包,它包含了tc工具。
sudo apt update
sudo apt install iproute2
假設你想限制SFTP用戶的帶寬為1Mbps(即1024kbps),你可以使用以下命令來創建一個簡單的帶寬限制規則。
sudo tc qdisc add dev eth0 root handle 1: htb default 30
這里eth0是你的網絡接口名稱,你需要根據實際情況替換它。
sudo tc class add dev eth0 parent 1: classid 1:1 htb rate 1024kbps ceil 1024kbps
這行命令創建了一個類,限制了帶寬為1Mbps,并且設置了上限也為1Mbps。
sudo tc filter add dev eth0 protocol ip parent 1:0 prio 1 u32 match ip dport 22 0xffff flowid 1:1
這行命令將所有發往端口22(SFTP默認端口)的流量導向我們剛剛創建的類。
你可以使用以下命令來查看當前的tc配置:
sudo tc -s qdisc ls dev eth0
sudo tc -s class ls dev eth0
sudo tc -s filter ls dev eth0
上述配置在系統重啟后會丟失。為了持久化配置,你可以將上述命令添加到系統的啟動腳本中,例如/etc/rc.local文件中。
#!/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.
tc qdisc add dev eth0 root handle 1: htb default 30
tc class add dev eth0 parent 1: classid 1:1 htb rate 1024kbps ceil 1024kbps
tc filter add dev eth0 protocol ip parent 1:0 prio 1 u32 match ip dport 22 0xffff flowid 1:1
exit 0
確保/etc/rc.local文件有執行權限:
sudo chmod +x /etc/rc.local
通過以上步驟,你應該能夠在Debian系統中成功配置SFTP的帶寬限制。