SFTP "Received message too long"
Why sftp says "Received message too long" when SSH works, what the remote shell is doing wrong, and how to fix it or work around it with scp -O.
Received message too long 1466264675
Ensure the remote shell produces no output for non-interactive sessions.
SSH login works, but SFTP fails at the start. The second line is the real hint: something printed text on the connection before the SFTP server started talking.
Why it happens
SFTP runs over an SSH channel as a subsystem. The server sends and receives binary packets, and each packet starts with a 4-byte number for its length. The OpenSSH client limits a packet to 256 KiB (262144 bytes). If the number it reads is larger, it stops with this message.
When something prints text first, for example a line in the account's shell startup file, the client reads the first four characters of that text as the length. Four ordinary letters make a number of over a billion. For instance, "Welc" read as a number is 1466264675.
The number in the message is not a real length. It is the first four bytes the server sent, read as a number.
What prints the text
The shell is started for the SFTP connection, and its startup files can print in a non-interactive session. Typical causes:
echoorcatlines in~/.bashrc,~/.profile,~/.bash_profile,~/.cshrcor similar files.- Programs started there that print a message, such as a tool that shows a banner or status.
- A command in the file that fails and prints an error.
Fixes
1. Make the startup file quiet when there is no terminal. For a POSIX shell, put output only in an interactive session:
case $- in
*i*) echo "Welcome" ;;
esac
The special parameter $- lists shell options, and i means interactive. Put any echo and similar lines inside the test, or return from the file early.
2. Find the line that prints. Run:
ssh [email protected] true
If any output appears, a startup file prints it. Remove or guard that line.
3. Ask the administrator if the shell startup is set by the host. Give them the line above, and the exact message.
Workaround: scp with the old protocol
If you cannot change the host, scp -O copies with the legacy SCP protocol instead of SFTP. The scp manual says the legacy protocol may be needed for servers that do not implement SFTP. It still needs a quiet shell, because the shell runs the scp command on the host. If the host has no SFTP subsystem, this is the way to copy; see scp "subsystem request failed on channel 0" and "scp: Connection closed".
Server side
The server decides the subsystem in sshd_config: Subsystem sftp sftp-server, or internal-sftp, which runs in the sshd process. The sshd_config manual says internal-sftp is an in-process SFTP server.
Related
- SFTP
- SSH problems
- SSH error messages, what each one means
- SFTP chroot: bad ownership or modes for chroot directory
- scp "subsystem request failed on channel 0" and "scp: Connection closed"
- Fix "mesg: ttyname failed: Inappropriate ioctl for device" over SSH, sudo -i or su -
- Turn off the SSH login message: motd, "Last login" and the pre-login banner