Synchronize Files Preserving Remote Ownership and Permissions with rsync

When managing files across different servers, you sometimes want to sync content from your local system to a remote server while leaving the remote ownership and permissions untouched. This guide walks you through using rsync to achieve exactly that.

Basic rsync Usage

Before diving into the specifics of preserving remote file attributes, let's look at a basic rsync command:

rsync -avz /path/to/local/dir/ user@remote_host:/path/to/remote/dir/

Here:

  • -a (archive mode) ensures that rsync recurses into directories and preserves symlinks, permissions, modification times, owner, group, and special files.
  • -v (verbose) provides detailed output of the transfer process.
  • -z (compress) compresses file data during transfer, which saves bandwidth over slow or metered connections. On fast local networks it adds CPU overhead for little benefit and can be omitted.

Preserving Remote Ownership and Permissions

In some cases, you might want rsync to sync files without altering the existing ownership and permissions on the remote host. This can be crucial for maintaining the integrity of file systems where permissions and ownership are carefully managed. To achieve this, you can use the --no-perms, --no-owner, and --no-group options:

rsync -avz --no-perms --no-owner --no-group /path/to/local/dir/ user@remote_host:/path/to/remote/dir/

Let's break down these additional options:

  • --no-perms: Prevents rsync from changing the file permissions on the remote host.
  • --no-owner: Ensures that the file owner on the remote host remains unchanged.
  • --no-group: Keeps the group ownership intact on the remote host.

By combining these options, rsync will transfer the files while preserving the existing permissions, ownership, and group settings on the remote server.

Note: When running as a non-root user (the typical case with user@remote_host), rsync cannot change remote ownership regardless — so --no-owner and --no-group are technically redundant in that scenario. They are still worth including explicitly to make the intent clear and to ensure correct behaviour if the command is ever run with elevated privileges.

Advanced: Deleting Remote Files Not Present Locally

In some synchronization scenarios, you may also want to ensure that files deleted locally are also removed from the remote host. This can be achieved with the --delete option:

rsync -avz --no-perms --no-owner --no-group --delete /path/to/local/dir/ user@remote_host:/path/to/remote/dir/

The --delete option tells rsync to remove files from the destination directory (remote host) that do not exist in the source directory (local host), thus ensuring an exact mirror of the local directory.

Warning: --delete causes irreversible data loss if the source path is wrong. Always do a dry run first to verify what will be deleted:

shell rsync -avz --no-perms --no-owner --no-group --delete --dry-run /path/to/local/dir/ user@remote_host:/path/to/remote/dir/

Practical Example

Let's put it all together with a practical example. Suppose you have a directory /home/user/documents/ on your local machine that you want to sync with /var/www/documents/ on a remote server example.com, while preserving the remote file attributes:

rsync -avz --no-perms --no-owner --no-group --delete /home/user/documents/ user@example.com:/var/www/documents/

This command will:

  1. Transfer files from /home/user/documents/ to /var/www/documents/ on the remote server.
  2. Preserve the existing permissions, ownership, and group on the remote server.
  3. Delete any files on the remote server that do not exist in the local directory.

Published by Ramiro Gómez on . Subscribe to the Geeksta RSS feed to be informed about new posts.

Tags: rsync sysadmin howto linux

Disclosure: External links on this website may contain affiliate IDs, which means that I earn a commission if you make a purchase using these links. This allows me to offer hopefully valuable content for free while keeping this website sustainable. For more information, please see the disclosure section on the about page.


Share post: Facebook LinkedIn Reddit Twitter

Merchandise