Home > php教程 > php手册 > PHP使用root权限调用shell

PHP使用root权限调用shell

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-06 19:49:15
Original
1112 people have browsed it

PHP 调用 shell 时,有些时候会遇到权限问题,通过查看Apache日志可以发现如下的内容 rm: instrumentscli0.trace/form.template: Permission denied 原因是shell在执行这些语句时可能需要root权限,可以在脚本中的具体语句加上sudo ,比如 sudo rm xxx;当然

PHP调用shell时,有些时候会遇到权限问题,通过查看Apache日志可以发现如下的内容

rm: instrumentscli0.trace/form.template: Permission denied

原因是shell在执行这些语句时可能需要root权限,可以在脚本中的具体语句加上sudo ,比如 sudo rm xxx;当然最好是不要修改shell脚本,而是在php调用shell时,加上sudo ./xxx.sh


加上sudo后再次执行,发现第二个问题

sudo: no tty present and no askpass program specified

原因是执行sudo时需要一个终端来输入密码,解决方法是赋予Apache的执行用户root权限。

方法:修改visudo文件

命令如下

sudo visudo

在visudo文件中为Apache的执行用户分配权限,新增下面这一行

_www   ALL=(ALL) NOPASSWD: ALL

赋予_www用户所有权限,并且不需要输入密码,这其中,第一项是Apache执行用户的名称,不同的版本可能不一样,有的是nobody,有的是daemon, 有的是_www

可以通过查看Apache目录下的httpd.conf获悉具体用户,该文件有如下一段配置项

# If you wish httpd to run as a different user or group, you must run

# httpd as root initially and it will switch.  

#

# User/Group: The name (or #number) of the user/group to run httpd as.

# It is usually good practice to create a dedicated user and group for

# running httpd, as with most system services.

#

User _www

Group _www

这里就是执行用户,出于安全考虑,最好把默认的用户名修改为自定义的用户,当然也可以不修改。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template