What does the -p flag actually preserve when creating and extracting a tarball? Is it the rwx permissions it preserves?

When I created a htdocs/ tarball owned by root, extracting it to my local machine changed the ownership from root to my user.

  • 5
    Maybe you should try to extract the archive as root. It's not possible to set permissions for another user as a normal user so I think tar won't be able to do so as well. Please tell me if this worked. May 9 '14 at 11:32
  • "local machine"? Please clarify what OS this machine has ;)
    – Rinzwind
    May 9 '14 at 11:39
  • Also tell us what filesystems you are using. Both the source and target ones.
    – terdon
    May 9 '14 at 11:40
  • Source: Red Hat Enterprise Linux Server release 6.5 (Santiago) Target: Ubuntu 13.10 Both use GPT
    – nicoX
    May 9 '14 at 12:47
  • 1
    Side note: tar preserves permissions. -p and --same-owner / --no-same-owner (as in answers) are extraction flags. They have no effect while archiving
    – Fr0zenFyr
    Aug 5 '19 at 14:20

Ownership and permissions are two different things. The -p flag preserves permissions. On *nix systems, regular users can't change file ownership to a user who is not themselves.

As explained here :

Only processes with an effective user ID equal to the user ID of the file or with appropriate privileges may change the ownership of a file. If _POSIX_CHOWN_RESTRICTED is in effect for path:

  • Changing the user ID is restricted to processes with appropriate privileges.

  • Changing the group ID is permitted to a process with an effective user ID equal to the user ID of the file, but without appropriate privileges, if and only if owner is equal to the file's user ID or ( uid_t)-1 and group is equal either to the calling process' effective group ID or to one of its supplementary group IDs.

The rationale behind this has been nicely explained by @Gilles in this Unix & Linux answer:

The reason for this restriction is that giving away a file to another user can allow bad things to happen in uncommon, but still important situations. For example:

  • If a system has disk quotas enabled, Alice could create a world-writable file under a directory accessible only by her (so no one else could access that world-writable directory), and then run chown to make that file owned by another user Bill. The file would then count under Bill's disk quota even though only Alice can use the file.
  • If Alice gives away a file to Bill, there is no trace that Bill didn't create that file. This can be a problem if the file contains illegal or otherwise compromising data.
  • Some programs require that their input file belongs to a particular user in order to authenticate a request (for example, the file contains some instructions that the program will perform on behalf of that user). This is usually not a secure design, because even if Bill created a file containing syntactically correct instructions, he might not have intended to execute them at this particular time. Nonetheless, allowing Alice to create a file with arbitrary content and have it taken as input from Bill can only make things worse.

So, even if you use tar's --same-owner flag, you will still need to extract the files as root to preserve ownership. That flag is on by default for root, so what you want is:

sudo tar xpf foo.tgz

There are 2 more options for tar that are interesting:

       try extracting files with the same ownership as exists in the ar‐
       chive (default for superuser)

       extract files as yourself (**default for ordinary users**)

The 2nd one is the default so you can add --same-owner to save your user. You probably will have to do this with sudo.

Besides that: this will -only- work on systems that support POSIX. And operating systems other than Ubuntu might not have these 2 options (they are not standard).

  • 4
    The --same-owner won't work for non-root users, they won't have the right to set the ownership to anyone but themselves. That's defined by POSIX.
    – terdon
    May 9 '14 at 12:00

To preserve owner run as root or use the --same-owner flag alongside the -p flag when extracting.

  • 5
    It needs to be run as root in any case. Non-root users can't change file ownership to other users.
    – terdon
    May 9 '14 at 11:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.