skip to content

After a CI image moved to a current OpenSSH, a script's scp to an older partner server fails; why, and when is scp -O the right fix?

level: seniorimportance: nice to knowfreq 14%

answer

  1. which protocol scp speaks now
  2. who expands the wildcard
  3. quotes meant for a remote shell
  4. tilde paths and an extension
  5. a server with no SFTP at all

basics

~20 s

Since OpenSSH 9.0, scp speaks SFTP by default, so it fails on servers without an SFTP subsystem, on shell-style inner quoting, and on ~user paths an old sftp-server cannot expand; -O restores the legacy protocol for those servers only.

solid answer

~40 s

Since OpenSSH 9.0, `scp` transfers over the **SFTP protocol** by default instead of the legacy scp/rcp protocol, and a script written for the old behaviour breaks in three ways. A server with no SFTP subsystem cannot serve it at all. Remote paths that carried extra quoting for the remote shell now arrive with those quotes as literal characters, because no remote shell parses them any more. And `~user` paths need the `[email protected]` extension, which sftp-server has offered only since 8.7. `-O` selects the legacy protocol and is right when the server truly cannot do SFTP or cannot expand the path; otherwise I remove the inner quoting, use absolute paths, or move the script to `sftp -b`, which also gives me a rename for atomic drops.

code

bash · 6 lines
bash
# Before OpenSSH 9.0 (legacy protocol): inner quotes for the remote shell
scp '[email protected]:"/exports/settle 2026-10-06.csv"' ./in/
# OpenSSH 9.0 and later (SFTP mode): the inner quotes would be literal, drop them
scp -o BatchMode=yes '[email protected]:/exports/settle 2026-10-06.csv' ./in/
# Server with no SFTP subsystem: legacy protocol on purpose, for this host only
scp -O -o BatchMode=yes '[email protected]:/exports/*.csv' ./in/

go deeper

for a junior

Recall that current scp copies files over the SFTP protocol, and that -O switches it back to the legacy protocol for servers that need it.

for a middle

Explain who expands wildcards and tilde paths in each mode, and why inner quoting written for the remote shell now breaks a transfer.

for a senior

Diagnose a post-upgrade scp failure by its cause, scope -O to the one host that needs it, and prefer fixing paths or moving to sftp -b.

for a principal

Plan a fleet-wide client upgrade: inventory partner servers that lack SFTP, agree upgrade dates with them, and keep legacy-protocol exceptions listed and owned.

## Two protocols behind one command `scp` has always looked like `cp` across machines, but two different protocols have sat behind it: | | Legacy scp/rcp protocol | SFTP protocol | |---|---|---| | How it starts | runs a remote `scp` program through the user's shell | requests the server's `sftp` subsystem | | Remote wildcards | expanded by the **remote shell** | matched by the client over SFTP | | Special characters in names | need **extra quoting** for the remote shell | taken literally | | `~` and `~user` paths | expanded by the remote shell | need the `[email protected]` extension | | Selected in OpenSSH 10.5 by | `scp -O` | default since OpenSSH 9.0 | OpenSSH 9.0 made SFTP the default. The release notes were explicit that scp in SFTP mode does not try to be bug-compatible with the old quoting rules. A CI image that moves from an older OpenSSH to a current one — 10.5 at the time of writing — changes the protocol a script uses without a single line of the script changing. ## The three ways an old script breaks 1. **The server has no SFTP subsystem.** SFTP mode needs the server to answer the subsystem request. An appliance or an old server configured without a `Subsystem sftp` line cannot, so the transfer fails before any file moves. 2. **Inner quoting meant for the remote shell.** Under the legacy protocol, the remote path was handed to the remote shell, so a name with a space needed a second layer of quotes: `'host:"/exports/settle 2026-10-06.csv"'`. In SFTP mode the path travels inside an SFTP request; the inner quotes become part of the name and no such file exists. The fix is to delete the inner layer, not to add more. 3. **Tilde paths on an old server.** SFTP has no native way to expand `~user/...`. OpenSSH's sftp-server has supported the `[email protected]` extension since 8.7; an older server, or one that does not offer it, cannot resolve the path. An absolute path avoids the question. ## When -O is the right fix `-O` makes `scp` use the legacy protocol again. It is the right tool when: - the server **cannot speak SFTP** and nobody can enable it — a device, an old appliance; - the server's SFTP side **cannot expand** a `~` path the script cannot avoid; - a particular filename wildcard pattern only works with the remote shell's expansion. It brings the legacy protocol's costs back with it. The remote user's shell runs to perform the transfer and the glob matching, so every special character needs careful quoting again, and the scp man page lists that requirement under its caveats. The legacy code path is old: OpenSSH 10.3 still fixed a bug where legacy downloads made as root without -p did not clear setuid and setgid bits. And an SFTP-only account, whose server forces the in-process SFTP server for every session, cannot run the remote scp program that legacy mode needs, so `-O` fails there. So `-O` belongs on the one host that needs it, written explicitly with a comment saying why, not added to every `scp` call in the pipeline. ## What to do instead, in order of preference 1. **Remove the inner quoting** and use absolute paths; most breakages end there. 2. **Ask the partner to enable SFTP** on their server, which also lets them confine the account to file transfer. 3. **Move the step to `sftp -b`** when the job needs more than a copy: a batch file can upload under a temporary name and rename, and it aborts with a non-zero status on the first failing command. 4. **Use `-O`** only for a server that cannot do any of the above. ## Writing scp calls that survive ```bash scp -o BatchMode=yes \ '[email protected]:/exports/settle 2026-10-06.csv' ./in/ scp -O -o BatchMode=yes \ '[email protected]:/exports/*.csv' ./in/ ``` The first call works in SFTP mode because the space is taken literally. The second is a deliberate legacy call for a server without SFTP: the single quotes stop the local shell from expanding `*`, and the remote shell expands it on the server. Both use `BatchMode=yes`, so a missing key or an unconfirmed host key fails at once, and `scp` exits non-zero on any error, which fails the CI step. ## Key points - The protocol changed underneath an unchanged command line; read failures after an OpenSSH upgrade with that in mind. - Fix quoting and paths first; reach for `-O` per host, with a reason. - Prefer `sftp -b` or a library when the job needs atomic publication or fine-grained error handling.

  • Why does scp -O fail against a partner's SFTP-only account that works fine with plain scp?
    Legacy mode starts a remote `scp` program through the account's shell. An SFTP-only account has its sessions forced into the in-process SFTP server, so that program never runs and the legacy exchange breaks. Plain `scp` in SFTP mode simply requests the SFTP subsystem, which is exactly what the account allows.
  • A remote wildcard such as host:/exports/*.csv still works in SFTP mode. Who expands it now?
    The client. In SFTP mode no remote shell runs, so scp lists the remote directory over SFTP and matches the pattern itself. The pattern still needs quoting against the local shell, but not a second layer for the server.

  • scp still uses its own protocol, so SFTP settings do not matter to it
  • Adding -O to every scp call is the safe way to upgrade
  • SFTP-mode scp cannot copy files whose names contain spaces
  • The remote shell still expands wildcards when scp uses SFTP
  • scp failing after an upgrade means the key or host key changed

Assumes OpenSSH 10.5; scp has used the SFTP protocol by default since OpenSSH 9.0, with -O selecting the legacy scp/rcp protocol, and sftp-server has offered [email protected] since 8.7.