Use PHP/shell_exec() to extract tar as specific user


I would like to extract a tar using shell_exec.

I run a Linux server.

<?php  shell_exec('tar xf tar.tar'); ?>

The problem is that when I do this the ownership of all files gets set to nobody.
I want the owner to be me.

My username is:

My password is:

I was told I could do this using sudo / su but I have no ida how to do this.
LVL 16
Who is Participating?
aldanchConnect With a Mentor Commented:
Can you not shell_execute a "chown -R user123:user123 directory" after you extract it?
cristidesConnect With a Mentor Commented:
You can't do that because the script running under apache user, this is the reason why the files have the owner nobody after unpacked
<?php  shell_exec('tar xf tar.tar'); ?>

If you want do that you can try :
shell_exec("cd destination_directory; gunzip {$filename}| tar xf -;");
hankknightAuthor Commented:
None of your ideas work.

I cannot shell_execute a chown because shell exec is user nobody not root
A proven path to a career in data science

At Springboard, we know how to get you a job in data science. With Springboard’s Data Science Career Track, you’ll master data science  with a curriculum built by industry experts. You’ll work on real projects, and get 1-on-1 mentorship from a data scientist.

aldanchConnect With a Mentor Commented:
Probably not a very secure way of doing this but maybe this idea will help...

create a sudo user, modify you sudoers file to include this use. (You could add 'nobody' to sudo users but that would be very dangerous)

Then do something like:

exec("sudo -u sudouser chown -R user:user dir");
You can create a folder with group sticky bit on . so whenever you extract files in that group the owner is always the group and you can add yourself to that group
hankknightAuthor Commented:
aldanch wrote:
->>exec("sudo -u sudouser chown -R user:user dir");

How do I tell sudo my password?
aldanchConnect With a Mentor Commented:
I am not sure which Linux distribution you are using but on Ubuntu you would want to edit the /etc/sudoers file.

You may see something along the lines of:

root ALL=(ALL) ALL

You would want to add something like the following:

mysudouser ALL=(ALL) ALL

Where "mysudouser" is the user you created to perform the action. You will not need a password once you do this.
Forgot to add the man (5) page for the sudoers file.

This will tell you exactly how it works.
hankknightAuthor Commented:
I don't have root access to this server.  That is why I am trying to do Linux command lines using shell_exec instead of SSH.  Is there no way that I can log in through the command line with a username / password?
aldanchConnect With a Mentor Commented:
That depends on your host. Do they allow shell access? If so, then yes :-)

The original question stated that you ran a Linux server.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.