Linux Rsync Permission
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:groupto set ownership during transfer - Test write access before running large syncs
- Use
--dry-runto preview changes before executing - Configure sudoers for passwordless rsync in automation scripts
Common Mistakes with rsync permission
- Using
foldlinstead offoldl'causing stack overflow on large lists - Forgetting
deriving (Show, Eq)on custom data types needed for debugging - 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
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro