简体   繁体   中英

How to run a shell script with root privileges from PHP?

I'm trying to use vpopmail's vadddomain to add new virtual domains. Since apache user has no access to vpopmail's binaries I cannot exec('../bin/vadddomain...') directly from PHP.

I made a shell script ( adddomain.sh ) as below:

#!/bin/bash
 cd /home/lxadmin/mail/bin
 ./vadddomain $1 postmaster_password

And appended this line to /etc/sudoers using visudo (forget about security for now):

ALL ALL=NOPASSWD: /home/lxadmin/mail/bin/adddomain.sh

I have something like this in my PHP script:

exec('sudo /home/lxadmin/mail/bin/adddomain.sh example.com', $output);

But this is not working at all (domain example.com will not be added to vpopmail). But when I run this command through SSH and with root user, it works.

Can anyone tell what is my problem here?

This should be possible by adding the webmaster -user tot the /etc/sudoers file and simply accept that user to use the command you are willing to use. I do not recommend to use the sudo command directly in your php since you probably have to store the password for this somewhere, which off course you do not want! You don't want to post your password by accident somewhere or another developer which you don't want to have root privileges to your code.

Another big downside of simply using root commands is that WHEN you have a security breach in your code, that I can run sudo commands through for example a form. Be very careful with the privileges.

Ps. I'm new to SSH and setting up users privileges as well, so please correct me if I'm wrong. But this is what I know.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM