/errors/rsync/connection-unexpectedly-closed-code-12
rsync: connection unexpectedly closed (0 bytes received so far): error in protocol data stream (code 12)
- tool
- rsync
- exit code
- 12
- versions
- rsync 3.x
- os
- linux, macos
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(NNN) means the rsync process at the other end of the SSH connection stopped, or never started, so the local rsync got no valid data. With 0 bytes received, it failed at the start: most often rsync is not installed on the remote host, SSH authentication failed, or a login script printed text. Read the line printed before these two; it names the real cause.
The file and line in at io.c(NNN) and the version in brackets differ between builds; the message text and exit status 12 do not.
Find the cause
Run the SSH part alone; it must print only the rsync version, with no extra text before it:
ssh user@host rsync --version
ssh user@host true | wc -c # must print 0: anything else is output from startup files| Line before the error | Cause | Fix |
|---|---|---|
| bash: rsync: command not found | rsync missing on the remote | Install it there (apt install rsync, dnf install rsync), or use --rsync-path |
| Permission denied (publickey) | SSH login failed | Fix the key or user; test with ssh -v user@host |
| protocol version mismatch -- is your shell clean? | A startup file prints text | Silence .bashrc for non-interactive shells |
| No space left on device | Destination disk full | Free space, then rerun with --partial |
| Nothing, after a long transfer | Remote rsync killed (timeout, OOM, reboot) | Check remote logs; add --timeout and retry |
Fix: rsync not found on the remote
# rsync installed in a non-standard place on the remote
rsync -av --rsync-path=/usr/local/bin/rsync ./site/ user@host:/var/www/site/
# rsync must run as root on the remote (needs sudo without a password prompt there)
rsync -av --rsync-path="sudo rsync" ./site/ user@host:/var/www/site/Fix: a noisy shell startup file
rsync talks to the remote rsync over the SSH session's standard output, so any echo in .bashrc breaks the protocol. Guard interactive-only output at the top of the remote ~/.bashrc:
case $- in
*i*) ;; # interactive: continue
*) return ;; # rsync, scp and ssh host command: stop here
esac