How to run PHP exec() as root? How to run PHP exec() as root? unix unix

How to run PHP exec() as root?


Don't do it! You will leave yourself wide open to all sorts of malicious hackery.

Have a look at the "sudo" documentation.

You should be able to set up all the commands you need as "sudo"able scripts.It is much better to write specific scripts with limited functions than to expose the underlying priviledged command.

As in:

exec ('sudo getIpTables.ksh')


You can run sudo through phpseclib, a pure PHP SSH implementation:

<?phpinclude('Net/SSH2.php');$ssh = new Net_SSH2('www.domain.tld');$ssh->login('username', 'password');$ssh->read('[prompt]');$ssh->write("sudo command\n");$ssh->read('Password:');$ssh->write("Password\n");echo $ssh->read('[prompt]');?>


I know this is an old question

add the user php runs on to the sudo group if it is not already assigned

use sudo -S, so you can pass the password via echo

$exec = "echo your_passwd | /usr/bin/sudo -S your command";exec($exec,$out,$rcode);

if you have trouble with the paths - use

"bash -lc 'echo your_passwd | /usr/bin/sudo -S your command'"

so you get a new bash that acts like a login shell and has the paths set

check the man pages of sudo