在 Ubuntu 上 SFTP 连接被拒绝,通常原因集中在 服务未开启、配置错误、权限问题或防火墙/网络限制。下面按最常见原因逐一说明,并给出排查方法。
SFTP 本质是通过 SSH(22 端口) 传输的。
systemctl status ssh
或(老版本)
service ssh status
如果没运行:
sudo systemctl start ssh
sudo systemctl enable ssh
sudo apt update
sudo apt install openssh-server
sudo ufw status
如果 22 没允许:
sudo ufw allow 22
sudo ufw reload
sudo nano /etc/ssh/sshd_config
确认有以下内容(通常默认有):
Subsystem sftp /usr/lib/openssh/sftp-server
如果这行被注释或改错,SFTP 会直接失败。
修改后重启:
sudo systemctl restart ssh
SFTP 用户通常需要:
/bin/bash
或
/usr/sbin/nologin(需配置 chroot)
查看用户:
grep 用户名 /etc/passwd
检查是否有:
AllowUsers
DenyUsers
或
Match User xxx
ForceCommand internal-sftp
如果配置了:
Match User sftpuser
ChrootDirectory /sftp
ForceCommand internal-sftp
Chroot 目录权限必须满足:
chown root:root /sftp
chmod 755 /sftp
否则会直接被拒绝。
测试:
ssh user@ip
如果 SSH 都连不上,SFTP 一定失败。
| 报错 | 原因 |
|---|---|
| Connection refused | SSH 没开 / 防火墙 |
| Permission denied | 密码错 / 用户被禁 |
| broken pipe | Chroot 权限错 |
| subsystem request failed | sftp-server 路径错 |
systemctl status ssh
sudo ufw status
grep sftp /etc/ssh/sshd_config
ssh user@localhost
如果你愿意,可以贴出:
sshd_config 相关配置我可以帮你精确判断是哪一种原因。