How do untar as the current user by default?
Solution 1:
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).