權限是SFTP文件傳輸中最常見的錯誤來源。需確保:
chmod 755 /path/to/remote/directory
,避免使用777
(安全風險))。chroot
限制用戶訪問,主目錄需歸root
所有且權限為755
(如sudo chown root:root /sftp_root && sudo chmod 755 /sftp_root
)。chown
命令將目錄所有者設為用戶(如sudo chown sftpuser:sftp_users /sftp_root/sftpuser
)。sudo systemctl status ssh
,若未運行則sudo systemctl start ssh
)。/etc/ssh/sshd_config
,確保以下配置正確:
Subsystem sftp internal-sftp
(啟用SFTP子系統);Match Group sftp_users\n ChrootDirectory /sftp_root\n ForceCommand internal-sftp\n AllowTcpForwarding no\n X11Forwarding no
(替換為實際組名和目錄);sudo systemctl restart ssh
)。ping server_ip
)。sudo ufw allow 22
開放防火墻端口(若使用云服務器,需檢查安全組設置)。ssh -v user@host
調試連接)。df -h
查看服務器磁盤使用情況,若空間不足,清理無用文件(如sudo apt autoremove
或刪除舊日志)。ls /path/to/remote/directory
),避免特殊字符或空格。若以上方法無效,通過日志獲取詳細錯誤信息:
sudo tail -f /var/log/auth.log
實時查看SSH/SFTP日志,根據日志提示(如“Permission denied”“No such file or directory”)針對性解決。使用命令行工具排查客戶端問題:
sftp username@hostname
,輸入密碼后嘗試ls
(查看遠程目錄)、put local_file remote_path
(上傳文件)、get remote_file local_path
(下載文件),觀察具體錯誤信息(如“Connection refused”“Not a directory”)。sftp_users
),用sudo usermod -aG sftp username
添加組,注銷后重新登錄。sudo setsebool -P sftp_home_dir on
)并調整安全上下文(sudo chcon -Rv --reference=/var/www/html /home/username/uploads
)。