Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Fix Rsync Connection Unexpectedly Closed Errors

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Rsync is a powerful file synchronization tool, but encountering an "unexpectedly closed" connection error can halt your transfers and leave you frustrated. This guide walks you through the most common causes and provides clear, actionable steps to diagnose and fix the issue, getting your syncs running smoothly again.

What You'll Need

  • Access to both the source and destination machines (local or remote).
  • SSH access to the remote server if you're using rsync over SSH (the default).
  • Basic command-line familiarity.
  • Permission to edit configuration files (e.g., /etc/rsyncd.conf or SSH configs) if needed.

Step-by-Step

  1. Check the rsync command syntax. The most common cause of an unexpected close is a typo in the source or destination path. Ensure you have the correct number of colons and slashes. For example, rsync -avz /local/folder/ user@host:/remote/folder/ is correct; missing the colon or adding an extra slash can break the connection.
  2. Test SSH connectivity separately. Run ssh user@host to see if you can log in. If SSH fails, rsync will too. Look for authentication errors, host key changes, or firewall blocks. If SSH works, the issue is likely rsync-specific.
  3. Run rsync with verbose and debug flags. Use rsync -avz --debug=ALL or rsync -avz -e 'ssh -v' to get detailed output. This will often reveal the exact point where the connection drops (e.g., during authentication, after the file list, or mid-transfer).
  4. Check for disk space and permissions. On the destination, ensure there's enough free space and that the target directory is writable by the user you're connecting as. Run df -h and ls -ld /path/to/dir to verify.
  5. Examine SSH server logs. On the remote server, check /var/log/auth.log (or /var/log/secure on RHEL-based systems) for messages like "Connection closed by authenticating user" or "error: Received disconnect". These logs often indicate authentication failures or forced closures.
  6. Verify rsync is installed on both ends. If the remote machine lacks rsync, the connection may close immediately. Install it with your package manager (e.g., apt install rsync or yum install rsync).
  7. Check firewall and network settings. If you're using a non-standard SSH port, ensure it's open. Also, some corporate networks block large transfers or long-lived connections. Try a smaller file to see if the issue is size-related.
  8. Look at rsync daemon configuration (if using rsync://). If you're connecting to an rsync daemon, verify /etc/rsyncd.conf has the correct module paths and that the path directive points to an existing directory. Also, check the uid and gid settings.
  9. Try a different SSH cipher or compression. Sometimes, SSH connection issues stem from cipher mismatches. Try rsync -avz -e 'ssh -c aes128-ctr' or disable compression with rsync -a --no-compress to rule out a bug in the compression library.
  10. Update rsync and SSH. Outdated versions may have bugs that cause unexpected disconnects. Update both client and server to the latest stable versions.

Common Mistakes

  • Trailing slashes: A trailing slash on the source directory means "copy the contents," while no slash means "copy the directory itself." Getting this wrong can cause rsync to behave unexpectedly, but it rarely causes a connection close—still, it's a common source of confusion.
  • Using the wrong SSH port: If you specify a custom port with -e 'ssh -p 2222', make sure the port is correct and reachable. A wrong port will cause a connection failure, not a close after connection.
  • Ignoring the rsync daemon's max connections: If you're using a daemon, the max connections setting might be hit, causing new connections to be dropped. Check the daemon's log.
  • Overlooking SELinux or AppArmor: On some Linux distributions, SELinux or AppArmor can block rsync's access to certain directories, causing the connection to close. Check audit logs (ausearch -m avc) or set SELinux to permissive temporarily to test.

Tips

  • Use a batch file for complex syncs: Write your rsync command to a script file to avoid typos and make it repeatable.
  • Set up SSH keys: Using key-based authentication eliminates password prompts and reduces the chance of authentication-related disconnects.
  • Use the --timeout option: Setting a timeout (e.g., --timeout=30) can help avoid hangs, but it won't fix a connection that closes unexpectedly.
  • Monitor network stability: If you're syncing over an unstable network, consider using --partial to keep partially transferred files, so you can resume later.

Troubleshooting

If the above steps don't resolve the issue, try these additional diagnostics:

  • Test with a minimal command: Run rsync -av /etc/hostname user@host:/tmp/ to see if a simple file transfers. If that works, the problem is with your specific files or paths.
  • Check for special characters in filenames: Files with newlines, non-UTF8 characters, or very long names can cause rsync to fail. Use --iconv or rename the files.
  • Try rsync with --no-whole-file or --whole-file: Sometimes the delta algorithm can cause issues with certain file types. Force whole-file transfer with -W to see if that helps.
  • Look at the server's rsync logs: If you're using a daemon, check /var/log/rsync.log for errors. For SSH-based transfers, the SSH logs on the server are your best bet.
  • Use strace on the client (if you have root): Run strace -f -o /tmp/rsync.strace rsync -avz src/ user@host:/dest/ and look for the last syscall before the error. This can reveal if the connection is being closed by a signal or an I/O error.

Frequently Asked Questions

Why does rsync say "connection unexpectedly closed" but SSH works fine?

This usually indicates that the SSH connection is established, but the rsync process on the remote side fails to start or exits immediately. Check that rsync is installed on the remote machine and that the remote user has execute permissions on it.

Can a firewall cause this error?

Yes, firewalls can drop long-lived connections or block the data channel. Try disabling the firewall temporarily (if you have control) or adjusting the idle timeout settings.

Does the error occur with large files only?

If it happens with large files, it could be a network timeout or a resource limit (like memory). Use --bwlimit to slow down the transfer or increase the SSH ServerAliveInterval to keep the connection alive.

How do I resume an interrupted rsync transfer?

Use --partial --append-verify to keep partially transferred files and resume from where it stopped. This doesn't fix the error but helps you recover.

Is it safe to ignore this error and just retry?

Retrying is fine, but if the error persists, you risk data corruption or incomplete transfers. It's better to diagnose the root cause before continuing.

Could a mismatch in rsync versions cause this?

Yes, major version differences can cause protocol incompatibilities. Update both ends to the same version or at least compatible ones.