user4209701
user4209701

Reputation:

Running sudo inside script sets uid to current user instead of root uid

I have a script that creates a filesystem archive. For this I need superuser privileges, but I do not wish to run the whole script with sudo, just the specific line that creates the archive.

The problem is, when I prefix the archiving command with 'sudo', the files which are created inside the archive all have my uid (1000), not the root uid (0). When this filesystem is later unpacked into the target platform, and we boot into it as root, attempting to perform actions that require superuser priviliges fails: the system thinks that the root account should have uid 1000, not 0.

How do I make the tar command run as root while also have the correct uid in the result?

Upvotes: 1

Views: 282

Answers (1)

Etan Reisner
Etan Reisner

Reputation: 81012

tar preserves the permissions, ownership, etc. that the files have on the filesystem by default.

You can override that with the --owner=USER and --group=USER flags.

Additionally, you can tell tar to use the numeric ownership and group information stored in the tar file instead of the name equivalents (in case the names differ on the different systems) with the --numeric-owner argument.

So if the only reason you needed root for the tar command was to set ownership, etc. in the tarball then you don't need that at all.

Upvotes: 2

Related Questions