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.

How to Use SCP to Copy Files Securely.
Image credit: Unknown. Please contact us if you are the owner.


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 destination

A 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.txt

If 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

  • -p preserves file times and permissions.
  • -q suppresses progress and non-error messages.
  • -r copies directories recursively.
  • -P sets the remote SSH port.
  • -v prints connection details for troubleshooting.
  • -C enables compression.
  • -l limits 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.

Keep Us Caffeinated  ⦿ ⦿
Spotify Logo
↑
SNUBmonkey Join our readers