如何从 apache (centOS) 以 root 身份运行 cmd?
我正在尝试在 CENTOS 中运行“useradd”命令,但我不能,因为我需要 root 权限。
在我的 php_info();
中,我有“--disable-posix”。我尝试重新安装 PHP,并尝试使用 yum 和更多选项启用 posix,但没有成功。
有人可以帮助我启用 posix 或其他一些解决方案吗?我注意到 posix_getuid(); 正在工作,但 posix_setuid(); 不起作用。
有什么解决办法吗?
我需要通过“用户单击”将 useradd
插入 passwd(root) 命令。最好、最安全的方法是什么?
多谢!
科伦·奥尔
I'm trying to run the "useradd" command in CENTOS but I can't because I need root permissions.
In my php_info();
I have '--disable-posix'. I have tried to re-install my PHP, and tried to enable the posix with yum and more options, but no luck.
Can someone help me to make the posix enable or some other solutions? I notice that posix_getuid();
is working, but posix_setuid();
is not.
Any solution?
All I need to insert useradd
into passwd(root) command by "user click". What is the best and most secure way to do this?
Thanks a lot!
Koren Or
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Sudo 可能是快速实现这一点的快速技巧,但它很难保证安全,而且即使对于经验丰富的 Unix 大师来说也存在陷阱。
另一种方法是编写自己的服务器守护程序,以 root 身份运行,侦听本地 unix 域套接字或命名管道,或者只是在某个受保护的目录中查找文件。然后从您的 php 脚本向该守护进程发送消息并添加要添加的用户名。在此守护进程中仅实现最低限度的应用程序功能,而其他所有功能都在 php 中实现。但是在你的守护进程中进行强大的输入检查,清理你的环境等,以确保它是 php 应用程序调用,而不是其他人。
Sudo could be a quick hack to implement this quickly, but it is hard to secure and there are pitfalls even for the experienced unix guru.
A different way is to write your own server daemon, running as root, listening to a local unix domain socket, or a named pipe, or simply to look for files in a certain protected directory. Then message this daemon from your php script with the user name to add. Implement only the bare minimum application functionality in this daemon, and everything else in php. But do strong input checks in your daemon, sanitize your environment, etc, to be really sure it is the php app calling, not someone else.
您想要阅读 suEXEC 文档: http://httpd.apache.org/docs/2.2 /suexec.html
然后重新思考您的应用程序如何工作并找出更好/更安全的方法。
You want to read the suEXEC documentation: http://httpd.apache.org/docs/2.2/suexec.html
Then rethink how your application work and figure out a better/safer way.
您可以使用 sudo 来执行此操作。
运行 visudo 并输入类似的内容
实际上,如果您强制执行 SELINUX,它将无法工作,并且不使用 useradd,我建议编写一个包装器脚本,该脚本可以正确设置环境并在运行 useradd 之前进行健全性检查
You could use
sudo
for doing this.run
visudo
and put something likeActually if you have SELINUX enforced it will not work, and instead of using useradd I would recommand writing a wrapper script which sets properly the environment and does sanity checks before running useradd
我建议编写一个 shell 脚本,通过 sudo 调用 useradd 。
您可以将特定用户的特定命令添加到 /etc/sudoers (通过 visudo 命令编辑)
应该是这样的:
Cmnd_Alias USERADD = /bin/sbin/useradd *
apache ALL=(USERADD ) NOPASSWD:用户添加
调用 useradd:
sudo /usr/sbin/useradd USERNAME
不要忘记在 php 脚本和 shell 脚本中验证您的输入。
I'd suggest to write a shell script which will call useradd via sudo.
You can add specific commands for specific users to /etc/sudoers (edited by visudo command)
Should be something like this:
Cmnd_Alias USERADD = /bin/sbin/useradd *
apache ALL=(USERADD) NOPASSWD:USERADD
calling useradd:
sudo /usr/sbin/useradd USERNAME
Do not forget to validate your input in both php script and shell script.
Sudo 需要以交互方式运行(它需要密码)
您不应该以任何方式向 Web 服务器授予 root 访问权限。
Suexec 是一种替代方案,但您也可以将 Web 脚本写入文件并使用 cron 作业来解析文件并处理命令
Sudo needs to be run interactively (it expects a password)
You shouldn't be granting root access in any way to the web server.
Suexec is an alternative, but you could also just have the web script write to a file and have a cron job that parses the file and processes the commands