[Fixed] scp: subsystem request failed on channel 0 / Connection closed: Step-by-Step Troubleshooting Guide

Quick Summary: The error subsystem request failed on channel 0 followed by scp: Connection closed occurs when transferring files with scp from a modern client (OpenSSH 9.0+, macOS Ventura/Sonoma/Sequoia, Ubuntu 22.04+) to a remote server that lacks an SFTP subsystem (such as minimal Docker containers, BusyBox/Dropbear embedded devices, Alpine Linux, or servers with a broken sftp-server path in /etc/ssh/sshd_config).

Understanding the Root Causes

Starting with OpenSSH 9.0, the scp client switched its default underlying file transfer protocol from the legacy SCP/rcp protocol to the more secure SFTP protocol. While standard Linux servers support SFTP out of the box, transfers fail immediately when:

  • Minimal or Embedded Remote Targets: Devices running Dropbear SSH (OpenWrt routers, IoT hardware), minimal Alpine containers, or stripped-down rescue environments often only include the legacy scp binary and lack an sftp-server binary.
  • Misconfigured sshd_config on the Server: The remote /etc/ssh/sshd_config points Subsystem sftp to a non-existent path (e.g., /usr/lib/openssh/sftp-server vs. /usr/libexec/sftp-server on RHEL/CentOS).
  • Restricted Shell or Forced Commands: The remote user’s shell or SSH authorized_keys command="..." wrapper blocks the SFTP subsystem request.

Step 1: Quick Fix – Force Legacy SCP Protocol with the -O Flag

If you cannot modify the remote server (for example, an embedded router or appliance), instruct your local scp client to use the legacy SCP protocol via the uppercase -O flag:

# Force legacy SCP protocol instead of SFTP (OpenSSH 9.0+)
scp -O localfile.tar.gz user@192.168.1.50:/tmp/

# Works for recursive directory transfers as well
scp -O -r ./dist/ user@192.168.1.50:/var/www/html/

If you frequently connect to legacy hosts and want to avoid typing -O every time, add a shell alias in ~/.bashrc or ~/.zshrc (e.g., alias scp-legacy='scp -O'), or use rsync -e ssh as an alternative.

Step 2: Fix the SFTP Subsystem Path on the Remote Server

If you manage the remote Linux server and want modern SFTP transfers to work natively, verify that the sftp-server binary exists and update /etc/ssh/sshd_config:

# 1. Locate the actual sftp-server binary on the remote server
find /usr -name "sftp-server" 2>/dev/null
# Typical paths:
# Debian/Ubuntu: /usr/lib/openssh/sftp-server
# RHEL/CentOS/Fedora: /usr/libexec/openssh/sftp-server
# Alpine Linux: /usr/lib/ssh/sftp-server (requires openssh-sftp-server package)

# 2. On Alpine Linux, install the SFTP server package if missing
apk add --no-cache openssh-sftp-server

# 3. Edit /etc/ssh/sshd_config on the remote server
sudo nano /etc/ssh/sshd_config

# Update or use OpenSSH's built-in internal-sftp (no external binary path needed!):
Subsystem sftp internal-sftp

# 4. Validate sshd configuration syntax and reload SSH
sudo sshd -t && sudo systemctl reload sshd

Step 3: Use SSH Piping or Rsync as a Universal Workaround

If the remote server has neither an SFTP subsystem nor a remote scp binary (common in ultra-minimal containers), stream the file directly over standard SSH stdin/stdout:

# Upload a single file via raw SSH stdin pipe
cat localfile.tar.gz | ssh user@192.168.1.50 "cat > /tmp/localfile.tar.gz"

# Download a file via raw SSH stdout pipe
ssh user@192.168.1.50 "cat /var/log/app.log" > ./app.log

# Transfer an entire directory using tar over SSH
tar czf - ./project | ssh user@192.168.1.50 "tar xzf - -C /opt/"

Verification & Testing Steps

Test file transfer using both the legacy flag and standard SFTP mode with verbose logging to verify the subsystem handshake:

# 1. Verify legacy SCP transfer succeeds
scp -O -v test.txt user@192.168.1.50:/tmp/

# 2. Verify SFTP subsystem works after updating sshd_config
sftp user@192.168.1.50 <<EOF
ls /tmp
bye
EOF

# 3. Check active sshd Subsystem configuration on the server
sudo sshd -T | grep -i subsystem

Troubleshooting Summary Comparison

Scenario Primary Root Cause Recommended Solution
OpenWrt / Dropbear / Embedded device No SFTP server binary on target device Run scp -O <src> <dest> to force legacy SCP
Alpine / Minimal Linux Server openssh-sftp-server not installed Install package and set Subsystem sftp internal-sftp
RHEL / Ubuntu Migration Wrong sftp-server binary path in sshd_config Switch to Subsystem sftp internal-sftp and reload sshd
Stripped Container (no scp or sftp) Both file-transfer helpers absent Pipe via cat file | ssh host "cat > file" or tar

Leave a Reply