Switch between users in an upstart script

家住魔仙堡 提交于 2020-02-02 12:21:19

问题


Is it possible to have a upstart script that runs the pre-script as root but the rest as normal_user. I'm trying something like:

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

Is it necessary to drop setuid?


回答1:


I finally got it working by removing setuid normal_user and change

exec /bin/bash <<"EOT"

to

exec sudo -u normal_user /bin/bash <<"EOT"



回答2:


In general, you will have to remove the setuid stanza so that your job runs as root. You can then drop privileges in the exec/script stanza.

From the Upstart Cookbook's Changing User section:

The recommended method for Debian and Ubuntu systems is to use the helper utility start-stop-daemon(8) like this:
exec start-stop-daemon --start -c myuser --exec command

If you want to use su(1)... To avoid the fork(2) caused by the shell being spawned, you could instead specify:
exec su -s /bin/sh -c 'exec "$0" "$@"' $user -- /path/to/command --arg1=foo -b wibble

A basic example using sudo(8):
exec sudo -u $user command



来源:https://stackoverflow.com/questions/31412554/switch-between-users-in-an-upstart-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!