Run a shell command as another user or change Apac

2020-02-11 09:24发布

问题:

I've been trying to figure out how to do this the whole day.

The short version: I have to manage some virtual machines using php shell_exec function, so far I cant do this because apache is run by the user www-data and virtualbox by the user vboxuser

From what I've read so far, I've thought of 3 possible solutions: 1.- Create a script on my vboxuser that I can call from php to manage the Virtual Machines. 2.- Change the apache user from www-data to vboxuser so I can manage the Virtual Machines through php 3.- Reinstall VirtualBox, this time using www-data as my user.

I'm not sure if any of these will work and I'm not too sure of which would be the best solution. Any suggestions/ideas?

Thanks in advance.

Now the longer more detailed version:

I have a remote server running Ubuntu 10.04.2, in that server I have set up VirtualBox so I can run several instances of WinXP to perform different tasks.

Everything is setup and I can manage the virtual machines through SSH. If I want to run them as a different user than the one that created them (a user that so far only has been used to create the Virtual Machines) I have to do sudo -u vboxuser.

Now, I need to create a PHP script to manage these virtual machines (I know about phpVirtualBox, but it's not what I need). If I try to run the virtual machines using shell_exec() from php, I get no answer at all (And I have tested that shell_exec is working on my server).

回答1:

you can configure sudo to allow www-data to execute commands as vboxuser use sudo visudo to add a line to /etc/sudoers like

www-data (ALL) = (vboxuser) /usr/bin/vboxmanage

check man sudoers for more information



回答2:

To change the apache user, you can edit:

/etc/apache2/envvars

And change APACHE_RUN_USER=root

Or you could execute it from root's cron

sudo crontab -e

HTH