How to Fix ‘scp: Connection closed’ Error in Linux

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 echo statements, MOTD banners, or terminal colour scripts inside the remote user’s ~/.bashrc file. scp expects clean binary protocol streams and immediately terminates the connection if it receives unexpected text output.
  • SSH Idle Session Timeouts (ClientAliveInterval): The remote sshd daemon 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 in sshd_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.

  1. Log into the remote server via interactive SSH:
    ssh user@remote_host
  2. Open the user’s ~/.bashrc file in a text editor (e.g., nano ~/.bashrc).
  3. Look for any commands that print text to the terminal (such as echo "Welcome", neofetch, fortune, or date commands).
  4. Add a check at the very top of ~/.bashrc to exit immediately if the session is non-interactive:
    # If not running interactively, do not do anything:
    [[ $- != *i* ]] && return
  5. Save and close the file. Test your scp transfer 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.

  1. Add the capital -O flag to your command:
    scp -O file.tar.gz user@remote:/destination/
  2. The -O option 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.

  1. Pass TCP keepalive parameters directly on the command line:
    scp -o ServerAliveInterval=30 -o ServerAliveCountMax=5 file.tar.gz user@remote:/dest/
  2. This instructs your SSH client to send an encrypted keepalive ping through the tunnel every 30 seconds, preventing intermediate firewall drops.
  3. Alternatively, make this permanent by adding the lines to your local ~/.ssh/config file:
    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.

  1. Log into the remote machine and check available storage:
    df -h
  2. Check inode availability:
    df -i
  3. 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.

  1. Execute transfer via rsync:
    rsync -avz --partial --progress file.tar.gz user@remote:/destination/
  2. The --partial flag preserves partially downloaded blocks, allowing you to resume seamlessly without restarting from zero if a network drop occurs.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.