SCP

Also known as: Secure Copy Protocol

On this page 7

SCP, the Secure Copy Protocol, is a method for copying files between hosts over an SSH connection, so that both authentication and data transfer are encrypted. Among Internet protocols for file transfer, it is the minimal, command-line oriented option: one command copies a file or directory tree to or from a remote machine, with no interactive session and no directory browsing.

SCP has no formal standard. It inherits its wire format from the old BSD rcp command and simply runs that exchange inside an SSH channel. Since OpenSSH 9.0 (released in 2022), the scp client uses the SFTP protocol underneath by default, while keeping the familiar command syntax, because the legacy protocol has design flaws that cannot be fixed without breaking compatibility.

How the Secure Copy Protocol transfers files

A legacy SCP transfer works by starting a remote scp process through SSH. The local client opens an SSH session and executes a command such as scp -t /target (sink mode) or scp -f /source (source mode) on the server. The two processes then exchange a short text-based control stream followed by raw file bytes inside the encrypted channel.

Each file is announced by a control line that carries its permission bits, size and name, after which the content follows and is confirmed by a single acknowledgment byte. Directories are entered and left with dedicated messages, which allows recursive copies with the -r option.

The table below lists the control messages of the legacy protocol.

MessageMeaningExample content
CSingle file followsMode 0644, size 1024, file name
DEnter a directoryMode 0755, directory name
ELeave the current directoryNo arguments
TTimestamps for the next itemModification and access times
Status byteAcknowledgment or error0 for success, 1 or 2 for errors
Legacy SCP control messages

Security comes entirely from SSH transport and authentication: key exchange, host key verification, ciphers and public-key or password login are handled by SSH. SCP itself adds no integrity checks beyond those of the SSH channel, which already provides a message authentication code on every packet.

SCP compared with SFTP, FTPS and rsync

SCP is the simplest SSH-based copy tool, but it offers fewer functions than its alternatives. The main competitor is SFTP, the SSH File Transfer Protocol, which runs over the same SSH connection but provides a full remote file system interface.

FeatureSCPSFTPFTPSRsync over SSH
TransportSSH, port 22SSH, port 22TLS, ports 21 and 990SSH, port 22
Directory listingNoYesYesLimited
Resume interrupted transferNoYesYesYes
Delta transferNoNoNoYes
Formal specificationNoneIETF draftsRFC 4217None
Firewall friendlinessHighHighLow, extra data channelsHigh
SCP and related file transfer methods

For a one-off copy of a known file, SCP remains convenient. For large or repeated synchronization, rsync transfers only changed blocks, and for interactive management or scripted file operations, SFTP is the better choice. FTPS suits environments that already depend on FTP tooling but need TLS encryption.

Using the scp command in practice

The scp command uses a source and destination syntax in which remote paths are written as user@host:path. The same options apply whether the client speaks legacy SCP or SFTP underneath.

OptionFunctionTypical use
-rCopy directories recursivelyUploading a site build folder
-P portConnect to a non-default SSH portServers listening on port 2222
-i keyUse a specific private keyAutomation with a deploy key
-pPreserve times and modesBackups that keep timestamps
-l limitLimit bandwidth in Kbit/sCopying over a shared link
-OForce the legacy SCP protocolOld servers without SFTP
-3Route a remote-to-remote copy through the clientHosts that cannot reach each other
Frequently used scp options

Note that scp uses capital P for the port, unlike ssh, which uses lowercase -p. This inconsistency is a frequent source of failed commands in scripts.

Security weaknesses of the legacy SCP protocol

The legacy protocol is considered outdated and inflexible because the remote side runs a shell command and the client must trust what the server sends back. Several historical vulnerabilities followed from this design.

  • Shell expansion of remote paths: file names are interpreted by the remote shell, so names with spaces or metacharacters need double quoting and can trigger unintended commands.
  • Weak validation of server responses: in 2019, CVE-2018-20685 and related flaws showed that a malicious server could overwrite unexpected files or change directory permissions on the client.
  • No partial resume or listing: an interrupted transfer must start again from the beginning.
  • Restricted accounts are hard to build: SFTP-only accounts can be confined with ForceCommand internal-sftp, while legacy SCP needs a remote shell.

For these reasons, the OpenSSH developers describe the legacy protocol as not recommended for new use. Keeping the scp command while letting it speak SFTP gives users the familiar syntax with stricter path handling on the server side.

When to choose SCP and when to avoid it

SCP fits quick, ad hoc transfers between machines that already allow SSH access. The decision table below summarizes typical situations.

SituationRecommended choiceReason
Copy one config file to a serverscpShort command, no setup
Nightly backup of a large treersync over SSHTransfers only changed data
Partner uploads to a shared hostSFTP with chrootRestricted, auditable access
Very old embedded devicescp with -ODevice lacks an SFTP subsystem
Multi-gigabyte file over an unstable linkSFTP or rsyncSupports resuming
Choosing a transfer method

Whatever tool is chosen, the host key fingerprint must be verified on first connection, because SCP inherits all of SSH’s protection and none of it applies if a forged host key is accepted.

Understanding it simply

SCP is like handing a sealed envelope to a trusted courier who already has a key to both buildings. You say which file to take and where to put it, and the courier carries it across without anyone along the way being able to read it.

It does one job only: it copies files safely from one computer to another. It cannot show you what is inside the other building or pick up where it stopped if interrupted. Newer versions quietly use a more capable courier service underneath, while you keep giving the same simple instructions.

Applied in practice

A systems administrator at a regional hospital needs to replace a TLS certificate on a Linux web server after renewal. From a hardened jump host, she runs scp -i ~/.ssh/deploy_ed25519 cert.pem admin@web01:/etc/ssl/site/, verifies the file with a checksum, and reloads the web server. The 4 KB file arrives in under a second over the existing SSH access, with no extra service exposed on the server.

The same team once used scp for nightly database dumps of about 40 GB, and transfers had to restart whenever the WAN link dropped. They moved that job to rsync over SSH with resumable partial files, which cut nightly transfer time by more than half, and kept scp for small, one-off administrative copies.