What does tar's -p (preserve permissions) flag actually preserve?

Solution 1:

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

Solution 2:

There are 2 more options for tar that are interesting:

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

--no-same-owner
       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).

Solution 3:

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