sudoers语法来运行shell脚本为php用户?

我想跑

  • 一个shell脚本restore
  • 作为postgres用户
  • 从PHP脚本(用户名是php
  • 使用sudo(所以不需要密码)

该脚本将有几行代码,像

 dropdb <database name> psql -c "create database <database name> with encoding 'unicode';" -U edutemplate1 psql -d <database name> -f edu.hourly.sql 
  • 运行脚本restoresudo命令语法是什么?
  • sudoers的语法是什么,以允许php用户运行restore脚本作为postgres用户?

  • Sudo版本1.7.2p7

  • Linuxtesting2.6.34-12-desktop#1 SMP PREEMPT 2010-06-29 02:39:08 +0200 x86_64 x86_64 x86_64 GNU / Linux

UPDATE

问题的第一部分已经在SO sudoers-syntax-to-run-shell-script-as-php-user

UPDATE2

我添加了php ALL=(postgres) NOPASSWD: /usr/bin/id到/ etc / sudoers然后做了su php ,然后sudo -su postgres /usr/bin/id但是我要求postgres的密码。 如果我在用户根下发出相同的sudo命令,我会得到正确的输出'uid = 26(postgres)gid = 26(postgres)groups = 26(postgres)'

UPDATE3

更新sudoers到php ALL=(postgres) NOPASSWD: ALL使它工作…

对于类似的情况也请参见这个答案 。

这进入/etc/sudoers

php ALL=(postgres) NOPASSWD: /the/restore/script

而你让PHP发出这样的命令: sudo -u postgres /the/restore/script

你可以从根目录“su $ USERNAME -c”命令“”无论如何你可以做到这一点威盛sudo,如果你可以请描述你想要做的到底是什么? 例如从哪个用户? 是否需要密码? (你可以在/ etc / sudoers中定义它)通过“php user”你的意思是www-data,或者没有人或其他用户?