File ownership preservation with .tgz

archivepermissionstarunix-philosophy

Back with another probably very very basic UNIX question.

I understand the premise of Tape Archive Zips (.tgz) is that they preserve uid, gid, permissions…

However, it seems that this isn't portable. For example, what if the user john makes a .tgz on one UNIX machine and unzips on a machine without that user, or with a user with the same name but different UID.

How does this work?

Best Answer

Really old tar formats only store numerical user and group identifiers, and so they have the problem you describe.

However beginning with the POSIX standard from 1988, tar formats such as the Unix standard tar format or pax also store the username and group name, so they can preserve ownership by name. Given a tarball containing a file owned by uid 1234, with username john, tar will look for a user named john, and extract the file with that ownership if possible (potentially with a uid that’s not 1234), falling back to uid 1234 if there is no such user.

None of this is perfect, which is why tar doesn’t restore ownership unless run as root (aside from the fact that it needs to be root to change ownership anyway); by default files are extracted with the ownership of the running user.

Related Question