
我已將以下單行新增至/etc/sudoers
via 命令visudo
:
www-data ALL=(ALL) NOPASSWD: /var/www/db_backup.sh
通過執行命令www-data
後:
sudo -u www-data /var/www/db_backup.sh
我收到以下訊息:
sudo: unable to execute /var/www/db_backup.sh: Permission denied
不確定為什麼會這樣,因為我過去曾使用過相同的程序,沒有出現問題。一定有什麼東西是我忽略的。有什麼向任何人指出的嗎?我的/etc/sudoers
文件如下:
#
# This file MUST be edited with the 'visudo' command as root.
#
# Please consider adding local content in /etc/sudoers.d/ instead of
# directly modifying this file.
#
# See the man page for details on how to write a sudoers file.
#
Defaults env_reset
Defaults mail_badpass
Defaults secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
# Host alias specification
# User alias specification
# Cmnd alias specification
# User privilege specification
root ALL=(ALL:ALL) ALL
www-data ALL=(ALL) NOPASSWD: /var/www/db_backup.sh
# Allow members of group sudo to execute any command
%sudo ALL=(ALL:ALL) ALL
# See sudoers(5) for more information on "#include" directives:
#includedir /etc/sudoers.d
另外,作為旁注,當嘗試sudo /var/www/db_backup.sh
從 www-data 下運行的 php 進程中執行命令時,會收到以下訊息(儘管我認為這只是由於未從 sudoers 檔案中獲取權限的問題?):
sudo: no tty present and no askpass program specified
編輯
ls -al /var/www/db_backup.sh
產生以下結果:
-rwxrwx--- 1 root root 523 Jan 7 2019 /var/www/db_backup.sh
答案1
正如評論中所述,其他用戶無法執行您的腳本。因此,如上所述,更改使用者所有權和群組所有權,然後將其標記為可執行檔:chown www-data:www-data /var/www/db_backup.sh && chmod +x /var/www/db_backup.sh
。關於你的第二個問題,應該單獨詢問,但無論如何,在不確切知道腳本包含什麼內容的情況下,很難說出哪裡出了問題。