Search code examples
phpubuntuexec

PHP - exec sudo command returns nothing


There are many questions on this topic, but my situation is quite strange.

I am trying to print out exec("sudo -u root whoami") in php. But it returns nothing.

I added %www-data ALL=(ALL:ALL) ALL via sudo visudo but no luck on what is happenning, no error, just not printing out anything.

Any suggestion?

Thanks in advance


Solution

  • How is your webserver going to enter the password for sudo?

    Your configuration allows the webserver-user to run all commands on the server. But that requires the user to enter their password. And the webserver

    1. can't do that interactively
    2. doesn't even have a apassword

    So if you want to do what you try to do (giving the process running PHP root-access to the machine is a very bad idea) you should add this to your sudo-config:

    %www-data     ALL = NOPASSWD: /usr/bin/whoami
    

    That allows the group www-data to run whoami as root without an interctive password-prompt