The "protocol error: unexpected <newline>" message typically occurs when there's an issue with how SCP interprets the remote server's response. This isn't a syntax problem with your command - scp user@server:/path/to/file .
is fundamentally correct.
Common culprits include:
1. Corrupted shell initialization files (.bashrc, .profile)
2. Non-standard shell configurations
3. Echoed messages during login
4. Incompatible SSH/SCP versions
First, verify your SSH connection works properly:
ssh user@server "echo test"
If you see any output before "test", that's the root cause. Try these commands:
ssh -t user@server "/bin/sh"
scp -T user@server:/path/to/file .
On the server, check your shell initialization files:
grep -r "echo" ~/.bashrc ~/.profile ~/.bash_profile
Remove any echo statements or commands that produce output. Alternatively, modify your SCP command:
scp -o "LogLevel=ERROR" user@server:/path/to/file .
For stubborn cases, try forcing the legacy SCP protocol:
scp -O user@server:/path/to/file .
Or specify a different shell:
scp -S /bin/bash user@server:/path/to/file .
When SCP fails persistently, consider these alternatives:
# Using rsync
rsync -avz -e ssh user@server:/path/to/file .
# Using SFTP
sftp user@server
get /path/to/file
When executing SCP transfers between Linux systems, the protocol error: unexpected <newline>
message typically indicates a malformed SSH/SCP handshake. The error occurs before file transfer begins, suggesting authentication succeeded but protocol negotiation failed.
From troubleshooting multiple servers, I've found these frequent culprits:
# 1. Corrupted shell initialization files on remote server
scp user@problem-server:/etc/profile . # Test if system files are readable
# 2. Strict SSH configurations
ssh -v user@server # Check for debugging output during connection
# 3. Output generation during login
scp -T user@server:/path/file . # Try disabling pseudo-terminal allocation
First verify basic SSH functionality:
ssh user@server "echo 'Test command'" > /dev/null
if [ $? -ne 0 ]; then
echo "SSH execution failed - check .bashrc/.profile"
fi
Then test raw SCP protocol:
echo "exec /usr/bin/scp -t /tmp" | ssh user@server
# Should return nothing if working properly
For production systems where you can't modify user profiles, use these workarounds:
# Method 1: Force direct shell command
scp -S ssh user@server:"/path/with spaces/file" .
# Method 2: Use sftp instead
sftp user@server <
If you have admin access, implement these permanent solutions:
# 1. Clean problematic shell files
sudo mv /etc/profile.d/motd.sh{,.bak}
# 2. Configure SSHd to skip PTY
echo "ForceCommand /usr/bin/scp" >> /etc/ssh/sshd_config
systemctl restart sshd
# 3. Create dedicated SCP user
useradd -m -s /usr/bin/scp scpuser
Remember to test changes with scp -v
for verbose output before and after modifications.