I need to backup some data with the "p" option on tar command. The problem is the place I'm going to restore this data will have all the same users, but those users may have different IDs. Does that make any difference to tar or will it restore permissions correctly by user name?
4 Answers
Summing up previous answers and adding some important information:
When creating archives,
tar
will always preserve files' user and group ID, unless told otherwise with--owner=NAME
,--group=NAME
. So there will always be a user and group associated with each file.GNU
tar
, and perhaps other versions oftar
, also store the user and group names, unless--numeric-owner
is used.bsdtar
also stores user and group names by default, but it had no support for--numeric-owner
option when creating archives until version 3.0 (note that it supported the option when extracting archives since much longer).When extracting as a regular user, all files will always be owned by the user. And it can't be any different, since extracting a file means creating a new one on the filesystem, and a regular user cannot create a file and give ownership to someone else.
When extracting as root,
tar
will by default restore ownership of extracted files, unless--no-same-owner
is used, which will give ownership to root himself.In GNU tar, bsdtar, and perhaps other versions of
tar
, restored ownership is done by user (and group) name, if that information is in the archive and there is a matching user in the destination system. Otherwise, it restores by ID. If--numeric-owner
option is provided, user and group names are ignored.Permissions and timestamps are also saved to the archive, and restored by default, unless options
--no-same-permissions
and/or--touch
are used. When extracted by the user, user'sumask
is subtracted from permissions unless--same-permissions
is used.--preserve-permissions
and--same-permissions
are aliases, and have the same functionality as-p
Hope this helps clarify the issue! :)

- 1,593
- 12
- 11
-
4Excellent answer; Answers this question as well as every other question that may arise on the subject. – user1107893 Feb 09 '14 at 17:09
-
It should be noted that only recent versions of GNU `tar` allow specifying arbitrary names in `--owner` or `--group`, in the past `tar` did a gratuitous lookup in the current machine `/etc/passwd` and refused to run if there was no match. – Matteo Italia Oct 31 '14 at 13:54
-
What happens if you create an archive with specified name with `--owner` but also added in the `--numeric-owner` flag? How does tar deal with these competing requirements? – CMCDragonkai Aug 01 '16 at 11:08
-
@CMCDragonkai : `--owner` and `--numeric-owner` are not mutually exclusive, and serve very distinct purposes: `--owner=USERNAME` will override the files and dirs owner(s) when archiving the files, while `--numeric-owner` will simply not store the username, just his numerical ID. – MestreLion Aug 23 '16 at 04:30
tar
records permissions based on the UID and GID, not on the string associated with them. So if the UID on one server was 3300 and that was linked to 'bob', on the new server the file will be owned by the user who has the UID 3300.
Virtual everything (I want to say everything, but you can never be 100% sure) on UNIX uses the UID:GID values, because that's what is actually stored at the filesystem level. The name is just a simple lookup in the passwd file, the underlying checks are done using the numeric values.

- 8,305
- 9
- 44
- 87

- 9,311
- 1
- 34
- 46
-
Ah, that is not good ... I guess in most situations it's suitable. Unfortunately, not for me ... Thank you, EightBitTony. – Marius Oct 19 '12 at 07:21
-
3You most likely mean GID (group identifier), not GUID (Globally Unique Identifier). – user Oct 19 '12 at 12:35
-
8GNU tar does save the user/group name data, too, because I can see it if I list an archive on a machine that doesn't have those users. There must be a way to get it to use that during extraction. – Rob H Sep 24 '13 at 13:32
-
5This answer is simply incorrect. `tar` *does* record owner names. – Steffen Heil Apr 18 '18 at 09:18
-
tar on ubuntu 16.04+ clearly does _not_ store gid or uid... but instead the user/group names. This became a problem when I had to restore a backup of a server from a live environment that did not use the same name-id mapping. Upon booting into the restored system it was just a litany of permissions errors. I presume this to be the case on most GNU systems. The hacky solution I came up with was to over-write the passwd and group files in the live environment with those from the back up before running tar. Then promptly reboot to restore them. – Cliff Armstrong Aug 17 '21 at 19:10
User the --same-owner option to GNU tar. See http://www.gnu.org/software/tar/manual/html_section/Attributes.html

- 302
- 2
- 16
-
1This is documented as the default for superusers, and seems to answer the OP's question differently than the accepted answer. (The link says that when GNU tar restores using --same-owner, it looks first for *names* in /etc/passwd.) The only outstanding issue is whether the OP's version of tar implements --same-owner. – Mike Sherrill 'Cat Recall' Oct 19 '12 at 13:17
-
The OP is using some Linux distro so better than even chance of using GNU tar, methinks. And going by the documentation it *is* possible while the accepted answer indicates it's not... – Colin 't Hart Oct 19 '12 at 15:36
-
@Catcall - sorry, i accepted the answer without even having a chance to test it. Somethimes i just blindly trust people. Still, the person who answered was kinda right, since i wasn't restoring with "--same-owner" and then you've added to the answer. Too bad i can't accept both. I'm using debian squeeze which indeed does support "--same-owner". Thanks for the tip. – Marius Oct 22 '12 at 12:59
-
@Marius: I'm pretty sure you can change the accepted answer whenever you like. (I'll just point out that I haven't provided any answers to this question, only comments. I don't have any rep at stake.) – Mike Sherrill 'Cat Recall' Oct 22 '12 at 15:09
If you are trying to transfer files between two systems, rsync will by default set the permissions by username instead of uid, looking at the usernames at both ends. Only if the user doesn't exist on one of the systems will it copy it with the uid, unless you tell it otherwise.

- 17,859
- 14
- 72
- 103
-
This doesn't directly answer the OP's question, but anyone asking the OP's question ought to learn this, too. – Mike Sherrill 'Cat Recall' Oct 19 '12 at 13:18