Skip to content

Linux Rsync Permission

DodaTech 2 min read

In this tutorial, you'll learn about Linux rsync Permission Denied Fix. We cover key concepts, practical examples, and best practices.

rsync fails with "Permission denied (publickey)", "Operation not permitted", or "rsync: mkstemp failed" when the SSH connection fails, the destination directory is not writable, or file ownership conflicts with the remote user.

The Problem

rsync -avz ./files/ user@server:/var/www/

Error:

rsync: mkstemp "/var/www/.files.txt.abc123" failed: Permission denied (13)

Or:

Permission denied (publickey).
rsync: connection unexpectedly closed (0 bytes received so far) [sender]

Wrong Approach

# WRONG — ignoring permissions with sudo inside rsync (does not work)
rsync -avz --rsync-path="sudo rsync" ./files/ user@server:/var/www/

# WRONG — running rsync as root from the start
sudo rsync -avz ./files/ user@server:/var/www/

Right Approach

# Ensure the user has write permissions on the destination
ssh user@server "sudo mkdir -p /var/www && sudo chown user:user /var/www"
rsync -avz ./files/ user@server:/var/www/

Expected output:

sending incremental file list
./
file1.txt
file2.txt

sent 1234 bytes  received 45 bytes  2558.00 bytes/sec
total size is 5678  speedup is 4.44

Step-by-Step Fix

Step 1: Test SSH connection

ssh user@server echo "SSH OK"

Step 2: Test write permission on destination

ssh user@server "touch /var/www/test.txt && rm /var/www/test.txt"

Step 3: Fix destination permissions

ssh user@server "sudo chown -R user:user /var/www && sudo chmod -R 755 /var/www"

Step 4: Use rsync with sudo on the remote side

rsync -avz --rsync-path="sudo rsync" ./files/ user@server:/var/www/

Step 5: Preserve permissions with the correct flags

rsync -avz --chown=user:group ./files/ user@server:/var/www/

Step 6: Handle file ownership changes

rsync -avz --no-owner --no-group ./files/ user@server:/var/www/

Step 7: Use a temporary directory for permission-sensitive transfers

rsync -avz ./files/ user@server:/tmp/staging/
ssh user@server "sudo cp -a /tmp/staging/* /var/www/ && sudo rm -rf /tmp/staging"

Prevention Tips

  • Ensure the remote user owns the destination directory
  • Use --chown=user:group to set ownership during transfer
  • Test write access before running large syncs
  • Use --dry-run to preview changes before executing
  • Configure sudoers for passwordless rsync in automation scripts

Common Mistakes with rsync permission

  1. Using foldl instead of foldl' causing stack overflow on large lists
  2. Forgetting deriving (Show, Eq) on custom data types needed for debugging
  3. Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable

These mistakes appear frequently in real-world LINUX code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### Why does rsync fail with "mkstemp failed"?

The remote user does not have write permission on the destination directory. rsync creates temporary files with .xxxxxx suffixes before renaming them. Check the destination directory ownership and permissions with ls -la /var/www/.

How do I preserve file permissions with rsync?

Use -a (archive mode) which preserves permissions, ownership, timestamps, and symlinks. Add --chown=user:group to override ownership. Note that only root can preserve user/group ownership across systems.

Can rsync resume interrupted transfers?

Yes. Use --partial or --append-verify to resume partially transferred files. --partial keeps the partial file; --append-verify appends data to it and verifies the checksum. This is useful for large file transfers over unstable connections.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro