How to Use SCP to Copy Files Securely.
Learn how to copy files and directories with SCP over SSH, set a custom port, and transfer files between local and remote hosts.
scp copies files between local and remote systems over SSH. The SSH connection encrypts the transfer and uses SSH authentication.
You can copy files in either direction, copy directories, or transfer files between two remote hosts. Check the destination path before copying because an existing file with the same name may be overwritten.
Command syntax
scp [options] source destinationA remote path uses this format: user@host:/path.
Copy a local file to a remote host
$ scp file.txt user@server:/remote/directory/To give the copied file a new name:
$ scp file.txt user@server:/remote/directory/new-name.txtIf you leave off the remote directory, the file is copied to the remote user’s home directory.
Use a custom SSH port
$ scp -P 12202 file.txt user@server:/remote/directory/The port option is uppercase -P.
Copy a directory
Use -r to copy a directory and its contents:
$ scp -r /local/directory user@server:/remote/directory/Copy a remote file to your local system
$ scp user@server:/remote/file.txt /local/directory/Copy between two remote hosts
$ scp user1@host1:/files/file.txt user2@host2:/files/Current OpenSSH versions transfer remote-to-remote files through your local machine by default. Add -3 to make this explicit:
$ scp -3 user1@host1:/files/file.txt user2@host2:/files/For different SSH ports on each host, define the ports in your SSH configuration file, then use the configured host aliases in the scp command.
Useful options
-ppreserves file times and permissions.-qsuppresses progress and non-error messages.-rcopies directories recursively.-Psets the remote SSH port.-vprints connection details for troubleshooting.-Cenables compression.-llimits bandwidth, in kilobits per second.
Newer OpenSSH versions use SFTP for scp transfers by default. Check man scp for options supported by your installed version.
scp makes secure file transfers between local and remote systems simple. With a few options, you can copy directories, use custom SSH ports, and manage transfers between hosts. Check your paths before copying, and explore man scp to learn more.