Resolving 'subsystem request failed on channel 0' in SCP
Problem Description
When using the scp
command to securely copy files between systems, you might encounter this error:
subsystem request failed on channel 0
scp: Connection closed
This typically occurs when running commands like:
scp -p -P 29418 user@host:source_path destination_path
The error indicates a protocol failure between your SSH client and the remote server during file transfer initiation. Common environments where this occurs include:
- Older SSH server implementations
- Windows-based OpenSSH servers
- Custom-configured SSH servers using non-standard setups
Solution: Use Legacy SCP Protocol with -O Flag
Add the -O
option to your scp
command to force use of the legacy SCP protocol:
scp -p -O -P 29418 user@host:source_path destination_path
Why This Works
- The
-O
flag bypasses the newer SFTP protocol and reverts to the original SCP transfer mechanism - Some servers don't fully implement the SFTP subsystem correctly
- Older servers (particularly on Windows) might have configuration quirks preventing proper SFTP negotiation
- The legacy protocol maintains security since it still operates over SSH encryption
Protocol Differences
Feature | SCP Protocol (-O ) | SFTP Protocol (Default) |
---|---|---|
Transfer Method | rcp -style protocol | SSH File Transfer Protocol |
Server Compatibility | Better for older/Windows servers | Modern standard |
Wildcard Handling | Original expansion rules | POSIX-compliant |
Path Expansion | Handles ~ prefixes | Standard implementation |
Security Basis | SSH encrypted channel | SSH encrypted channel |
Implementation Example
# Original failing command:
scp -p -P 29418 michealvern.genzola@192.168.0.122:hooks/commit-msg "jyei-erp/.git/hooks/"
# Fixed version with -O:
scp -p -O -P 29418 michealvern.genzola@192.168.0.122:hooks/commit-msg "jyei-erp/.git/hooks/"
Additional Considerations
Windows OpenSSH Servers: This error frequently occurs with PowerShell's Win32-OpenSSH implementation. A GitHub issue confirms the
-O
workaround is effective.Alternative Solutions:
bash# Try using rsync instead (if available) rsync -e 'ssh -p 29418' -av user@host:source_path destination_path # Use sftp with batch mode (requires different syntax) echo "get remote_path local_path" | sftp -P 29418 user@host
When Not to Use -O
The legacy SCP protocol has limitations:
- Doesn't support newer SSH features like transfer resumption
- Limited error handling capabilities
- Wildcard expansion differences Only use
-O
when necessary for compatibility
- Server Configuration Fixes:
If you control the server, consider these permanent solutions:- Update SSH server software
- Verify subsystem configuration in
/etc/ssh/sshd_config
:Subsystem sftp /usr/lib/openssh/sftp-server
- Restart SSH service after changes
Troubleshooting Flowchart
This solution addresses a common interoperability issue between modern SCP clients and servers with incomplete SFTP implementations. The -O
flag provides a reliable client-side workaround while maintaining secure data transfer through SSH encryption.