Linux 如何在 root 用户下从 PHP 运行 bash 脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10915241/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 06:41:10  来源:igfitidea点击:

How to run from PHP a bash script under root user

phplinuxbashshellunix

提问by Oleg

How to run from PHP a bash script under root user(with all permissions) and not nobodyuser - php default user?

如何在root用户(具有所有权限)而不是nobody用户下从PHP运行bash脚本- php默认用户?

thats my output after sudo visudo:

这就是我在 sudo visudo 之后的输出:

Defaults        env_keep += "LINES COLUMNS"
Defaults        env_keep += "LSCOLORS"
Defaults        env_keep += "SSH_AUTH_SOCK"
Defaults        env_keep += "TZ"
Defaults        env_keep += "DISPLAY XAUTHORIZATION XAUTHORITY"
Defaults        env_keep += "EDITOR VISUAL"
Defaults        env_keep += "HOME MAIL"

#User privilege specification
root    ALL=(ALL) ALL
%admin  ALL=(ALL) ALL


# Uncomment to allow people in group wheel to run all commands
# %wheel        ALL=(ALL) ALL

# Same thing without a password
# %wheel        ALL=(ALL) NOPASSWD: ALL

# Samples
# %users  ALL=/sbin/mount /cdrom,/sbin/umount /cdrom
# %users  localhost=/sbin/shutdown -h now

采纳答案by Pavel Strakhov

You can use sudo:

您可以使用 sudo:

exec("sudo /your/script");

You should allow executing your script without password prompt. Run sudo visudoin console and add the following string to the end:

您应该允许在没有密码提示的情况下执行您的脚本。sudo visudo在控制台中运行并将以下字符串添加到末尾:

nobody ALL = NOPASSWD: /your/script

You must set up file mode properly to ensure that no one can modify this script and put dangerous contents into it (in root console):

您必须正确设置文件模式以确保没有人可以修改此脚本并将危险内容放入其中(在根控制台中):

chown root:root /your/script
chmod 755 /your/script

回答by Sjoerd

You can make a program which is set-uid root. This causes the program to always run as root. This doesn't work with shell scripts, so you have to use a program which calls your script.

你可以制作一个set-uid root的程序。这会导致程序始终以 root 身份运行。这不适用于 shell 脚本,因此您必须使用调用您的脚本程序

回答by Ja?ck

Under Linux you normally do this using sudo. Try to be as specific as possible, so not to give the script too many permissions.

在 Linux 下,您通常使用sudo. 尽量做到具体,不要给脚本太多权限。

For examples on how to use sudo: http://aplawrence.com/Basics/sudo.html

有关如何使用的示例sudohttp: //aplawrence.com/Basics/sudo.html

回答by J. Bruni

I would add a specific rule to allow this script to be called by nobodyuser, using sudo.

我会添加一个特定的规则来允许nobody用户调用这个脚本,使用sudo.

回答by MerlinTheMagic

I recently published a project that allows PHP to obtain and interact with a real Bash shell (as user: apache/www-data or root if needed). Get it here: https://github.com/merlinthemagic/MTS

我最近发布了一个项目,允许 PHP 获取真正的 Bash shell 并与之交互(如果需要,可以作为用户:apache/www-data 或 root)。在这里获取:https: //github.com/merlinthemagic/MTS

After downloading you would simply use the following code:

下载后,您只需使用以下代码:

$shell    = \MTS\Factories::getDevices()->getLocalHost()->getShell('bash', true);
$return1  = $shell->exeCmd('/full/path/to/script.sh');