The scp (secure copy protocol) utility is a staple tool in Unix and Linux systems administration, allowing operators to transfer files securely between local and remote hosts over encrypted SSH connections. However, initiating a transfer command such as scp file.tar.gz user@remote:/var/backups/ frequently halts immediately or aborts midway through with the terse notification: ‘Connection closed’ or ‘lost connection’. Because scp runs on top of the OpenSSH transport protocol, diagnosing unexpected connection terminations requires examining remote shell startup files, timeout parameters, and network packet boundaries.
What Causes ‘Connection Closed’ During SCP File Transfers?
When scp initiates a session, it executes a non-interactive remote shell on the destination server. Common triggers for dropped connections include:
- Extraneous Echo Output in Remote .bashrc: The single most common culprit; having
echostatements, MOTD banners, or terminal colour scripts inside the remote user’s~/.bashrcfile.scpexpects clean binary protocol streams and immediately terminates the connection if it receives unexpected text output. - SSH Idle Session Timeouts (ClientAliveInterval): The remote
sshddaemon dropping the TCP connection during long transfers of massive multi-gigabyte archives due to inactivity timers. - Insufficient Remote Disk Space: The destination partition or filesystem filling to 100% capacity, forcing the remote kernel to close the write socket abruptly.
- MTU / Packet Fragmentation Mismatches: Network routers or VPN tunnels dropping packets exceeding the Maximum Transmission Unit (MTU) size.
- Legacy SCP Protocol vs Modern SFTP Subsystem: Modern OpenSSH versions default to the SFTP protocol for
scp. If the remote host has the SFTP subsystem disabled insshd_config, the connection aborts.
Step 1: Check for and Silence Terminal Output in Remote ~/.bashrc
Non-interactive sessions (such as SCP and SFTP) require pristine standard output without interactive shell messages.
- Log into the remote server via interactive SSH:
ssh user@remote_host - Open the user’s
~/.bashrcfile in a text editor (e.g.,nano ~/.bashrc). - Look for any commands that print text to the terminal (such as
echo "Welcome",neofetch,fortune, or date commands). - Add a check at the very top of
~/.bashrcto exit immediately if the session is non-interactive:# If not running interactively, do not do anything: [[ $- != *i* ]] && return - Save and close the file. Test your
scptransfer from the local machine. It will now execute without closing.
Step 2: Force the Legacy SCP Protocol with the -O Flag
In OpenSSH 9.0 and later, scp uses the SFTP protocol by default. If the remote server lacks SFTP support, force legacy SCP protocol mode.
- Add the capital
-Oflag to your command:scp -O file.tar.gz user@remote:/destination/ - The
-Ooption instructs the client to use the original legacy SCP protocol rather than demanding an SFTP subsystem handshake.
Step 3: Enable SSH KeepAlive to Prevent Middlebox Timeouts
Stateful firewalls and NAT routers frequently terminate long file transfers if packet frequency pauses.
- Pass TCP keepalive parameters directly on the command line:
scp -o ServerAliveInterval=30 -o ServerAliveCountMax=5 file.tar.gz user@remote:/dest/ - This instructs your SSH client to send an encrypted keepalive ping through the tunnel every 30 seconds, preventing intermediate firewall drops.
- Alternatively, make this permanent by adding the lines to your local
~/.ssh/configfile:Host * ServerAliveInterval 30 ServerAliveCountMax 5
Step 4: Check Remote Disk Space and Quota Limits
If the remote storage volume runs out of free blocks, the kernel abruptly terminates incoming streams.
- Log into the remote machine and check available storage:
df -h - Check inode availability:
df -i - If either disk usage or inode usage displays 100%, delete unneeded logs or temp files before resuming file transfers.
Step 5: Alternative: Use Rsync for Resumable and Resilient Transfers
For large file transfers prone to network drops, rsync is vastly superior to scp because it can automatically resume interrupted transfers.
- Execute transfer via
rsync:rsync -avz --partial --progress file.tar.gz user@remote:/destination/ - The
--partialflag preserves partially downloaded blocks, allowing you to resume seamlessly without restarting from zero if a network drop occurs.