Linux SFTP 错误“收到的消息太长”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8250379/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 02:32:17  来源:igfitidea点击:

SFTP error "Received message too long"

linuxsftp

提问by Nicholas Zhang

I recently tried to using sftp to access my linux box where I implement a simple shell of my own. And I set the users except root to use mine shell in default(by editing /etc/passwd file). Then problem arise, once I tried to access through sftp, I will receive a message saying "Received message too long:", I searched for the solutions and one solution is to change the default shell for this user back to normal bash shell. I tried so and it worked, the problem is that is there a way that I can still using my own shell and also allow sftp to go through? Please answer me with more details like which file I should go editing, etc. Thanks in advance:)

我最近尝试使用 sftp 来访问我的 linux box,在那里我实现了一个我自己的简单 shell。我将除 root 之外的用户设置为默认使用我的 shell(通过编辑 /etc/passwd 文件)。然后问题出现了,一旦我尝试通过 sftp 访问,我会收到一条消息说“收到的消息太长:”,我搜索了解决方案,一个解决方案是将该用户的默认 shell 更改回正常的 bash shell。我试过了,它奏效了,问题是有没有办法让我仍然可以使用我自己的 shell 并允许 sftp 通过?请回答我更多详细信息,例如我应该编辑哪个文件等。提前致谢:)

采纳答案by salva

Configure your server to use the internal sftp server adding the following directive to /etc/ssh/sshd_config:

配置您的服务器以使用内部 sftp 服务器,将以下指令添加到/etc/ssh/sshd_config

Subsystem sftp internal-sftp

That way, it will not use the user shell to launch the sftp server program.

这样,它就不会使用用户 shell 来启动 sftp 服务器程序。

回答by Kenster

"Received message too long" means that your SFTP client received bad data from the SFTP server. The typical reason is that the shell startup scripts on the server (.bashrc, .profile, .cshrc, etc.) are producing some output, and your SFTP client is trying to parse that output as an SFTP message. You can check this by running the command:

“Received message too long”表示您的 SFTP 客户端从 SFTP 服务器接收到错误数据。典型的原因是服务器上的 shell 启动脚本(.bashrc、.profile、.cshrc 等)正在生成一些输出,而您的 SFTP 客户端正在尝试将该输出解析为 SFTP 消息。您可以通过运行以下命令来检查这一点:

ssh user@remote 'echo hello'

If this produces any output other than the "hello", then that output would probably prevent SFTP or SCP from working properly.

如果这会产生除“hello”之外的任何输出,那么该输出可能会阻止 SFTP 或 SCP 正常工作。

As in salva's answer, you can avoid this by setting the SSH server to use internal-sftpfor SFTP sessions. This avoids launching your shell for SFTP sessions. This won't help with SCP.

正如在 Salva 的回答中一样,您可以通过将 SSH 服务器设置为对 SFTP 会话使用internal-sftp来避免这种情况。这避免了为 SFTP 会话启动 shell。这对SCP没有帮助。

The other ways to fix this is to go through your shell startup commands, figure out what is producing the output, and prevent that from happening during non-interactive SSH sessions. One tip is to test for a TTY before running commands which produce output:

解决此问题的另一种方法是检查您的 shell 启动命令,找出产生输出的原因,并防止在非交互式 SSH 会话期间发生这种情况。一个技巧是在运行产生输出的命令之前测试 TTY:

if [ -t 1 ]; then
    # standard output is a TTY
    ...
fi