简体   繁体   中英

SFTP on linux server gives error “Received message too long”

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

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

Subsystem sftp internal-sftp

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

"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:

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.

As in salva's answer, you can avoid this by setting the SSH server to use internal-sftp for SFTP sessions. This avoids launching your shell for SFTP sessions. This won't help with SCP or with other programs like git or rsync which run through ssh.

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:

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

It can be fixed by using the internal-sftp subsystem. Edit /etc/ssh/sshd_config and replace

Subsystem sftp /usr/lib/openssh/sftp-server

by

Subsystem sftp internal-sftp

Then you need to restart sshd service with this command:

/bin/systemctl restart sshd.service

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM