如何在 根用户 (具有所有权限)而不是 没人 用户(php默认用户)下从PHP运行bash脚本?
在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
您可以使用sudo:
exec("sudo /your/script");
您应该允许在没有密码提示的情况下执行脚本。sudo visudo在控制台中运行,并将以下字符串添加到末尾:
sudo visudo
nobody ALL = NOPASSWD: /your/script
您必须正确设置文件模式,以确保没有人可以修改此脚本并将危险内容放入该脚本(在根控制台中):
chown root:root /your/script chmod 755 /your/script