Run the external command as root in PHP [from Osso]

Source: Internet
Author: User

Introduction: This is a detailed page for PHP to run external commands [from Osso] as root. It introduces PHP, related knowledge, skills, experience, and some PHP source code.

Class = 'pingjiaf' frameborder = '0' src = 'HTTP: // biancheng.dnbc?info/pingjia.php? Id = 327675 'rolling = 'no'>

External sources that can only be run by the root user in PHPProgramIt has always been an old problem, and it is difficult to implement it using conventional methods. This is because, in general, PHP is a module of Apache, that is, PHP is a part of Apache, and Apache has the suexec mechanism, commands cannot be executed using different user IDs, but the suexec mechanism is only valid for CGI.

There was an article on the InternetArticleIt can be implemented by calling "Su-C command". However, after many experiments, we found that the su command cannot be used because the root password must be entered on stdin.

What should I do? It is difficult to use the conventional method, so you can only think about other methods. The key to success lies in the ability to have a tool that can switch the user ID but can enter the password (or do not need to enter the password) on the command. Is there such a tool? Yes, it is super.

Next, let's talk about how to do it?

Note that installation and configuration of Super must be performed as root.

Step 1: Switch to root

Step 2: Install super
First go to ftp://ftp.mdtsoft.com/pub/superdownload super-3.14.0-1.i386.rpm. This is an RPM file. Other tools include setuid and super, as well as their documents and man manuals. Run the following command to install it to the system:
% Rpm-uvh super-3.14.0-1.i386.rpm
You can also use this command to view files in this RPM:
% Rpm-qpl super-3.14.0-1.i386.rpm
The result shows that both tools will be installed in the/bin directory.

Step 3: Configure super
The super configuration file is/etc/super. tab. This is a text file and the format is complex. However, we only need to add a few lines here. For more information, see man Super. tab.
Assume that the user running Apache is nobody. To use super to add a system user (call the useradd command), add the following line to the super. Tab file:
Auser/sbin/useradd nobody, Hunte
The first section is the alias of a command that super can recognize; the second section is the full path of the system command corresponding to the alias; the third section is the list of users who can run the command, separated by commas. In addition to the nobody, a common user called Hunte is used for the following tests. Of course, you should use any common user in your system.
So far, super configuration is good.

Step 4: Test
Log on to the non-nobody user specified in step 3 and run:
%/Bin/super auser testuser
If the preceding configuration is correct, testuser should be created successfully. Available:
% CAT/etc/passwd | grep testuser
Command to verify.

Step 5: Call this command in PHP
Below is the PHPCode:
<?
If ($ username)
{
// Check whether a new user already exists
Echo 'Creating user <$ username> ...';
System (escapeshellcmd ("/bin/super auser $ username "));
}
?>
Using Super makes it no longer difficult to run external commands as root in PHP. Try it.
Test environment: RedHat Linux 7.0 (kernel 2.4.3) + Apache 1.3.9 + PhP 4.0.4pl1

Chpasswd. cgi is a CGI program that modifies the Linux User Password. It makes up for the defect that two passwords are required to change the password.

More articles about "Running External commands [from Osso] as root in PHP"

Love J2EE follow Java Michael Jackson video station JSON online tools

Http://biancheng.dnbcw.info/php/327675.html pageno: 12.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.