Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

SCP Fails but SSH Works: Causes & Fixes

· 7 min read · Home Tech

In this article
  1. Background: How SCP Differs from SSH
  2. Why SSH Works but SCP Fails: The Core Causes
  3. How to Diagnose the Exact Problem
  4. Fixes: From Quick to Permanent
  5. Common Misconception: SCP is Just SSH
  6. Comparing SCP, SFTP, and rsync
  7. Preventing Future SCP Failures
  8. Frequently Asked Questions

SCP (Secure Copy) failing while SSH works is almost always a problem with the remote shell environment or the SCP protocol version, not with your SSH connection itself. The fix usually involves switching to SFTP, using the -O flag for legacy SCP, or fixing the remote user's shell startup files.

Background: How SCP Differs from SSH

SSH is a network protocol that provides a secure, encrypted channel between two computers. When you run ssh user@host, you get an interactive terminal session on the remote machine. SCP, on the other hand, is a separate program that runs over that SSH connection. It uses the SSH protocol for authentication and encryption, but it relies on a remote SCP server component to actually transfer files.

In traditional SCP (the original implementation from the OpenSSH suite), the client sends a command to the remote host like scp -t /path/to/destination. The remote host then executes this command using the user's default shell. The SCP protocol then communicates through that shell session, sending file data and receiving status messages. This design is why SCP is sometimes called a "non-interactive" protocol — it expects the remote shell to be clean and predictable.

Modern OpenSSH versions (8.0 and later) have added an alternative SCP mode that uses the SFTP protocol instead, which is more robust. But many systems still default to the legacy SCP protocol, which is where most failures originate.

Why SSH Works but SCP Fails: The Core Causes

When SSH works perfectly but SCP fails, the problem is almost never the SSH connection itself. Here are the most common reasons:

  • Shell startup files print output. If your remote .bashrc, .zshrc, or .profile contains an echo statement, a banner, or any command that writes to stdout, that output gets mixed into the SCP protocol stream. The remote SCP server expects only protocol messages, so the extra text corrupts the transfer. SSH works because it's interactive — the output just appears on your terminal.
  • The remote shell is not POSIX-compliant. SCP relies on the remote shell to parse the command it sends. If the user's shell is something unusual (like a restricted shell, a custom script, or a Windows command prompt), it may not understand the SCP command syntax.
  • SCP protocol version mismatch. The legacy SCP protocol is simple but has known issues with filenames containing spaces or special characters. Newer OpenSSH versions (8.0+) can use the SFTP protocol for SCP, but this requires the -O flag (to force legacy) or the -s flag (to force SFTP). If the client and server disagree on which protocol to use, you can see failures.
  • Missing or misconfigured SFTP subsystem. If you're using the SFTP-based SCP (the default in OpenSSH 9.0+), the remote server must have the SFTP subsystem enabled in sshd_config. If it's disabled or points to a non-existent binary, SCP fails while SSH still works.
  • Path issues. The remote SCP server may not be in the PATH for non-interactive shells. If the user's PATH is set in .bashrc but that file is skipped for non-interactive sessions, the scp binary may not be found.

How to Diagnose the Exact Problem

Before applying a fix, you need to know which of the above is causing your issue. Here's a step-by-step diagnostic process:

  1. Test with verbose output. Run scp -v file user@host:/path/. The verbose output shows the exact command sent to the remote host. Look for the line starting with Sending command: — that tells you what the remote shell is being asked to execute.
  2. Check for shell output. Run ssh user@host 'echo test'. If you see any extra output before "test", your shell startup files are printing something. Try ssh user@host 'cat ~/.bashrc' to inspect them.
  3. Test the remote SCP binary. Run ssh user@host 'which scp'. If this returns nothing or an error, the scp binary is not in the PATH for non-interactive shells.
  4. Try SFTP directly. Run sftp user@host. If that works, the SFTP subsystem is fine, and the issue is likely with the legacy SCP protocol.
  5. Check the server's SSH configuration. Look at /etc/ssh/sshd_config on the remote host for lines like Subsystem sftp or SFTPPath. Ensure they are not commented out and point to a valid binary.

Fixes: From Quick to Permanent

Once you know the cause, pick the appropriate fix. Start with the quickest and work your way down.

  • Force SFTP protocol. If your OpenSSH version supports it (8.0+), run scp -s file user@host:/path/. The -s flag tells scp to use the SFTP protocol instead of legacy SCP. This bypasses all shell-related issues.
  • Use legacy SCP mode. If you're on OpenSSH 9.0+ and the default is SFTP, but SFTP is broken, try scp -O file user@host:/path/. The -O flag forces the original SCP protocol.
  • Clean up shell startup files. Edit the remote user's .bashrc (or equivalent) and remove any echo statements, banners, or commands that write to stdout. For example, if you have echo "Welcome!" at the top, delete it. Make sure the file is otherwise clean.
  • Set a safe PATH in the shell file. If which scp fails, add a line like export PATH=$PATH:/usr/bin:/bin to the remote user's .bashrc or .profile. This ensures the scp binary is found even in non-interactive sessions.
  • Switch to SFTP or rsync. If you can't fix the remote environment, use sftp interactively or rsync -e ssh for file transfers. Both are more robust than legacy SCP.
  • Fix the SSH server config. If the SFTP subsystem is missing, edit sshd_config on the remote host to add Subsystem sftp /usr/lib/openssh/sftp-server (path varies by OS), then restart sshd. This requires root access.

Common Misconception: SCP is Just SSH

Many people assume that because SCP uses SSH for encryption, it's the same thing. That's only half true. The SSH connection is the secure tunnel, but SCP is a separate application protocol that runs inside that tunnel. Think of SSH as the armored truck and SCP as the courier inside it. If the courier has a bad map (shell issues) or the package is mislabeled (protocol mismatch), the delivery fails even though the truck runs fine.

Another misconception is that SCP is always faster or more reliable than SFTP. In reality, SFTP (which is also built on SSH) is more feature-rich and less fragile. Modern OpenSSH has been moving toward SFTP as the default for file transfers, which is why the -O flag exists — to force the old behavior for compatibility.

Comparing SCP, SFTP, and rsync

When SCP fails, you have alternatives. Here's a quick comparison:

  • SCP — Simple, widely available, but fragile with shell output and special characters. Best for quick one-off transfers on trusted systems.
  • SFTP — Interactive or scriptable, uses a proper protocol, handles filenames with spaces well. Available on any system with OpenSSH. Use sftp user@host or scp -s.
  • rsync over SSH — Best for large or repeated transfers, supports incremental sync and compression. Use rsync -avz -e ssh /local/path user@host:/remote/path. Requires rsync installed on both ends.

For a one-time file copy, SFTP is the safest drop-in replacement. For syncing directories, rsync is superior.

Preventing Future SCP Failures

To avoid this problem recurring, consider these practices:

  • Keep shell startup files clean. Avoid printing anything to stdout in .bashrc or .profile. If you need a welcome message, use ~/.hushlogin or only print when the shell is interactive (check $- for i).
  • Use SFTP as your default. If your OpenSSH version supports it, set an alias: alias scp='scp -s' in your local shell config.
  • Test after any server change. If you update the remote server's SSH config or shell files, run a quick scp test to ensure nothing broke.
  • Document your environment. If you manage multiple servers, note which ones use legacy SCP vs SFTP. This saves debugging time later.

Frequently Asked Questions

Why does SCP fail with "protocol error: unexpected newline"?

This error almost always means the remote shell printed something to stdout before the SCP protocol started. The fix is to remove any echo or banner from the remote user's shell startup files. Check .bashrc, .profile, and .bash_profile.

Can I use SCP with a Windows remote host?

Yes, but you need an SSH server like OpenSSH for Windows. The same shell-output issue can occur if the Windows command prompt or PowerShell prints anything. Use SFTP instead, which is more reliable on Windows.

What does "scp: Connection closed" mean?

This usually means the remote SCP process exited before completing the transfer. Causes include shell startup errors, missing scp binary, or a forced command in the SSH server config (like a restricted shell). Check the SSH server logs on the remote host.

Is SCP deprecated?

Not officially, but OpenSSH has been warning that the legacy SCP protocol is outdated and buggy. Since OpenSSH 9.0, the default for scp is to use SFTP, and the legacy protocol is only available with the -O flag. It's wise to migrate to SFTP or rsync.

Why does SCP work from one machine but not another?

This is usually a client-side version issue. The working machine may have an older OpenSSH that defaults to legacy SCP, while the failing one uses SFTP by default. Or the failing client has a local configuration that forces a specific protocol. Check scp -V to see the version and flags.