27

I would like to create a tar file with contents belonging to an owner:group pair who do not exist on the system from which the file is being made.

Here's the direction I've tried:

tar ca --owner='otherowner' --group='othergroup' mydata.tgz mydata

And when running this command, I get the following error:

tar: otherowner: Invalid owner
tar: Error is not recoverable: exiting now

Is there a way to force tar to accept the owner:group, even though neither of them exist on the system from which the file is being created?

1
  • 2
    Note that --owner is not an option supported by tar. This is a non-portable GNUism. With star, you e.g. use the built-in find and specify -chown username/userid -chgrp groupname/groupid.
    – schily
    Commented Aug 27, 2015 at 14:50

4 Answers 4

27

Linux doesn't use internally owners and groups names but numbers - UIDs and GIDs. Users and groups names are mapped from contents of /etc/passwd and /etc/group files for convenience of user. Since you don't have 'otherowner' entry in any of those files, Linux doesn't actually know which UID and GID should be assigned to a file. Let's try to pass a number instead:

$ tar cf archive.tar test.c --owner=0 --group=0
$ tar -tvf archive.tar 
-rw-rw-r-- root/root        45 2013-01-10 15:06 test.c
$ tar cf archive.tar test.c --owner=543543 --group=543543
$ tar -tvf archive.tar 
-rw-rw-r-- 543543/543543    45 2013-01-10 15:06 test.c

It seems to work.

4
  • Interesting! So the tar command must be polling the system for the user and group numbers to match the names I was trying to use. Thanks!
    – David
    Commented Jan 14, 2013 at 15:10
  • 3
    A note for others coming across this: tar automatically outputs user/group names when using the -tvf flags. To view the current numbers for files in an archive use a command like this: $ tar --numeric-owner -tvf archive.tar
    – David
    Commented Jan 14, 2013 at 15:23
  • 2
    Actually with my tar version I can enter any username I'd like and have it stored in the tar file (but with my numeric user id by default). While listing you get the usernames by default but the user ids using the --numeric-owner flag. What's more interesting is that you can set both using --owner=name:1234 or --group=groupname:4711. Source: function parse_owner_group of the tar source code
    – Bluehorn
    Commented Feb 2, 2017 at 18:25
  • 5
    It is probably worth noting that the ustar (standard on BSD and Linux) tar header encodes both numeric UID/GID values as well as the user and group names. When unpacking, the names are used first, if they're present in the header and match users and groups defined on the system. The numeric UID and GID will only be used as a fallback.
    – kbolino
    Commented May 10, 2019 at 17:17
2

Add the params --no-same-owner --no-same-permissions with tar. Take a look at the docs.

2
  • 3
    Those options are only used when extracting files from a tar archive -- not creating one. Commented Jan 25, 2019 at 12:34
  • You are right @AnthonyGeoghegan!
    – Bruno Wego
    Commented Jan 25, 2019 at 12:42
0

A tar archive can store both the names and numeric IDs for users and groups (though I’m not sure of this goes for all possible tar format flavors).

GNU tar accepts two options, --user=USER and --group=GROUP to set the user and group for a file.

Both USER and GROUP can be specified either by their name, by their numeric ID, or both (in the form NAME:ID).

Unless both name and ID are explicitly specified, the missing part is filled in by looking at the users/groups on the local system. Since there is no user named otherowner on your system, tar doesn’t know what UID to assign to the file.

Specifying the options as --owner=otherowner:1000 --group=othergroup:1000 should do the trick.

-1

Here is a piece of code to replace the user/group with ids on the fly:

tar ca --owner="$(id -u ***otherowner***)" --group="$(id -g ***othergroup***)" mydata.tgz mydata
1
  • 5
    No. If id knows how to resolve the name, tar knows too. The question is about a user name unknown to the system.
    – Daniel S
    Commented Aug 25, 2015 at 11:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .