Tar Of Files

Home » CentOS » Tar Of Files
CentOS 7 Comments

When I “tar” up an archive the files have an owner bob, when I extract that to another machine bob is there also but user number is different. So when I extract bob is no longer the owner of the files but someone else.

Is there a good way to account for this ?
User ID on one box being different to the next box ?

I was expecting to untar and bob still be the owner .

Thanks,

Jerry

7 thoughts on - Tar Of Files

  • either assign the same UID/GIDs on both boxes to the same user, or do a chown -R after you untar-ed it.

    (also, it helps to have users use the same ‘moniker’ cross platforms.)

  • If you pack and extract as root, then numeric UID will be the same. But on different systems there may be different usernames that have that numeric UID.

    Incidentally: sometimes we enable SGID bit on a directory (to have everything that is created in it inherit the group that directory belongs to). tar will be one of the tools that will break it: extracted archive will have group as it was in the archived original (numeric GID
    that is, if both archiving and un-archiving was performed by root).

    I hope, this helps.

    Valeri

  • Does anyone understand why there exists a “–numeric-owner” flag? I replied off-list with the same information as others posted here, but the existence and docs for that flag at least imply that the default is usernames, not UIDs.

    Noam

  • Regular users don’t have the ability to change file ownership (only group, assuming they’re a member of the target group and own the file), so this is mainly a consideration if you’re running tar as root. By default, if you’re running as root, GNU tar assumes the –same-owner switch, which preserves the ownership in the archive. If the IDs are different, you can use –owner or –owner-map to translate the IDs. If you need to get even trickier, you can use –to-command and pass the stream to your own custom filter.

  • I think on linux (most systems I ran into actually), UIDs and GIDs are numerical with just a “human friendly” translation (from passwd/group.  
    If you extract a tar file, for example, and the  owner/group
    (numerically) does not exist on the target system, you get to see the 
    ‘old’ uid/gid from the src system.

  • The system has no way of knowing that bob:uid1 and bob:uid2 are the same person and in fact on a lot of systems they are not (aka if you downloaded a tar ball from my box and it had the user bob on it.. it definitely is not your bob).

    The ways to do this are any of the following or a list of ones not there.
    1. find ./tree_you_untarred -uid -print0 | xargs -0 chown bob2
    2. su bob2 (cd /tree_place_to_untar; tar xzvf )
    3. use gnu tar’s many options to create the tar file with the correct uid/gid you wanted using –owner –group (or a map if there are multiple).

  • In order to answer that question, we need to know what system is creating the tar file and what system is extracting the file. There are several formats for tar archives (see the –format option to GNU tar for a list), and tar will have different options on different platforms.

    This process works the way you expect, by default, on CentOS systems.

    I can demonstrate that by creating a CentOS container, adding a user and creating a tar archive with a file owned by that user.  A new container, with users that have different numeric IDs will extract that archive and preserve the file’s ownership by name and not numeric IDs:

    $ podman run -v $(pwd):$(pwd):z -w $(pwd) -it CentOS:8
    [root@9d60934a7390 tartest]# useradd user1
    [root@9d60934a7390 tartest]# touch user1file
    [root@9d60934a7390 tartest]# chown user1 user1file
    [root@9d60934a7390 tartest]# tar cf user1.tar user1file
    [root@9d60934a7390 tartest]# exit

    $ podman run -v $(pwd):$(pwd):z -w $(pwd) -it CentOS:8
    [root@49a5818ef7a6 tartest]# useradd user2
    [root@49a5818ef7a6 tartest]# useradd user1
    [root@49a5818ef7a6 tartest]# rm user1file rm: remove regular empty file ‘user1file’? y
    [root@49a5818ef7a6 tartest]# tar xf user1.tar
    [root@49a5818ef7a6 tartest]# ls -l total 12
    -rw-r–r–. 1 root  root 10240 Mar  3 19:21 user1.tar
    -rw-r–r–. 1 user1 root     0 Mar  3 19:21 user1file