[SOLVED] rsync error: error in rsync protocol data stream (code 12) connection unexpectedly closed
While transferring backups or replicating directory trees over SSH, your transfer abruptly aborts with rsync: connection unexpectedly closed (0 bytes received so far) [sender] followed by rsync error: error in rsync protocol data stream (code 12) at io.c. Exit code 12 signifies that the bidirectional data stream between the local sender and remote receiver process collapsed prematurely.
Quick Diagnostics
Quick Solution (1 Minute):
- Verify remote rsync binary availability:
ssh user@server "which rsync"- Run transfer with resilient TCP keepalive options:
rsync -avzP -e "ssh -o ServerAliveInterval=30" src/ user@server:/dest/
Step-by-Step Solution
-
1
Step 1: Verify and Install rsync on Remote Destination Host
The most common root cause when rsync fails instantly at 0 bytes transferred is the absence of the rsync executable on the target machine:
BASH# On remote Ubuntu / Debian sudo apt update && sudo apt install -y rsync # On remote RHEL / Rocky Linux / AlmaLinux sudo dnf install -y rsync -
2
Step 2: Block Stateful NAT Dropouts with SSH KeepAlive Flags
When syncing multi-gigabyte ISOs or databases, intermediate firewalls frequently drop connections deemed idle. Enforce active probing packets:
BASHrsync -avzP -e "ssh -o ServerAliveInterval=15 -o ServerAliveCountMax=4" /local/dir/ user@server:/remote/dir/ -
3
Step 3: Enable Resumable Partial Transfers (-P)
Prevent network blips from forcing a complete restart of huge transfers by adding the
-Pargument (combines--partialand--progress):BASHrsync -avhP /var/backups/ user@server:/backups/If a network disconnect occurs, re-running the command resumes transmission from the exact byte where it paused.
Frequently Asked Questions
Why does rsync trigger Out of Memory before code 12 on budget VPSs?
Building recursive in-memory file indexes for millions of files exhausts constrained RAM. Pass --no-inc-recursive or sync partitioned subtrees.
How can I inspect the exact reason for the abrupt termination?
Invoke rsync in hyper-verbose mode combined with SSH debugging: rsync -vvv -e "ssh -vvv" ... to expose the initiating termination signal.
Prevention Advice
Recommended security practices:
- Add
ClientAliveInterval 30inside remote/etc/ssh/sshd_configto sustain background batch transfers. - For enterprise-scale migrations, wrap rsync commands inside detachable sessions using
tmuxorscreen.