Ubuntu 上 SFTP 连接失败的原因有很多,常见情况和排查方法如下:
SFTP 依赖 ssh 服务。
检查是否安装并运行:
systemctl status ssh
若未运行:
sudo apt update
sudo apt install openssh-server
sudo systemctl enable --now ssh
sudo ufw status
若开启且未允许 SSH:
sudo ufw allow ssh
# 或
sudo ufw allow 22/tcp
云服务器还要检查 安全组 / 防火墙规则。
cat /etc/passwd | grep 用户名
sudo passwd 用户名
检查:
grep 用户名 /etc/passwd
shell 应为:
/bin/bash
若为 /usr/sbin/nologin 或 /bin/false,SFTP(交互式)会失败。
编辑:
sudo nano /etc/ssh/sshd_config
确保有:
Subsystem sftp internal-sftp
若做了 chroot,需确认路径权限正确(根目录必须属 root,且 755)。
修改后重启:
sudo systemctl restart ssh
如果配置中有:
PasswordAuthentication no
则密码 SFTP 会失败,需要使用私钥。
连接时需指定:
sftp -P 端口号 user@host
Connection refused → 服务未开 / 端口错Permission denied → 账号 / 密钥问题Connection timed out → 网络 / 防火墙broken pipe → chroot 配置错误ping 主机IP
ssh user@host
systemctl status ssh
sudo journalctl -u ssh
如果你愿意,可以把 具体报错信息(客户端提示或 /var/log/auth.log)发出来,我可以精确判断是哪一类问题。