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?
Super User is a question and answer site for computer enthusiasts and power users. It only takes a minute to sign up.Sign up to join this community
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@example.com tar xzf - -C ~/saved_tree
to send it over, and
ssh firstname.lastname@example.org "tar czf - ~/saved_tree" | tar xzvf - -C ./
to get it back.
Edit: Are you positive you are entering in a valid path in the scp command? For example:
scp test.txt email@example.com
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 firstname.lastname@example.org:~/
Send a file:
scp /path/to/local/file email@example.com:/path/to/remote/directory
Get a file:
scp firstname.lastname@example.org:/path/to/remote/file /path/to/local/directory
Send a file from my Desktop to my home folder on a remote server:
scp ~/Desktop/myfile.txt email@example.com:~/
~ is a shortcut for your home directory... e.g., /home/
Send a file to the the webroot:
scp ~/Documents/working/index.html firstname.lastname@example.org:/var/www/index.html
In this example, the user john_doe would need write privileges on the remote /var/www directory.
On some hosts they incorrectly source
.bash_profile for non-interactive logins like scp. Messages that get printed to the terminal can possibly cause
scp to not function correctly. If you have messages in your
.bash_profile this can be the cause.
To still have your login messages, banner, etc. display on interactive logins and still be able to use
scp via a non-interactive login add the following before any message that would print out in your
# ********** If not running interactively, don't do anything more! *********** [ -z "$PS1" ] && return
Alternative code is:
[[ $- == *i* ]] || return
And another alternative code:
case $- in *i*) ;; *) return;; esac
Which I believe is the longer version of the first alternative code. I have found on some hosts the first code does not work correctly but the second one does.
During a non-interactive scp login it will abort further execution of .bash_profile and allow scp to work, but will display your login messages when you login via ssh.
Note: This can also be used in your
.bashrc file if you source it from
.bash_profile (for $PATH) so only part of it is sourced during non-interactive logins.
This does not answer the question directly, but might be helpful for folks like me, looking for a solution with a freezing scp when transferring files between 2 remote hosts.
scp hangs because of messages from ssh, it could helpt to surpress them:
scp -o "StrictHostKeyChecking no"
and / or
From the scp man:
-B Selects batch mode (prevents asking for passwords or passphrases).
-o ssh_option Can be used to pass options to ssh in the format used in there is no separate scp command-line flag. For full details of the options listed below, and their possible values, see ssh_config(5).
In my case that seemed to help, but did not solve the whole issue. We could not find out why scp hangs when transferring from remote to remote. It hung in the middle of the file. 9 times it worked, attempt number 10 did not. We suspected it could be that it hangs when our VPN connection gets a traffic spike for a moment and then scp does not recover. It really just hangs forever and does not even give an error message.
However, I gave up and switched to sftp. This is reasonably faster, as it uses a direct connection between the remote hosts. You have to enable
Host example.com AgentForward yes
in the ~/.shh/config file of the machine that is running the script though. Of course this is only a solution if the remote machines are both within your trusted network.
When troubleshooting ssh/scp, it's a good idea to add the -v option for verbose output. You may also want to specific the user on the remote server. Lastly, it is sometimes helpful to copy the file to a temporary place such as /tmp where you know you have write permissions. For example, to send a hello world file to the remote server with verbose on, the syntax would be:
scp -v hello_world.txt username@server-uri:/tmp
Replace username with a user that exists on that system and of course replace the server-uri with the actual server FQDN or IP Address.