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
How to run from PHP a bash script under root user
提问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 visudo
in 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
有关如何使用的示例sudo
:http: //aplawrence.com/Basics/sudo.html
回答by J. Bruni
I would add a specific rule to allow this script to be called by nobody
user, 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');