Creating a tarball for distribution (without user/group information)
A tarball is the common way to convey several files on UNIX systems. But because tar was originally intended for backup, it stores not only the permission information, but also the owner and group of each file. Try listing the content of a tarball with e.g.
$ tar -tzvf thestuff.tar.gz
Note the “v” flag that goes along with the flag for listing, “t”: It causes tar to print out ownership and permission information.
This doesn’t matter much if the tarball is extracted as a non-root user on the other end, because tar doesn’t set the user and group ID in that case: The extracted files get the uid/gid of the process that extracted them.
However if user at the other end extract the tarball as root, the original uid/gid is assigned, which may turn out confusing.
To avoid this, tell tar to assign user root to all files in the archive. This makes no difference if the archive is extracted by a non-root user, but sets the ownership to root if extracted by root. In fact, it sets the ownership to the extracting user in both cases, which is what one would expect.
So this is the command to use to create an old-school .tar.gz tarball:
$ tar --owner=0 --group=0 --mode='og-w' -czf thestuff.tar.gz thestuff
Note that you don’t have to be root to do this. You’re just creating a plain file with your own ownership. It’s extracting these file as root that requires root permissions (if so desired).
The –mode part changes turns off write permission for everyone except the user, so plain files get 0644 (at most).