SSH 登录有效,但 SFTP 登录无效

SSH 登录有效,但 SFTP 登录无效

我已经看过了线,但它没有回答我的问题因为它已经被搁置了。

正如标题所述,当我使用 Putty 登录我的 VPS 时,一切都运行正常。但是当通过 SFTP 连接 FileZilla 时,我总是收到错误:(Authentication failed, cannot establish connection to the server大致翻译)。
我在 FileZilla 中使用了正确的设置,因为我 3 天前才收到此错误,以前它运行正常:通过端口 22 进行 SFTP。

这是一个iptables -L
(TL;DR:接受端口 20、21 和 22 上的所有进出,以及端口 1024+ 上的被动入站连接)

Chain INPUT (policy ACCEPT)
target     prot opt source               destination
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:ftp ctstate ESTABLISHED /* Allow ftp connections on port 21 */
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:ftp-data ctstate RELATED,ESTABLISHED /* Allow ftp connections on port 20 */
ACCEPT     tcp  --  anywhere             anywhere             tcp spts:1024:65535 dpts:1024:65535 ctstate ESTABLISHED /* Allow passive inbound connections */
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:ssh ctstate ESTABLISHED /* Allow ftp connections on port 22 */

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:ftp ctstate NEW,ESTABLISHED /* Allow ftp connections on port 21 */
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:ftp-data ctstate ESTABLISHED /* Allow ftp connections on port 20 */
ACCEPT     tcp  --  anywhere             anywhere             tcp spts:1024:65535 dpts:1024:65535 ctstate RELATED,ESTABLISHED /* Allow passive inbound connections */
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:ssh ctstate ESTABLISHED /* Allow ftp connections on port 22 */

我确实手动设置了这一点,以防这是我的问题的根源,但什么都没有改变。

我也按照上一个帖子的建议设置了,但重启 sshd 后什么也没有改变。PasswordAuthentication yes这 是我尝试连接 FileZilla 时得到的结果:LogLevel DEBUG
/var/log/auth.log没有什么与 SFTP 登录相关。
它仅包含有关我sudo访问文件的操作。

我不知道它是否来自 FileZilla,因为auth.log没有显示与 SFTP 连接相关的任何内容,或者它来自 sshd 配置只是忽略了 SFTP 请求。
我似乎找不到任何可以帮助我的东西,你有什么建议吗?

感谢您花时间阅读本文。

答案1

查看了 iptables -L 后,我认为服务器端存在防火墙问题。

ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:ssh ctstate ESTABLISHED

ESTABLISHED 通常意味着连接活跃或已经建立

在你的 iptables 中添加此行

sudo iptables -A INPUT -p tcp --dport ssh -j ACCEPT

iptables 的简单指南我喜欢这个 这是一个解释

或者你可以尝试

sudo sh -c "iptables-save > /etc/iptables.rules"
sudo iptables -F

测试你的连接然后你可以使用以下方法恢复你的规则

iptables-restore < /etc/iptables.rules

答案2

这可能是由登录期间写入控制台的 echo 或其他命令引起的。例如,我试图为复杂的构建过程设置环境变量,并将 echo 添加到我的所有 .profile 文件中,包括.bashrc我尝试过的所有 sftp 客户端:filezilla,Beyond Compare 突然停止工作,但没有给出有用的错误消息。Beyond Compare 说:

Connection failed: Failed to establish SFTP connection (error code is 103)
Failed to establish SFTP connection (error code is 103)

最后,公司 IT 部门让我尝试 WinSCP,它给出了一条有用的错误消息:

Received too large (1701737573 B) SFTP packet. Max supported packet size is 1024000 B. 
The error is typically caused by message printed from startup script (like .profile). 
The message may start with "ente". 
Cannot initialize SFTP protocol. 
Is the host running a SFTP server?

这给了我所需的线索,删除该行后

echo 'entering .bashrc'

我的.bashrcsftp 又正常工作了

回答类似的问题也提到了这一点,以及如何检查启动脚本的输出

相关内容