Linux Tar – Owner Map When Extracting

archivinglinuxtar

Tar has several options to affect how file and directory ownership will be stored in that tar file. For example –numeric-owner, or –owner-map. These options directly affect how the tar file will be extracted, or what will be done on extraction assuming we run extraction as root.

  • Without options, tar will find corresponding group/user in current system and set the ownership to that group/user even if the uid/gid are different then they were when creating the archive.
  • With –numeric-owner, the extraction will always set uid/gid to the same as it was when we created the archive
  • With –owner-map, we can provide our own mapping for users/uids, but only when we are creating the archive. Using this option does nothing when extracting the archive.

These options are great, but I need to be able to affect what tar does with ownerships when extracting, at the time I am extracting the archive, not when I am creating the archive. The –owner-map seemed like a perfect solution to me, until I found it does nothing when it is provided when extracting.

So my question is: Is it somehow possible to control with what ownerships will tar extract the files and directories, at the time when we are extracting the archive?

I could not find anything relevant. As a solution I attempted to create chroot environment with /etc/passwd and /etc/group that would ensure the correct mapping, and then extract the tar archive under the chroot, but it did not work. I assume it is because the chroot still takes users and groups from host system somehow, but it feels like this could lead to a solution.

I would appreciate any kind of help or ideas, even if they are hacky (I think replacing /etc/passwd and /etc/group in the system I am extracting in would work, but that is not acceptable for me).

Best Answer

When extracting files as root, tar by default will use the original ownership.

You can override that using the --no-same-owner option (alternatively -o), to create files owned by the user who launched tar.

If you wish to extract as another user, you will need to become this user, using su, and extract with the --no-same-owner option.

Related Question