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.
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 :
The rationale behind this has been nicely explained by @Gilles in this Unix & Linux answer:
So, even if you use tar's
--same-owner
flag, you will still need to extract the files asroot
to preserve ownership. That flag is on by default forroot
, so what you want is:There are 2 more options for tar that are interesting:
The 2nd one is the default so you can add
--same-owner
to save your user. You probably will have to do this withsudo
.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).
To preserve owner run as root or use the --same-owner flag alongside the -p flag when extracting.