在upstart脚本中切换用户

时间:2015-07-14 16:40:58

标签: bash ubuntu upstart

是否可以使用以root身份运行预脚本的upstart脚本,但其余部分为normal_user。我尝试过这样的事情:

setuid normal_user

pre-start exec su -c "echo I'm root" root

script
exec /bin/bash <<"EOT"
    echo "haha, I'm normal user"
EOT

是否有必要放弃setuid?

1 个答案:

答案 0 :(得分:2)

通常,您必须删除setuid节,以便您的作业以root身份运行。然后,您可以在exec / script节中删除权限。

来自Upstart Cookbook的Changing User部分:

  

Debian和Ubuntu系统的推荐方法是使用helper实用程序start-stop-daemon(8),如下所示:
  exec start-stop-daemon --start -c myuser --exec command

     

如果你想使用su(1)...为了避免由于产生shell而引起的fork(2),你可以改为指定:
  exec su -s /bin/sh -c 'exec "$0" "$@"' $user -- /path/to/command --arg1=foo -b wibble

     

使用sudo(8)的基本示例:
  exec sudo -u $user command

相关问题