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.
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.
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.
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.
Load key "/home/user/.ssh/id_rsa": invalid format
How to fix 'Load key invalid format' in SSH
Bad owner or permissions on /home/user/.ssh/config
How to fix "Bad owner or permissions on .ssh/config" in SSH
Error connecting to agent: Connection refused
How to fix "Error connecting to agent: Connection refused" in SSH
Connection closed by UNKNOWN port 65535
How to fix 'Connection closed by UNKNOWN port 65535' in SSH
Offending ECDSA key in /home/user/.ssh/known_hosts:line
How to fix "Offending ECDSA key in known_hosts" in SSH