The "Received message too long" error in SFTP occurs when shell startup scripts output text that corrupts the SFTP protocol stream. The error number is actually the first four bytes of the output interpreted as a packet size.
SFTP communicates using a binary protocol where the first four bytes of each message indicate the packet size. When shell startup files (.bashrc, .bash_profile, etc.) output text during SSH login, this text is interpreted as a packet size value. Since the text is not a valid packet, the resulting number appears extremely large, triggering the "message too long" error. The numeric value shown (like 1416128883) is the ASCII representation of the output text (like "Hi!!" in this case).
Run this command to check if your remote SSH session is producing unexpected output:
ssh user@host /usr/bin/trueIf you see any output (text, spaces, or characters), your shell startup scripts are producing unwanted output. A clean connection should produce no output at all.
SSH executes startup files in order. Check each file for output commands:
cat ~/.bashrc
cat ~/.bash_profile
cat ~/.profile
cat /etc/profile
cat /etc/bash.bashrcLook for statements like echo, printf, fortune, cat, motd, banner, or any other commands that produce console output. These should not be in files executed during non-interactive sessions.
Modify your shell startup files to only run output commands in interactive sessions. Add this check before any echo or output commands:
# In ~/.bashrc or ~/.bash_profile
if [ -t 1 ]; then
# Only run these in interactive terminals
echo "Welcome to my server"
fortune
fiThe [ -t 1 ] test checks if file descriptor 1 (stdout) is connected to a terminal. It's false for non-interactive SFTP sessions.
For commands that shouldn't run at all in startup files, remove them entirely or move them to ~/.bash_profile (which only runs for login shells):
Move from .bashrc to .bash_profile:
- Commands that should run once per login
- Welcome messages or startup scripts
- Environment setup that only needs to run once
Remove entirely:
- Debug print statements
- Temporary diagnostic output
- Fortune cookies or other non-essential output
After modifying your startup files, test again:
ssh user@host /usr/bin/trueYou should see no output. Then test SFTP:
sftp user@hostThe connection should succeed without errors.
If modifying startup files isn't feasible, system administrators can configure OpenSSH to use the internal SFTP server, which doesn't process shell startup files:
Edit /etc/ssh/sshd_config:
Subsystem sftp internal-sftpThen restart SSH:
sudo systemctl restart ssh
# or
sudo service sshd restartThis forces SFTP to use OpenSSH's built-in server instead of forking a shell, completely bypassing startup file issues.
The numeric error value can be decoded to understand what text was output. For example, the hex value 1416128883 converts from the ASCII characters "Hi!!" The error occurs because SFTP uses a binary protocol where the first 4 bytes of every message encode the packet length as a 32-bit integer. When shell output appears instead, these bytes are interpreted as a wildly large packet size, exceeding the typical 256KB maximum for OpenSSH clients or 34KB minimum per spec.
On Windows with PowerShell SSH, the issue often stems from profile scripts being executed despite the -NoProfile flag in sshd_config. Check your PowerShell profile and ensure no output statements are present. For large directory listings in SFTP, some clients limit directory entries per response (OpenSSH limits to 100 entries), which can cause similar errors when directories contain thousands of files.
Load key "/home/user/.ssh/id_rsa": invalid format
How to fix 'Load key invalid format' in SSH
Bad owner or permissions on /home/user/.ssh/config
How to fix "Bad owner or permissions on .ssh/config" in SSH
Error connecting to agent: Connection refused
How to fix "Error connecting to agent: Connection refused" in SSH
Connection closed by UNKNOWN port 65535
How to fix 'Connection closed by UNKNOWN port 65535' in SSH
Offending ECDSA key in /home/user/.ssh/known_hosts:line
How to fix "Offending ECDSA key in known_hosts" in SSH