[英]How to sudo or su to other user as www-data
我試圖建立一個運行腳本的網頁。 事實是,這些腳本必須使用其他用戶運行。 這不是權限問題,我可以將腳本實際作為www-data運行,但是腳本依賴於用戶設置的整個框架和環境變量。
我希望對用戶使用sudo或su並在之后運行腳本。
該網頁會調用一個運行的php文件:
$cmd1 = "sudo login -f otheruser";
$cmd2 = "whoami";
echo exec(escapeshellcmd($cmd1), $output, $status);
foreach($output as $line) echo "$line\n";
echo exec(escapeshellcmd($cmd2), $output, $status);
foreach($output as $line) echo "$line\n";
我嘗試同時使用“ sudo login -f otheruser”和“ su-otheruser”,並且進行了設置,因此從www-data登錄到otheruser不需要密碼(/etc/pam.d/su)
第一個命令什么都不返回,第二個命令返回:www-data,因此顯然它沒有登錄到另一個用戶。
關於如何完成此操作的任何想法,或者如何以其他用戶身份運行腳本的替代方法?
編輯:所以在看了更多之后,這是由於許多原因。
首先,讓我說,最有可能的解決方案是給Apache HTTPD sudoers訪問權限。
其次,如果用戶是www-data
,那么我將假設您使用的是某些Ubuntu版本。
因此,這是使它工作的方法。我首先看的是日志文件。 journalctl -u apache2 -f
httpd日志文件。 您會看到很多這樣的條目:
Nov 02 15:27:17 ubuntu sudo[23541]: pam_unix(sudo:auth): conversation failed
Nov 02 15:27:17 ubuntu sudo[23541]: pam_unix(sudo:auth): auth could not identify password for [www-data]
Nov 02 15:27:17 ubuntu sudo[23541]: www-data : command not allowed ; TTY=unknown ; PWD=/var/www/html ; USER=postgres ; COMMAND=/var/www/html/test2.sh
因此,讓我們首先創建幾個文件進行測試。 我們將以盡可能確保這一點的思路來進行研究。 因此,我將創建兩個測試文件,一個是我們要授予執行許可的文件,另一個是我們不需要的文件。
cd /var/www/html
echo -e "#\x21/bin/bash\necho \"Running command: \$0\"\n" | tee test.sh test2.sh
chown postgres:postgres test*.sh
chmod 700 test*.sh
現在,我們要授予www-data
用戶訪問sudo
權限。 但是,我們只希望它能夠執行命令/var/www/html/test.sh
而不是/var/www/html/test2.sh
。 因此,讓我們創建以下文件:
www-data ALL=(ALL) NOPASSWD:/var/www/html/test.sh
echo 'www-data ALL=(ALL) NOPASSWD:/var/www/html/test.sh' > /etc/sudoers.d/www-data
我對示例PHP腳本進行了一些更新,以提供更多輸出:
<?php
$cmd1 = "sudo -u postgres /var/www/html/test.sh";
$cmd2 = "sudo -u postgres /var/www/html/test2.sh";
$cmd3 = "whoami";
echo "Executing command 1: $cmd1\n";
exec(escapeshellcmd($cmd1), $output, $status);
foreach($output as $line) echo "$line\n";
unset($output);
echo "\nExecuting command 2: $cmd2\n";
exec(escapeshellcmd($cmd2), $output, $status);
foreach($output as $line) echo "$line\n";
unset($output);
echo "\nExecuting command 3: $cmd3\n";
exec(escapeshellcmd($cmd3), $output, $status);
foreach($output as $line) echo "$line\n";
?>
現在,我得到以下信息:
$ curl localhost
Executing command 1: sudo -u postgres /var/www/html/test.sh
Running command: /var/www/html/test.sh
Executing command 2: sudo -u postgres /var/www/html/test2.sh
Executing command 3: whoami
www-data
命令2不生成任何輸出,因為www數據用戶沒有執行該輸出的權限。 您應該在日志條目中看到以下內容:
Nov 02 15:27:17 ubuntu sudo[23541]: pam_unix(sudo:auth): conversation failed
Nov 02 15:27:17 ubuntu sudo[23541]: pam_unix(sudo:auth): auth could not identify password for [www-data]
Nov 02 15:27:17 ubuntu sudo[23541]: www-data : command not allowed ; TTY=unknown ; PWD=/var/www/html ; USER=postgres ; COMMAND=/var/www/html/test2.sh
原始答案:
我使用sudo su -c
能夠獲得預期的輸出。 請參閱以下內容:
$cmd1 = "sudo su -c 'whoami' postgres";
$cmd2 = "whoami";
echo exec(escapeshellcmd($cmd1), $output, $status);
foreach($output as $line) echo "$line\n";
echo exec(escapeshellcmd($cmd2), $output, $status);
foreach($output as $line) echo "$line\n";
輸出
$ ./test.php
postgrespostgres
myuserpostgres
myuser
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.