简体   繁体   中英

php system commands linux

I have set of linux system commands that requires to be logged into shell first. How can i do that in php ?

For example:

$output2 = shell_exec('ls -lrt /opt/test 2>&1');

Since /opt/test can be accessed by user/owner XXX only, I need to login with that username. runuser command could be helpful but i need directions.

Actually I need to run an application via system command but before that user must be logged in to have access to that application.

One way to do it would be to create a shell script containing all of the commands that you need to run as the privileged logged in user, and then run that shell script using sudo.

You will need to change your sudoers file so that the execution of the script can be run by the user that the script is running as.

eg:

 Cmnd_Alias HTTP_COMMANDS = /usr/local/my_ls_script
 XXXX ALL=(ALL) NOPASSWD: HTTP_COMMANDS

Be careful when doing this though! Make sure that the sudoers line can only run the commands that you specify.

Also, make sure that you switch to the user at least once, and run sudo, otherwise, the webserver will not be able to run sudo for the user.

Additionally, you might need to disable NO_TTY in the sudoers file.

You would change your command to:

$output2 = shell_exec('sudo -u XXXX /usr/local/my_ls_script 2>&1');

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