How to fix lost connection in SSH

SSHINTERMEDIATEMEDIUMUpdated Jan 4, 2026ยท 291 views

The 'lost connection' error in SCP occurs when the SSH connection is unexpectedly dropped during file transfer. This can happen due to network issues, SSH protocol incompatibilities, or command path problems on the remote host.

Quick fix

Fixes lost connection

ssh -v user@remote-host echo "test"
  1. Verify SSH connectivity first
  2. Check if scp is installed on the remote host
  3. Use -O flag for OpenSSH 9.0+
  4. Limit bandwidth to prevent network stalls
ssh โ€” error
ssh -v user@remote-host echo "test"
lost connection

What this error means

SCP (Secure Copy Protocol) uses SSH to securely transfer files between hosts. When you see 'lost connection', it means the underlying SSH connection failed or was terminated prematurely during the file transfer operation. This is typically a signal that something went wrong with either the SSH connection itself, the remote scp command, or the network path between the two hosts.

Typical symptoms

  • File transfer starts but stops abruptly with 'lost connection' message
  • SCP command hangs and then fails after several seconds or minutes
  • SSH works fine, but SCP transfers fail consistently
  • Larger files fail more frequently than smaller ones
  • Error occurs at random points during long transfers

Common causes

  1. OpenSSH 9.0+ changed default protocol from SCP to SFTP, causing incompatibility with devices that don't support SFTP
  2. The scp command is not installed or not in the PATH on the remote host
  3. Network bandwidth limitations or firewall rules dropping large packets
  4. SSH connection timeout due to inactivity or firewall idle timeouts
  5. User ID resolution issues on the remote host (cannot find name for user ID)
  6. Disk space or permission issues on the destination during transfer
  7. Firewall or rate-limiting middleware interrupting the data stream

How to fix "lost connection"

1Verify SSH connectivity first

Before troubleshooting SCP, confirm that basic SSH works:

bash
ssh -v user@remote-host echo "test"

If this fails, the issue is with SSH itself, not SCP. Ensure your SSH key is loaded and the remote host is reachable.

2Check if scp is installed on the remote host

Log into the remote host via SSH and verify scp is available:

bash
ssh user@remote-host which scp

If this returns nothing, scp is not installed or not in the PATH. Install OpenSSH on the remote host:

Linux (Debian/Ubuntu):

bash
sudo apt-get install openssh-client openssh-server

Linux (RedHat/CentOS):

bash
sudo yum install openssh-clients
3Use -O flag for OpenSSH 9.0+

If your local OpenSSH is version 9.0 or higher, try using the -O option to force the legacy SCP protocol instead of SFTP:

bash
scp -O local-file.txt user@remote-host:/path/to/destination/

You can check your OpenSSH version with:

bash
ssh -V

If the remote host doesn't support SFTP, this flag forces SCP compatibility.

4Limit bandwidth to prevent network stalls

High-speed transfers can cause network stalls. Use the -l option to limit bandwidth (in Kbit/s):

bash
scp -l 8192 large-file.zip user@remote-host:/path/

This limits the transfer to ~1 MB/second, which is often more stable for problematic connections. Adjust the value based on your network capacity.

5Enable verbose output for debugging

Get detailed information about what's happening:

bash
scp -vvv local-file.txt user@remote-host:/path/

The multiple -v flags increase verbosity. Look for:
- Authentication errors
- Connection timeouts
- Channel opening failures
- Permission denied messages

This output often reveals the exact point of failure.

6Check disk space and permissions on destination

Ensure the destination directory exists and has space:

bash
ssh user@remote-host "df -h /path/to/destination && ls -ld /path/to/destination"

The transfer will fail if:
- The destination directory doesn't exist
- No write permissions in the destination
- No free space on the destination disk

Create the directory if needed:

bash
ssh user@remote-host mkdir -p /path/to/destination
7Try alternative transfer methods

If SCP continues to fail, use SFTP (if supported):

bash
sftp user@remote-host
put local-file.txt /path/to/destination/
quit

Or use rsync over SSH for more robust large file transfers:

bash
rsync -avz --partial local-file.txt user@remote-host:/path/

Both are more tolerant of network interruptions than SCP.

Advanced notes

OpenSSH Protocol History: OpenSSH 8.9 and earlier used the SCP protocol by default. Starting with 9.0 (released in April 2023), SFTP became the default, which is more modern but breaks compatibility with devices that only support the legacy SCP protocol (like older Cisco IOS XE devices).

Large File Transfers: SCP greedily consumes network bandwidth, which can cause TCP stalls if there are firewall rules, packet shaping, or network delays. Bandwidth limiting with -l is often the solution for transfers larger than 50MB.

AWS Session Manager Issue: When using SCP through AWS Systems Manager Session Manager proxy, large files (60MB+) may fail with 'connection corrupted' or 'lost connection'. Try reducing bandwidth with -l or switching to SFTP/rsync.

Non-interactive Shell PATH: When using SCP with non-interactive shells (like cron or CI/CD), the remote PATH may be different. The scp command may not be in the standard PATH. Explicitly specify the full path to scp in the remote command if needed.

Firewall and Network Equipment: Some firewalls or load balancers have timeouts for idle connections. If transfers are slow, the connection may timeout. Use keepalive options: ssh -o ServerAliveInterval=60 -o ServerAliveCountMax=10 to keep the connection alive.

Frequently asked questions

What does "lost connection" mean?
SCP (Secure Copy Protocol) uses SSH to securely transfer files between hosts. When you see 'lost connection', it means the underlying SSH connection failed or was terminated prematurely during the file transfer operation. This is typically a signal that something went wrong with either the SSH connection itself, the remote scp command, or the network path between the two hosts.
How do I fix "lost connection"?
The 'lost connection' error in SCP occurs when the SSH connection is unexpectedly dropped during file transfer. This can happen due to network issues, SSH protocol incompatibilities, or command path problems on the remote host.
What causes "lost connection"?
OpenSSH 9.0+ changed default protocol from SCP to SFTP, causing incompatibility with devices that don't support SFTP The scp command is not installed or not in the PATH on the remote host Network bandwidth limitations or firewall rules dropping large packets
Is the "lost connection" error serious?
It is a medium-severity error โ€” more of a blocker than a risk. The steps above resolve it.

Official resources & further reading