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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 13:20:15  来源:igfitidea点击:

Switch between users in an upstart script

bashubuntuupstart

提问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_userand 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 setuidstanza so that your job runs as root. You can then drop privileges in the exec/scriptstanza.

通常,您必须删除该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