Securely transferring files between hosts with SCP

scp (or “secure copy“) is a method of securely transferring files between two hosts. Unlike wget, the files to be transferred do not need to be publicly accessible via either HTTP or FTP.

To put it another way, ssh + cp = scp.

Some caveats:

  • You must specify a target file. If you don’t, scp will fail with an error. If the target doesn’t exist, scp will create it. And if it does exist, scp will overwrite it completely without warning, so choose the name wisely.
  • If the remote connection requires any port other than 22 (the default), add it after scp with the -P flag.

I want to transfer a file from my local host to a remote host via port 2022

scp -P 2022 local/path/to/source/file user@remote_host:remote/path/to/target/file

I want to transfer a file from a remote host (via port 22) to my local host

scp user@remote_host:remote/path/to/source/file local/path/to/target/file

Note the lack of port declaration, as port 22 is the default.

I want to transfer a file from one remote host to another

scp source_user@source_host:path/to/source/file target_user@target_host:path/to/target/file