Transferring gigabytes of backup archives, website directories, or remote databases using rsync is an indispensable Linux administration workflow. However, transfers frequently terminate abruptly with the error: rsync: connection unexpectedly closed (0 bytes received so far) [sender], accompanied by error code 12 or 255. When this happens, synchronisation aborts before writing a single byte to the destination disk. This error indicates that the underlying remote transport layer (typically SSH) or the remote rsync helper process terminated unexpectedly during initial connection negotiation.
Why Does rsync Close Connections Unexpectedly?
The premature termination occurs before file delta scanning begins:
- Missing rsync Binary on Remote Host: Initiating an rsync over SSH session requires the remote machine to execute
rsync --server. If rsync is not installed on the destination server, SSH drops the channel immediately. - SSH Shell Profile Echo / Banner Output: Non-interactive SSH sessions executing
~/.bashrcscripts on the remote host that output motd text or echo statements, corrupting the rsync binary stream. - Strict Firewall or MTU Black Hole Packet Drops: Network routers or cloud security groups terminating idle SSH connections during initial tree enumeration.
- Incorrect File Permissions or Sudo Requirements: Destination directory ownership denying write access to the connecting user.
Step 1: Verify rsync Is Installed on the Remote Machine
Ensure the destination host has the rsync executable available in its system PATH.
- Connect to the remote host directly via SSH:
ssh username@remote-server-ip - Check if rsync is installed:
which rsync || rsync --version - If rsync is missing, install it:
# Ubuntu / Debian: sudo apt update && sudo apt install -y rsync # RHEL / Rocky / AlmaLinux: sudo dnf install -y rsync
Step 2: Inspect Remote .bashrc for Non-Interactive Output
Ensure login scripts do not echo text during non-interactive SSH automated sessions.
- On the remote host, inspect
~/.bashrc:head -n 15 ~/.bashrc - Ensure that any custom
echo,neofetch, or interactive commands are placed after this standard non-interactive check:# If not running interactively, don't do anything case $- in *i*) ;; *) return;; esac - Save the file and test rsync from your local workstation.
Step 3: Run rsync with Verbose Diagnostics and SSH Keepalive
Isolate exact handshake drop points using verbose debugging flags.
- Append the
-vvvflag and configure SSH transport keepalives:rsync -avz -vvv -e "ssh -o ServerAliveInterval=30 -o ServerAliveCountMax=5" /local/source/ username@remote-server-ip:/remote/destination/ - Examine the verbose debug output to confirm whether the connection dropped due to public-key authentication failure, network timeout, or directory permission rejection.