Home > Enterprise >  How to: sudo -u <username> in sudoer?
How to: sudo -u <username> in sudoer?

Time:11-20

I need to launch a command with sudo rights out of a php file (user: www-data), explicitly as user www-data:

<?php
    $command = 'sudo -u www-data /usr/bin/python3 /var/www/html/script.py';
    shell_exec($command);
?>

to be able to use sudo for www-data I want to put the command in sudoers (sudo visudo), like:

www-data ALL=NOPASSWD: sudo -u www-data /usr/bin/python3 /var/www/html/script.py

or

www-data ALL=NOPASSWD: -u www-data /usr/bin/python3 /var/www/html/script.py

but the syntax is wrong (error message from visudo). The following is working with sudoers (correct syntax)

www-data ALL=NOPASSWD: /usr/bin/python3 /var/www/html/script.py

but doesn't work for my script (apache error in log file):

Sorry, user www-data is not allowed to execute '/usr/bin/python3 /var/www/html/script.py' as www-data on raspberrypi.

it seems it needs sudo -u www-data. How can I solve this?

CodePudding user response:

It makes no sense to use sudo to allow www-data to run commands as www-data, but you can easily do so:

www-data ALL=(www-data) NOPASSWD: /usr/bin/python3 /var/www/html/script.py

The problem with your approaches was that you tried to add the command sudo -u www-data .. to sudoers, which corresponds to double-sudo sudo sudo -u www-data ..

  • Related