3

I have a batch of CentOS EC2 instances, which have an additional user added to them via ansible. Normally when unpacking a tarball, the owner is centos.centos (default login for EC2 Centos). When an additional user is created (via ansible or manually) it no longer unpacks the tarball with the current user, but uses the uid/gid of the tarball creator. (9999.9999 on another system.)

I know there is a flag I can use when untarring (--no-same-owner) that removes the old permissions, but sets it as 0.0. Is there any way to get tar to automatically set permissions to the current user? I'm unsure if getting everyone untarring on these machines to add another step (chown) is a good idea.

1
  • I cannot reproduce this. Are you running the tar command as root? If so prepending sudo -u <user> will fix the problem. Dec 24, 2020 at 20:04

1 Answer 1

4

If you run tar extract as non-root user it gets extracted as current user by default.

If you get root ownership on untared files you are running it as root user.

Regular user wouldn't be able to change ownership of extracted files to root, that would be a huge security issue (you could put setuid and 777 on any file and then get root privileges that way, by changing file ownership to root).

If you try to extract something from tar as regular user with --same-owner, in case it would try to extract files with ownership not same as user running untar command you will get "Cannot change ownership to uid x, gid x: Operation not permitted".

When you extract tar files as non-root user you extract them with the user running the untar command by default, because regular user cannot do chown to other users.

If you are running untar as root then they get extracted with same uid:gid ownership that they were packed with by default, because root user can do chown to any user.

If it gets run as root you can use sudo -u username or su username -c to run command as some other user, and you should get ownership as that user then.

You can also use cpio to extract from tar as some user:group

cpio -iR user:group -F file.tar

This is from tar manpage.

You should by default get same ownership as user running untar command when not root user.

   --no-same-owner
          Extract files as yourself (default for ordinary users).

   --same-owner
          Try extracting files with the same ownership as exists in
          the archive (default for superuser).
1
  • tar: Unknown option same-owner tar: Unknown option no-same-permission
    – CS QGB
    May 7 at 17:09

You must log in to answer this question.

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