How to tar a directory preserving not only permissions, but ownership too

The owners of the file is preserved by default.

When extracting you need to use --same-owner flag. Such as tar --same-owner -xvf file.tar although the flag is only recommended for super users.

Check the tar man page.


I have to compress a directory using tar.gz preserving not only permissions, but ownership/groups too.

By default, tar will preserve file permissions and ownership when creating the archive.

To extract file permissions and ownership, you will need to run tar as root when extracting, since changing file ownership usually requires superuser privileges. See this question for more information.