scp returns "unexpected <newline>" error

I am trying to scp a file from a server to my local machine, but it is giving me this error:

protocol error: unexpected <newline>

This is my syntax:

scp user@server:/path/to/file .

It did not work on this server, but then I tried the same command on my other server, so I can only assume that it is something wrong with my server and not the syntax of the scp command.

Any ideas?


Solution 1:

One of your login scripts (.bashrc/.cshrc/etc.) is outputting data to the terminal when it shouldn't be. This is causing scp to error when it is connecting and getting ready to copy as it starts receiving extra data it doesn't expect. Remove output that is generated here.

You can check if your terminal is interactive and only output text by using the following code in a bashrc. Something equivalent exists for other shells as well:

if shopt -q login_shell; then
    [any code that outputs text here]
fi

Solution 2:

Found the solution. My .bashrc file and .bash_logout were set to echo out information to me whenever I log in/out. Apparently that echoed new line was giving scp some issues. I commented out my echo statements and that did the trick!