温馨提示×

linux sftp常见错误有哪些

小樊
44
2026-09-17 04:45:38
栏目: 智能运维

在 Linux 中使用 SFTP(SSH File Transfer Protocol) 时,常见错误通常与 权限、配置、网络、认证 有关。下面按 错误类型 + 常见现象 + 原因 + 解决方法 整理:


一、连接类错误

1. Connection refused

connect to host xxx port 22: Connection refused

原因

  • SFTP 服务(sshd)未启动
  • SSH 端口不是 22
  • 防火墙拦截

解决

systemctl status sshd
systemctl start sshd

检查端口:

ss -tlnp | grep ssh

防火墙:

firewall-cmd --list-all
# 或
iptables -L

2. Connection timed out

Connection timed out

原因

  • 网络不通
  • 安全组 / 防火墙未放行
  • IP 被封

解决

ping ip
telnet ip 22

3. No route to host

原因

  • 目标主机不存在
  • 网络中断

二、认证类错误

4. Permission denied (publickey)

Permission denied (publickey).

原因

  • 未使用正确私钥
  • 公钥未写入 ~/.ssh/authorized_keys
  • 权限错误

解决

chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys

使用私钥连接:

sftp -i key.pem user@host

5. Permission denied (password)

原因

  • 密码错误
  • 禁止密码登录

检查:

grep PasswordAuthentication /etc/ssh/sshd_config

6. Too many authentication failures

原因

  • 尝试密钥过多

解决:

sftp -o IdentitiesOnly=yes -i key.pem user@host

三、权限与目录错误

7. Couldn’t read: Permission denied

原因

  • 对文件/目录无读权限

8. put: Permission denied

原因

  • 目标目录不可写
  • 用户不是目录属主

解决:

chown user:user /data
chmod 755 /data

9. sftp chroot 报错

fatal: bad ownership or modes for chroot directory

原因

  • chroot 目录属主不是 root
  • 权限不是 755

解决:

chown root:root /sftp
chmod 755 /sftp

四、SFTP 服务配置错误

10. Subsystem sftp not found

subsystem request failed on channel 0

原因

  • sshd_config 中 sftp 配置错误

正确示例:

Subsystem sftp internal-sftp

重启:

systemctl restart sshd

11. This service allows sftp connections only

This service allows sftp connections only

原因

  • 用户被限制只能 SFTP,不能 SSH

五、常见客户端错误

12. Host key verification failed

原因

  • 主机指纹变化

解决:

ssh-keygen -R host

13. Remote host identification has changed

同上


14. Broken pipe

原因

  • 网络不稳定
  • 超时断开

解决:

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

如果你愿意,可以告诉我:

  • 具体报错信息
  • 使用方式(命令行 / 工具)
  • 是服务器还是客户端问题

我可以帮你精准定位。

0