在 Linux 中使用 SFTP(SSH File Transfer Protocol) 时,常见错误通常与 权限、配置、网络、认证 有关。下面按 错误类型 + 常见现象 + 原因 + 解决方法 整理:
connect to host xxx port 22: Connection refused
原因
解决
systemctl status sshd
systemctl start sshd
检查端口:
ss -tlnp | grep ssh
防火墙:
firewall-cmd --list-all
# 或
iptables -L
Connection timed out
原因
解决
ping ip
telnet ip 22
原因
Permission denied (publickey).
原因
~/.ssh/authorized_keys解决
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
使用私钥连接:
sftp -i key.pem user@host
原因
检查:
grep PasswordAuthentication /etc/ssh/sshd_config
原因
解决:
sftp -o IdentitiesOnly=yes -i key.pem user@host
原因
原因
解决:
chown user:user /data
chmod 755 /data
fatal: bad ownership or modes for chroot directory
原因
解决:
chown root:root /sftp
chmod 755 /sftp
subsystem request failed on channel 0
原因
正确示例:
Subsystem sftp internal-sftp
重启:
systemctl restart sshd
This service allows sftp connections only
原因
原因
解决:
ssh-keygen -R host
同上
原因
解决:
sftp -o ServerAliveInterval=60 user@host
# 查看 ssh 服务
systemctl status sshd
# 测试 ssh
ssh -v user@host
# 查看端口
ss -tlnp | grep :22
# 查看日志
journalctl -u sshd
/var/log/secure
如果你愿意,可以告诉我:
我可以帮你精准定位。