bash 在新贵脚本中的用户之间切换
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31412554/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Switch between users in an upstart script
提问by Ingo
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:
是否有可能有一个新贵脚本以 root 身份运行 pre-script 但其余的以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
Is it necessary to drop setuid?
是否有必要删除setuid?
回答by Ingo
I finally got it working by removing setuid normal_user
and change
我终于通过删除setuid normal_user
和更改让它工作了
exec /bin/bash <<"EOT"
to
到
exec sudo -u normal_user /bin/bash <<"EOT"
回答by cyfur01
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.
通常,您必须删除该setuid
节,以便您的作业以 root 身份运行。然后您可以在exec
/script
节中删除权限。
From the Upstart Cookbook's Changing Usersection:
来自 Upstart Cookbook 的“更改用户”部分:
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
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