How to fix lost connection in SSH
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"- Verify SSH connectivity first
- Check if scp is installed on the remote host
- Use -O flag for OpenSSH 9.0+
- Limit bandwidth to prevent network stalls
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
- 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
- SSH connection timeout due to inactivity or firewall idle timeouts
- User ID resolution issues on the remote host (cannot find name for user ID)
- Disk space or permission issues on the destination during transfer
- Firewall or rate-limiting middleware interrupting the data stream
How to fix "lost connection"
Before troubleshooting SCP, confirm that basic SSH works:
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.
Log into the remote host via SSH and verify scp is available:
ssh user@remote-host which scpIf this returns nothing, scp is not installed or not in the PATH. Install OpenSSH on the remote host:
Linux (Debian/Ubuntu):
sudo apt-get install openssh-client openssh-serverLinux (RedHat/CentOS):
sudo yum install openssh-clientsIf your local OpenSSH is version 9.0 or higher, try using the -O option to force the legacy SCP protocol instead of SFTP:
scp -O local-file.txt user@remote-host:/path/to/destination/You can check your OpenSSH version with:
ssh -VIf the remote host doesn't support SFTP, this flag forces SCP compatibility.
High-speed transfers can cause network stalls. Use the -l option to limit bandwidth (in Kbit/s):
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.
Get detailed information about what's happening:
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.
Ensure the destination directory exists and has space:
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:
ssh user@remote-host mkdir -p /path/to/destinationIf SCP continues to fail, use SFTP (if supported):
sftp user@remote-host
put local-file.txt /path/to/destination/
quitOr use rsync over SSH for more robust large file transfers:
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.