modify file ownership for files inside tar archive
Solution 1
How can I create a tar.xz archive, so that the files have root ownership when unpacked by root?
That's up to the root who unpacks:
tar --no-same-owner -xf ...
If you want to make them all root to start with, you can use
tar --owner=root --group=root -cf ...
Solution 2
Fakeroot
The fakeroot utility, or the newer utility fakeroot-ng (same purpose, different implementation technique) runs a program and pretends to the program that it is running as root and that system calls such as chown
succeeded. Only the program believes that these calls succeeded, nothing is actually reflected in the filesystem (it can't be since fakeroot
has no extra privileges). However, if the program changes the ownership of a file and then takes some action based on the ownership of that file, this can change the behavior of the program.
A typical way to get useful work out of fakeroot by running a fakeroot environment where the following happens:
- Create some files, move them around, change their ownership and modes, etc.
- Create an archive of these files.
Example:
fakeroot sh -c '
chown root:root usr/bin/foo
tar cf foo.tar usr
'
You need to use a single invocation of fakeroot
, since there is no memory between invocations.
Linux namespaces
Just for completeness, I'll mention that if you have a Linux kernel ≥3.8, then namespaces are another way to create a pretend-root environment. The userland support isn't quite there yet so I won't go into more detail.
Mount the archive
A different way to solve your problem is to mount the archive as a directory. You can use archivemount, which is capable of modifying several archive formats via libarchive, including compressed tar.
mkdir mnt
archivemount foo.tar.xz mnt
chown root:root mnt/usr/bin/foo
fusermount -u mnt
user1968963
Updated on September 18, 2022Comments
-
user1968963 almost 2 years
I am working as user, and I would like to create a tar archive, which when unpacked (by root) will extract its files with root ownership (otherwise root would have to change the ownership manually for each file, after the files have been extracted to its destination).
I have found
fakeroot
which seems to do exactly that. But i am not able to find the syntax which I need to use to create my archive.How can I create a tar.xz archive, so that the files have root ownership when unpacked by root?
do something with fakeroot ... tar cfpJ foo.tar.xz foo/
-
user1968963 about 10 yearsthat does not work for me:
tar --owner=root --group=root cfpJ files.tar.xz files/
gives me errortar: You must specify one of the
-Acdtrux' or--test-label' options Try
tar --help' ortar --usage' for more information.
-
goldilocks about 10 yearsYou need a
-
before your short options string:tar --owner=root --group=root -cfpJ ...
I.e.-cfpJ
, notcfpJ
. -
user1968963 about 10 years
tar --owner=root --group=root -cfpJ files.tar.xz files/
gives me another error:tar: files.tar.xz: Cannot stat: No such file or directory tar: Exiting with failure status due to previous errors
and moreover, it creates a file calledpJ
. -
goldilocks about 10 yearsOkay. I always put the
f
at the end (because it makes more sense, intuitively) and low and behold, I get the same failure with-cfpJ
BUT-cpJf
works. -
Olivier Dulac over 8 years@user1968963: f should always be just before the filename, as it means "the next parameter is the filename". If you don't put it just before the filename, tar will think the filename is "", and then it tries to open a "" file, which it cannot stat (of course).