Thursday, May 9, 2024
30
rated 0 times [  30] [ 0]  / answers: 1 / hits: 96394  / 3 Years ago, thu, may 13, 2021, 10:18:48

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.


More From » command-line

 Answers
7

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

[#25329] Friday, May 14, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
splenueak

Total Points: 448
Total Questions: 118
Total Answers: 110

Location: Vanuatu
Member since Mon, Oct 3, 2022
2 Years ago
;