在PHP中以root身份運(yùn)行外部命令[轉(zhuǎn)自linuxaid]
Jun 21, 2016 am 09:12 AMHunte 2001年4月15日
在PHP中運(yùn)行只有root用戶才可以運(yùn)行的外部程序,一直是個(gè)老問題,用常規(guī)的辦法很難實(shí)現(xiàn)。這是因?yàn)橐话闱闆r下,PHP是作為APACHE的一個(gè)模塊的,也就是說,PHP是APACHE的一部分,而APACHE除了suEXEC機(jī)制外,是不能以不同的用戶ID來執(zhí)行命令的,但suEXEC機(jī)制只能CGI有效。
網(wǎng)上曾經(jīng)有一篇文章,說用調(diào)用"su - -c COMMAND"可以實(shí)現(xiàn),但經(jīng)過多次試驗(yàn),發(fā)現(xiàn)不行,因?yàn)閟u命令必須在STDIN上輸入root的密碼。
怎么辦?用常規(guī)的方法難以奏效,只能再想其它的方法了。成功的關(guān)鍵在于能有一個(gè)可以切換用戶ID但又可以在命令上輸入密碼(或不用輸入密碼)的工具。有這樣的工具嗎?有,它就是super。
下面就具體說說如何來做?
要注意的是,安裝和配置super,都要以root身份來進(jìn)行。
第一步,切換到root下
第二步,安裝super
先到ftp://ftp.mdtsoft.com/pub/super下載super-3.14.0-1.i386.rpm。這是一個(gè)RPM文件,其它包括了兩個(gè)工具:setuid和super,以及它們的文檔和man手冊。用下面的命令將它安裝到系統(tǒng)中:
% rpm -Uvh super-3.14.0-1.i386.rpm
你還可以用這個(gè)命令來查看這個(gè)RPM中的文件:
% rpm -qpl super-3.14.0-1.i386.rpm
從結(jié)果可以看到,兩個(gè)工具都將被安裝到/bin目錄下。
第三步,配置super
super的配置文件是/etc/super.tab。這是一個(gè)文本文件,格式也比較復(fù)雜。不過,我們這里只要很簡單的加上幾行就可以了。至于詳細(xì)的說明,可以通過man super.tab來查看。
假設(shè)運(yùn)行Apache的用戶是nobody,我們欲通過super來增加系統(tǒng)用戶(調(diào)用useradd命令),那么我們只要在super.tab文件中加入以下這行:
auser /sbin/useradd nobody,hunte
第一段是super能夠識別的命令的別名;第二段是該別名所對應(yīng)的系統(tǒng)命令的全路徑;第三段是可以運(yùn)行該命令的用戶列表,用逗號分隔。這里除了nobody外,還一個(gè)叫hunte的普通用戶,是用于下面的測試。當(dāng)然,你應(yīng)該用你系統(tǒng)中有的任意一個(gè)普通用戶。
至此,super的配置就算好了。
第四步,測試
以第三步中指定的非nobody用戶登錄,運(yùn)行:
% /bin/super auser testuser
如果前面的配置沒什么錯(cuò)誤的話,用戶testuser應(yīng)該是成功地創(chuàng)建了??梢杂茫?br>% cat /etc/passwd | grep testuser
命令來驗(yàn)證一下。
第五步,在PHP中調(diào)用該命令
下面是PHP代碼:
if ($username)
{
//應(yīng)該檢查新用戶是否已經(jīng)存在
echo '正在創(chuàng)建用戶...';
system(escapeshellcmd("/bin/super auser $username"));
}
?>
使用super,使得在PHP中以root身份運(yùn)行外部命令不再是難事。試試看吧。
測試環(huán)境:RedHat Linux 7.0 (Kernel 2.4.3) + Apache 1.3.9 + PHP 4.0.4pl1
chpasswd.cgi是一個(gè)修改linux用戶密碼的cgi程序,彌補(bǔ)更改密碼需要兩次輸入密碼的缺陷

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHPhasthreecommentstyles://,#forsingle-lineand/.../formulti-line.Usecommentstoexplainwhycodeexists,notwhatitdoes.MarkTODO/FIXMEitemsanddisablecodetemporarilyduringdebugging.Avoidover-commentingsimplelogic.Writeconcise,grammaticallycorrectcommentsandu

The key steps to install PHP on Windows include: 1. Download the appropriate PHP version and decompress it. It is recommended to use ThreadSafe version with Apache or NonThreadSafe version with Nginx; 2. Configure the php.ini file and rename php.ini-development or php.ini-production to php.ini; 3. Add the PHP path to the system environment variable Path for command line use; 4. Test whether PHP is installed successfully, execute php-v through the command line and run the built-in server to test the parsing capabilities; 5. If you use Apache, you need to configure P in httpd.conf

The basic syntax of PHP includes four key points: 1. The PHP tag must be ended, and the use of complete tags is recommended; 2. Echo and print are commonly used for output content, among which echo supports multiple parameters and is more efficient; 3. The annotation methods include //, # and //, to improve code readability; 4. Each statement must end with a semicolon, and spaces and line breaks do not affect execution but affect readability. Mastering these basic rules can help write clear and stable PHP code.

How to start writing your first PHP script? First, set up the local development environment, install XAMPP/MAMP/LAMP, and use a text editor to understand the server's running principle. Secondly, create a file called hello.php, enter the basic code and run the test. Third, learn to use PHP and HTML to achieve dynamic content output. Finally, pay attention to common errors such as missing semicolons, citation issues, and file extension errors, and enable error reports for debugging.

PHPisaserver-sidescriptinglanguageusedforwebdevelopment,especiallyfordynamicwebsitesandCMSplatformslikeWordPress.Itrunsontheserver,processesdata,interactswithdatabases,andsendsHTMLtobrowsers.Commonusesincludeuserauthentication,e-commerceplatforms,for

The steps to install PHP8 on Ubuntu are: 1. Update the software package list; 2. Install PHP8 and basic components; 3. Check the version to confirm that the installation is successful; 4. Install additional modules as needed. Windows users can download and decompress the ZIP package, then modify the configuration file, enable extensions, and add the path to environment variables. macOS users recommend using Homebrew to install, and perform steps such as adding tap, installing PHP8, setting the default version and verifying the version. Although the installation methods are different under different systems, the process is clear, so you can choose the right method according to the purpose.

TohandlefileoperationsinPHP,useappropriatefunctionsandmodes.1.Toreadafile,usefile_get_contents()forsmallfilesorfgets()inaloopforline-by-lineprocessing.2.Towritetoafile,usefile_put_contents()forsimplewritesorappendingwiththeFILE_APPENDflag,orfwrite()w

The key to writing Python's ifelse statements is to understand the logical structure and details. 1. The infrastructure is to execute a piece of code if conditions are established, otherwise the else part is executed, else is optional; 2. Multi-condition judgment is implemented with elif, and it is executed sequentially and stopped once it is met; 3. Nested if is used for further subdivision judgment, it is recommended not to exceed two layers; 4. A ternary expression can be used to replace simple ifelse in a simple scenario. Only by paying attention to indentation, conditional order and logical integrity can we write clear and stable judgment codes.
