Secure Copy Protocol (SCP) is a command-line utility used to transfer files securely between local and remote hosts, or between two remote machines. Built on top of the Secure Shell (SSH) protocol, it uses the same authentication and encryption mechanisms to protect data while it moves across a network. Even though modern systems lean toward alternatives like SFTP or rsync for advanced management, SCP remains a staple for quick, automated file transfers in scripts.
Understanding how SCP operates helps you move files efficiently without compromising server security. This guide covers core syntax, practical copy scenarios, optimization flags, and common pitfalls that basic tutorials miss.
How SCP Works Under the Hood
When you run an SCP command, the client initiates an SSH connection to the target machine and requests an SCP process to start. This process operates in one of two modes:
- Source mode: Reads files from the disk and sends them back to the client.
- Sink mode: Accepts incoming files from the client and writes them to the disk on the remote host.
Because it relies on SSH, port 22 is used by default unless specified otherwise. All data, including passwords and cryptographic keys, is encrypted during transit, preventing man-in-the-middle attacks.
For official documentation on foundational cryptographic protocols, consult the OpenSSH Project.
Core SCP Syntax and Basic Operations
The basic structure of an SCP command follows a strict order: options first, followed by the source path, and finally the destination path.
Copying a Local File to a Remote Server
To send a single file from your local machine to a remote directory, use this structure:
scp document.txt remote_user@192.168.1.50:/home/remote_user/docs/
If your SSH server runs on a non-standard port (such as 2222), pass the -P flag with the port number:
scp -P 2222 document.txt remote_user@192.168.1.50:/home/remote_user/docs/
Copying a File from a Remote Server to Local
To pull a file down from a remote server to your current local directory, reverse the source and destination positions:
scp remote_user@192.168.1.50:/home/remote_user/docs/report.pdf .
Copying Entire Directories Recursively
To transfer an entire folder along with its subdirectories, you must include the recursive -r flag:
scp -r /var/www/html remote_user@192.168.1.50:/var/www/
Advanced SCP Options and Flags
Beyond basic file pushing, several flags help fine-tune transfers, especially in production environments or constrained networks.
-C: Enables compression. This is useful for large text-based files over slow network connections.-i: Specifies an explicit identity file (private SSH key) for authentication.-l: Limits the bandwidth consumption in kilobits per second, preventing the transfer from choking network traffic.-p: Preserves modification times, access times, and file modes from the original source file.
For example, to limit bandwidth usage to 800 Kb/s while pushing a backup archive, run:
scp -l 800 backup.tar.gz remote_user@192.168.1.50:/backups/
Common Mistakes and How to Avoid Them
Even experienced engineers run into friction when using SCP. Watch out for these frequent missteps:
- Forgetting the trailing slash on directories: If the destination directory does not exist on the remote host, SCP treats your destination as a filename, renaming your transferred folder into a single file.
- Ignoring SSH banner interference: If your remote user profile prints a welcome message or warning via standard output upon login (such as an
echostatement in.bashrc), SCP can choke or deadlock because it misinterprets text output as an error code. Keep non-interactive logins clean. - Mismatched file permissions: If you encounter
Permission deniederrors, check that the target directory grants write access to your remote user account.
For deeper insights into secure shell configurations, review the guidelines provided by the National Institute of Standards and Technology (NIST).
Frequently Asked Questions
Can I resume an interrupted SCP transfer?
No. Unlike SFTP or rsync, SCP cannot resume broken or interrupted file transfers. If a large transfer fails halfway through, you have to restart the process from scratch. For unstable connections, use rsync with the --partial flag instead.
How do I use SCP without entering a password?
You can bypass manual password prompts by setting up SSH key-based authentication. Generate a key pair using ssh-keygen and copy your public key to the target server using ssh-copy-id username@remote_host. Once configured, SCP authenticates automatically.
Why are modern systems moving away from SCP?
The core protocol architecture is older, lacks flexibility, and cannot perform interactive file management tasks like listing directory contents or deleting remote files. Furthermore, many modern OpenSSH distributions default to SFTP for backend transfers due to security and diagnostic improvements.
Can I transfer files directly between two remote servers?
Yes. You can execute an SCP command from your local machine to pull data from remote server A and push it straight to remote server B:
scp userA@serverA.com:/path/file.txt userB@serverB.com:/path/destination/
Both remote hosts must permit SSH communication for this routing to succeed.