SCP (Secure Copy) allows you to securely transfer files between a local host and a remote host—or even between two remote hosts. It is based on the SSH protocol, which means it inherits strong authentication and encryption. SCP is popular for its simplicity, security, and wide availability, as it comes pre-installed on most Linux distributions.

Basic SCP Examples

Copy a file from a remote host to a local host

scp username@from_host:file.txt /local/directory/

Copy a file from a local host to a remote host

scp file.txt username@to_host:/remote/directory/

Copy a directory from a remote host to a local host

scp -r username@from_host:/remote/directory/ /local/directory/

Copy a directory from a local host to a remote host

scp -r /local/directory/ username@to_host:/remote/directory/

Copy a file between two remote hosts

scp username@from_host:/remote/directory/file.txt username@to_host:/remote/directory/

Advanced SCP Usage

Copying files using a specific port

Specify a custom SSH port (e.g., 2222):

scp -P 2222 username@from_host:file.txt /local/directory/

Enable compression during transfer

Useful for large files:

scp -C username@from_host:file.txt /local/directory/

Limit bandwidth usage

Limits bandwidth to 1000 Kbit/s:

scp -l 1000 username@from_host:file.txt /local/directory/

Preserve file attributes

Keeps original modification times, access times, and modes:

scp -p username@from_host:file.txt /local/directory/

Using SSH Options with SCP

Use a jump host

You can go through a jump host before connecting to the target:

scp -o "ProxyJump user@jump_host" username@from_host:file.txt /local/directory/

Quiet mode

Suppresses progress meter and warnings:

scp -q username@from_host:file.txt /local/directory/

Verbose mode

Prints debugging messages for troubleshooting:

scp -v username@from_host:file.txt /local/directory/

Recursive copy with compression and SSH options

Combines multiple flags for advanced transfers:

scp -r -C -o "ProxyJump user@jump_host" username@from_host:/remote/directory/ /local/directory/

SCP Options Summary

OptionDescription
-rRecursively copy directories (follows symbolic links).
-CEnable compression.
-l limitLimit bandwidth in Kbit/s.
-o ssh_optionPass custom SSH options (e.g., ProxyJump).
-P portSpecify the SSH port (capital P).
-pPreserve modification, access times, and file modes.
-qQuiet mode: disables progress and warnings.
-vVerbose mode for debugging and connection messages.

Notes and Best Practices

  • Testing first: When using advanced options, test in a safe environment before production.
  • File overwrites: SCP replaces the contents of a target file if it already exists. Be careful!
  • Host identification: Use either an IP address or domain name. SSH password will be prompted after pressing Enter.
  • Cross-platform: These commands work on Linux and macOS. On Windows, use tools like WinSCP or PowerShell with SCP support.
  • Example backup command:
scp -r root@123.123.123.123:/var/www/html/ /home/hydn/backups/test/

Conclusion

SCP remains a reliable and secure method for transferring files over SSH. With options for compression, bandwidth limiting, preserving file attributes, and jump hosts, it can handle simple transfers as well as advanced backup workflows.

READ 👉  Winuxos Review: The Linux Distro That Looks Just Like Windows 11

For frequent Linux sysadmins and power users, mastering SCP is essential for secure, efficient, and reliable file transfers.

Did you enjoy this article? Feel free to share it on social media and subscribe to our newsletter so you never miss a post!

And if you'd like to go a step further in supporting us, you can treat us to a virtual coffee ☕️. Thank you for your support ❤️!
Buy Me a Coffee

Categorized in:

Tagged in:

, ,