Home Backend Development PHP Tutorial PHP中 上运行 root 用户才可以运行的外部程序_PHP

PHP中 上运行 root 用户才可以运行的外部程序_PHP

Jun 01, 2016 pm 12:32 PM
Can Order external user program run

在PHP中运行只有root用户才可以运行的外部程序,一直是个老问题,用常规的办法很难实现。这是因为一般情况下,PHP是作为APACHE的一个模块的,也就是说,PHP是APACHE的一部分,而APACHE除了suEXEC机制外,是不能以不同的用户ID来执行命令的,但suEXEC机制只能CGI有效。

网上曾经有一篇文章,说用调用"su - -c COMMAND"可以实现,但经过多次试验,发现不行,因为su命令必须在STDIN上输入root的密码。

怎么办?用常规的方法难以奏效,只能再想其它的方法了。成功的关键在于能有一个可以切换用户ID但又可以在命令上输入密码(或不用输入密码)的工具。有这样的工具吗?有,它就是super。

下面就具体说说如何来做?

要注意的是,安装和配置super,都要以root身份来进行。

第一步,切换到root下

第二步,安装super

先到ftp://ftp.mdtsoft.com/pub/super下载super-3.14.0-1.i386.rpm。这是一个RPM文件,其它包括了两个工具:setuid和super,以及它们的文档和man手册。用下面的命令将它安装到系统中:

% rpm -Uvh super-3.14.0-1.i386.rpm

你还可以用这个命令来查看这个RPM中的文件:

% rpm -qpl super-3.14.0-1.i386.rpm

从结果可以看到,两个工具都将被安装到/bin目录下。

第三步,配置super

super的配置文件是/etc/super.tab。这是一个文本文件,格式也比较复杂。不过,我们这里只要很简单的加上几行就可以了。至于详细的说明,可以通过man super.tab来查看。

假设运行Apache的用户是nobody,我们欲通过super来增加系统用户(调用useradd命令),那么我们只要在super.tab文件中加入以下这行:

auser /sbin/useradd nobody,hunte

第一段是super能够识别的命令的别名;第二段是该别名所对应的系统命令的全路径;第三段是可以运行该命令的用户列表,用逗号分隔。这里除了nobody外,还一个叫hunte的普通用户,是用于下面的测试。当然,你应该用你系统中有的任意一个普通用户。

至此,super的配置就算好了。

第四步,测试

以第三步中指定的非nobody用户登录,运行:

% /bin/super auser testuser

如果前面的配置没什么错误的话,用户testuser应该是成功地创建了。可以用:

% cat /etc/passwd | grep testuser

命令来验证一下。

第五步,在PHP中调用该命令

下面是PHP代码:


if ($username)

{

//应该检查新用户是否已经存在

echo '正在创建用户...';

system(escapeshellcmd("/bin/super auser $username"));

}

?>

使用super,使得在PHP中以root身份运行外部命令不再是难事。试试看吧。

测试环境:RedHat Linux 7.0 (Kernel 2.4.3) + Apache 1.3.9 + PHP 4.0.4pl1

原作者: Hunte
原出处: 不详

摘自:代码实验室

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to make Google Maps the default map in iPhone How to make Google Maps the default map in iPhone Apr 17, 2024 pm 07:34 PM

The default map on the iPhone is Maps, Apple's proprietary geolocation provider. Although the map is getting better, it doesn't work well outside the United States. It has nothing to offer compared to Google Maps. In this article, we discuss the feasible steps to use Google Maps to become the default map on your iPhone. How to Make Google Maps the Default Map in iPhone Setting Google Maps as the default map app on your phone is easier than you think. Follow the steps below – Prerequisite steps – You must have Gmail installed on your phone. Step 1 – Open the AppStore. Step 2 – Search for “Gmail”. Step 3 – Click next to Gmail app

How to use Xiaohongshu account to find users? Can I find my mobile phone number? How to use Xiaohongshu account to find users? Can I find my mobile phone number? Mar 22, 2024 am 08:40 AM

With the rapid development of social media, Xiaohongshu has become one of the most popular social platforms. Users can create a Xiaohongshu account to show their personal identity and communicate and interact with other users. If you need to find a user’s Xiaohongshu number, you can follow these simple steps. 1. How to use Xiaohongshu account to find users? 1. Open the Xiaohongshu APP, click the "Discover" button in the lower right corner, and then select the "Notes" option. 2. In the note list, find the note posted by the user you want to find. Click to enter the note details page. 3. On the note details page, click the "Follow" button below the user's avatar to enter the user's personal homepage. 4. In the upper right corner of the user's personal homepage, click the three-dot button and select "Personal Information"

Log in to Ubuntu as superuser Log in to Ubuntu as superuser Mar 20, 2024 am 10:55 AM

In Ubuntu systems, the root user is usually disabled. To activate the root user, you can use the passwd command to set a password and then use the su- command to log in as root. The root user is a user with unrestricted system administrative rights. He has permissions to access and modify files, user management, software installation and removal, and system configuration changes. There are obvious differences between the root user and ordinary users. The root user has the highest authority and broader control rights in the system. The root user can execute important system commands and edit system files, which ordinary users cannot do. In this guide, I'll explore the Ubuntu root user, how to log in as root, and how it differs from a normal user. Notice

How to run SUDO commands in Windows 11/10 How to run SUDO commands in Windows 11/10 Mar 09, 2024 am 09:50 AM

The sudo command allows users to run commands in elevated privilege mode without switching to superuser mode. This article will introduce how to simulate functions similar to sudo commands in Windows systems. What is the Shudao Command? Sudo (short for "superuser do") is a command-line tool that allows users of Unix-based operating systems such as Linux and MacOS to execute commands with elevated privileges typically held by administrators. Running SUDO commands in Windows 11/10 However, with the launch of the latest Windows 11 Insider preview version, Windows users can now experience this feature. This new feature enables users to

How to execute .sh file in Linux system? How to execute .sh file in Linux system? Mar 14, 2024 pm 06:42 PM

How to execute .sh file in Linux system? In Linux systems, a .sh file is a file called a Shell script, which is used to execute a series of commands. Executing .sh files is a very common operation. This article will introduce how to execute .sh files in Linux systems and provide specific code examples. Method 1: Use an absolute path to execute a .sh file. To execute a .sh file in a Linux system, you can use an absolute path to specify the location of the file. The following are the specific steps: Open the terminal

Clock app missing in iPhone: How to fix it Clock app missing in iPhone: How to fix it May 03, 2024 pm 09:19 PM

Is the clock app missing from your phone? The date and time will still appear on your iPhone's status bar. However, without the Clock app, you won’t be able to use world clock, stopwatch, alarm clock, and many other features. Therefore, fixing missing clock app should be at the top of your to-do list. These solutions can help you resolve this issue. Fix 1 – Place the Clock App If you mistakenly removed the Clock app from your home screen, you can put the Clock app back in its place. Step 1 – Unlock your iPhone and start swiping to the left until you reach the App Library page. Step 2 – Next, search for “clock” in the search box. Step 3 – When you see “Clock” below in the search results, press and hold it and

PyCharm usage tutorial: guide you in detail to run the operation PyCharm usage tutorial: guide you in detail to run the operation Feb 26, 2024 pm 05:51 PM

PyCharm is a very popular Python integrated development environment (IDE). It provides a wealth of functions and tools to make Python development more efficient and convenient. This article will introduce you to the basic operation methods of PyCharm and provide specific code examples to help readers quickly get started and become proficient in operating the tool. 1. Download and install PyCharm First, we need to go to the PyCharm official website (https://www.jetbrains.com/pyc

How to check the MAC address of the network card in Win11? How to use the command to obtain the MAC address of the network card in Win11 How to check the MAC address of the network card in Win11? How to use the command to obtain the MAC address of the network card in Win11 Feb 29, 2024 pm 04:34 PM

This article will introduce readers to how to use the command prompt (CommandPrompt) to find the physical address (MAC address) of the network adapter in Win11 system. A MAC address is a unique identifier for a network interface card (NIC), which plays an important role in network communications. Through the command prompt, users can easily obtain the MAC address information of all network adapters on the current computer, which is very helpful for network troubleshooting, configuring network settings and other tasks. Method 1: Use "Command Prompt" 1. Press the [Win+X] key combination, or [right-click] click the [Windows logo] on the taskbar, and in the menu item that opens, select [Run]; 2. Run the window , enter the [cmd] command, and then

See all articles