Nam Nguyen
Nam Nguyen

Reputation: 79

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

Upvotes: 2

Views: 551

Answers (1)

heiglandreas
heiglandreas

Reputation: 3861

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

Upvotes: 1

Related Questions