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