Skip to content
Advertisement

Why am I able to run PHP sudo commands?

I have a PHP script that does the following:

$output = exec('sudo nginx -t 2>&1');
echo $output;

As expected, it returns the following due to using a sudo command:

sudo: no tty present and no askpass program specified

However, I am able to run PHP in the terminal php /var/www/html/script.php and it works without being prompted for a sudo password.

Someone has told me that I am running PHP as a root on my system, which is dangerous.

However, all I did was follow this tutorial – is it really that dangerous?

Also, if I use a Cron job to run the PHP script, it runs perfectly

I am using NGINX with Ubuntu 20.04 + PHP 7

Advertisement

Answer

When you run php /var/www/html/script.php, you run the php process under your current user (that is most probably in the sudoers group).

Ubuntu default sudo password timeout is 15 minutes. Which means, if your user (the one who also runs the PHP script) has entered sudo password less than 15 minutes ago, the system won’t ask for it again.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement