Skip to content

/errors/rsync/connection-unexpectedly-closed-code-12

rsync: connection unexpectedly closed (0 bytes received so far): error in protocol data stream (code 12)

The commands on this page have not been executed by our verification runner yet.updated
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:

bash
ssh user@host rsync --version
ssh user@host true | wc -c        # must print 0: anything else is output from startup files
Line before the errorCauseFix
bash: rsync: command not foundrsync missing on the remoteInstall it there (apt install rsync, dnf install rsync), or use --rsync-path
Permission denied (publickey)SSH login failedFix the key or user; test with ssh -v user@host
protocol version mismatch -- is your shell clean?A startup file prints textSilence .bashrc for non-interactive shells
No space left on deviceDestination disk fullFree space, then rerun with --partial
Nothing, after a long transferRemote rsync killed (timeout, OOM, reboot)Check remote logs; add --timeout and retry

Fix: rsync not found on the remote

bash
# 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:

~/.bashrc on the remote host
case $- in
  *i*) ;;        # interactive: continue
  *) return ;;   # rsync, scp and ssh host command: stop here
esac

Sources