rsync: mkstemp failed: Permission denied (13)

This is a wiki page. Be bold and improve it!

If you have any questions about the content on this page, don't hesitate to open a new ticket and we'll do our best to assist you.

Symptoms

Using rsync, you encounter such errors

rsync: delete_file: unlink(/my/path/foo.txt) failed: Permission denied (13)
rsync: mkstemp "/my/path/.foo.txt.SFXX1Y" failed: Permission denied (13)

Solutions

Basic permissions

Obviously, the rsync process must have POSIX read/write permissions on the filesystem sync'ing to. Check for the obvious first: what user does rsync run as, and does that user have read/write permissions?

Parent directory

Since rsync is creating temporary files (unless you use --inplace), the process must also have write permissions in the parent directory. In the above example, rsync should have write permissions to /my/path/.

SELinux

Do you use SELinux? If so, overrides POSIX permissions.

The following command shows that SELinux is not used in this system:

$ getenforce
bash: getenforce: command not found

If you do use SELinux you can disable by setting SELINUX=disabled in /etc/selinux/config.

Use as root

If rsync is embedded within another script, and if you run that script as root, it may still cause the aforementioned problem. Running your script as root is no guarantee that the rsync process itself will be run as root. So, depending on the script you use, either modify it as indicated below, or change the filesystem permissions to reflect the actual user who is running rsync.

If you use the script with sudo or as root, you might want to try:

--rsync-path='/usr/bin/sudo /usr/bin/rsync'

to make sure the script is running with the same elevated privileges.

Emerge

The above problem may occur with emerge --sync.
The portage repository must be owned by the user 'portage'.
Do:

# chown -R portage:portage /usr/portage"