在 CentOS 上排查 SFTP 故障,可以按“由外到内、由网络到配置”的思路逐步定位。下面是一套实用排查流程。
先明确:
systemctl status sshd
若未运行:
systemctl start sshd
systemctl enable sshd
ss -lntp | grep :22
# 或
netstat -lntp | grep sshd
在服务器本机测试:
sftp localhost
或
ssh user@localhost
firewall-cmd --list-all
确保 22 端口允许:
firefirewall-cmd --add-service=ssh --permanent
firewall-cmd --reload
getenforce
如果是 Enforcing,可临时验证:
setenforce 0
若恢复正常,说明是 SELinux 问题。
查看相关日志:
tail -f /var/log/audit/audit.log
vim /etc/ssh/sshd_config
重点检查:
Subsystem sftp internal-sftp
# 或
Subsystem sftp /usr/libexec/openssh/sftp-server
PasswordAuthentication yes
PubkeyAuthentication yes
Match User sftpuser
ChrootDirectory /data/sftp
ForceCommand internal-sftp
修改后重启:
systemctl restart sshd
id sftpuser
若使用 ChrootDirectory:
例如:
chown root:root /data/sftp
chmod 755 /data/sftp
mkdir /data/sftp/upload
chown sftpuser:sftpuser /data/sftp/upload
tail -f /var/log/secure
常见错误:
Permission deniedfatal: bad ownership or modes for chrootAuthentication failuresftp -v user@host
# 或
ssh -vvv user@host
| 现象 | 可能原因 |
|---|---|
| 连不上 | 防火墙 / sshd 未启动 |
| 能 SSH 不能 SFTP | sftp subsystem 配置错误 |
| 登录后立刻断开 | Chroot 权限错误 |
| 认证失败 | 密码 / 公钥 / PAM |
| SELinux 报警 | 上下文不对 |
如果你愿意,可以把:
发出来,我可以帮你精准定位。