scp doesn't work but ssh does

If I want to send something via scp to server:

$ scp file server:
                   _____  _____  _____
$

, then three lines are printed and file is not copied. However I can connect to server via ssh without problem:

$ ssh server

How to make scp work?


One possible cause of this type of behavior is having any message print out during the login process on server. Scp depends on ssh to provide a totally transparent encrypted tunnel between the client and the server.

Check all of the login scripts on the server, and also try using a different user. Another method of identifying the source of the error is to use the -v in the command, to trace the progress of the transaction, and see where it fails. You can use up to -vvv to increase the verbosity, if necessary. Checking the various forms of scp can also be instructive, as listed in the post by InChargeOfIT.

scp, under the hood, sets up a tunnel using ssh, and then transfers the file over that tunnel, with a ssh command on the far end to catch the file as it comes over. This is illustrated by the use of tar and ssh to copy a directory structure preserving ownership and creation times with the following commands:

  tar czf - ./* | ssh [email protected] tar xzf - -C ~/saved_tree

to send it over, and

ssh [email protected] "tar czf - ~/saved_tree" | tar xzvf - -C ./

to get it back.


Check the target user's .bashrc or equivalent file. ~/.bashrc is sourced for non-interactive logins. If there's an echo or command that outputs anything, it will break the SCP protocol.


Edit: Are you positive you are entering in a valid path in the scp command? For example:

scp test.txt [email protected]

will fail (in fact, it will just print out the command like you are seeing). In this case, you will need to provide a valid path to the remote server.. e.g., scp test.txt [email protected]:~/

Example usages:

Send a file:

scp /path/to/local/file [email protected]:/path/to/remote/directory

Get a file:

scp [email protected]:/path/to/remote/file /path/to/local/directory

Examples:

Send a file from my Desktop to my home folder on a remote server:

scp ~/Desktop/myfile.txt [email protected]:~/

Remember the ~ is a shortcut for your home directory... e.g., /home/

Send a file to the the webroot:

scp ~/Documents/working/index.html [email protected]:/var/www/index.html

In this example, the user john_doe would need write privileges on the remote /var/www directory.