CentOS SFTP故障排查思路
一 快速定位流程
ping <服务器IP>;nc -vz <IP> 22 或 telnet <IP> 22。systemctl status sshd、systemctl start sshd、systemctl enable sshd。ss -tnlp | grep :22。tail -f /var/log/secure、journalctl -u sshd -f。firewall-cmd --list-all、firewall-cmd --permanent --add-service=ssh --permanent && firewall-cmd --reload。-v/-vv/-vvv 获取详细握手与认证过程。sftp -v user@host 或 ssh -v user@host。二 常见症状与对应处理
| 症状 | 重点检查 | 快速修复 |
|---|---|---|
| 连接超时/端口不通 | 监听、云安全组/本机防火墙、网络路径 | `ss -tnlp |
| 认证失败/拒绝 | 用户名/口令/密钥、/etc/ssh/sshd_config 限制、/var/log/secure |
核对凭据;检查 AllowUsers/AllowGroups/DenyUsers;必要时在 sshd_config 中为该用户做针对性配置并 systemctl restart sshd |
| 仅部分用户无法连接 | 用户家目录/权限、用户是否被限制登录 | 检查家目录属主与权限(如 chmod 755 /home/username);确认未被 DenyUsers//etc/ssh/sshd_config 限制 |
| 登录后立即断开或报 “Couldn’t read packet: Connection reset by peer” | ChrootDirectory 权限与属主、Subsystem sftp 配置 |
ChrootDirectory 及其上级目录属主必须为 root 且权限不超过 755;子目录如需写入,可单独授权给相应用户;确认 Subsystem sftp internal-sftp 或路径正确 |
| 目录列表很慢或闲置后断开 | 会话保活参数 | 在 /etc/ssh/sshd_config 设置:TCPKeepAlive yes、ClientAliveInterval 60、ClientAliveCountMax 3,然后 systemctl restart sshd |
| SELinux 拦截 | SELinux 状态与策略 | getenforce(如为 Enforcing 可临时 setenforce 0 验证);定位后使用 semanage/restorecon 调整策略而非长期关闭 |
| 以上症状与处理覆盖了权限、配置、网络与保活等高频问题点。 |
三 配置与权限关键点
Subsystem sftp internal-sftp。.ssh/authorized_keys 权限为 600、.ssh 为 700,属主正确。/etc/ssh/sshd_config 后执行 systemctl restart sshd 并再次观察 /var/log/secure。四 命令清单与最小复现
systemctl status sshd、ss -tnlp | grep :22tail -f /var/log/secure、journalctl -u sshd -ffirewall-cmd --list-all、firewall-cmd --permanent --add-service=ssh --permanent && firewall-cmd --reloadgetenforce、setenforce 0(仅测试)nc -vz <IP> 22、sftp -v user@host、ssh -vv user@hostsftp user@127.0.0.1 验证本机可用性;