scp returns "unexpected <newline>" error

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!

Tags:

Ssh

Scp