When using rsync to transfer files between servers, maintaining proper file ownership is crucial for system security and functionality. A common pitfall occurs when the --owner
and --group
options don't seem to have any effect during SSH transfers.
Here's the problematic command we're examining:
sudo rsync -rlptDvz --owner=cmsseren --group=cmsseren \
/home/serena/public_html/ -e ssh root@ip:/home/cmsseren/public_html2/
Several factors can prevent ownership modification:
- The remote server's SSH configuration (typically in
/etc/ssh/sshd_config
) may havePermitRootLogin without-password
which restricts ownership changes - The target user/group might not exist on the remote system
- The rsync process might not have sufficient privileges on the remote end
Option 1: Use --chown Parameter (Modern Rsync)
For rsync 3.1.1+ (check with rsync --version
):
sudo rsync -rlptDvz --chown=cmsseren:cmsseren \
/home/serena/public_html/ -e ssh root@ip:/home/cmsseren/public_html2/
Option 2: Post-Sync Ownership Change
First sync files, then modify ownership:
# Sync files
sudo rsync -rlptDvz /home/serena/public_html/ -e ssh root@ip:/home/cmsseren/public_html2/
# Change ownership remotely
ssh root@ip 'chown -R cmsseren:cmsseren /home/cmsseren/public_html2/'
Option 3: Use Intermediate Tar Stream
This preserves permissions through the transfer:
sudo tar cf - -C /home/serena/public_html/ . | \
ssh root@ip 'tar xf - -C /home/cmsseren/public_html2/ && \
chown -R cmsseren:cmsseren /home/cmsseren/public_html2/'
- Always verify user/group existence on both systems
- Consider using non-root transfers when possible
- Test with
--dry-run
first to verify expected changes - Check
/etc/rsyncd.conf
on remote if using rsync daemon
- Run
id cmsseren
on both systems to verify UID/GID - Check
ssh root@ip 'ls -ld /home/cmsseren'
- Test with
rsync --version
on both ends - Add
--no-perms --no-owner --no-group
temporarily to isolate the issue
Many developers encounter this frustrating scenario: you're using rsync to transfer files between servers, but despite specifying --owner
and --group
flags, the ownership permissions don't change on the destination files.
sudo rsync -rlptDvz --owner=cmsseren --group=cmsseren /home/serena/public_html/ -e ssh root@ip:/home/cmsseren/public_html2/
The root cause lies in how rsync handles permissions. By default, rsync preserves the original file permissions unless explicitly told otherwise. The --owner
and --group
options only work when:
- You're running rsync as root
- The destination filesystem supports ownership changes
- You're not using
-a
flag (which implies-o -g
)
Here are three working approaches:
1. Use --chown flag (rsync 3.1.1+)
sudo rsync -rlptDvz --chown=cmsseren:cmsseren /source/ user@host:/destination/
2. Combine with --no-perms
sudo rsync -rlptDvz --no-perms --chown=cmsseren:cmsseren /source/ user@host:/destination/
3. Post-sync chown command
sudo rsync -rlptDvz /source/ user@host:/destination/
ssh user@host "sudo chown -R cmsseren:cmsseren /destination/"
Remember that:
- The destination user must exist on the remote system
- You need proper sudo privileges on both systems
- NFS-mounted directories might have additional restrictions
Always verify with a small test directory first:
mkdir test_dir
touch test_dir/test_file
sudo rsync -v --chown=testuser:testgroup test_dir/ remote:/tmp/test_dir/
ssh remote "ls -l /tmp/test_dir/"