简体   繁体   English

在upstart脚本中切换用户

[英]Switch between users in an upstart script

Is it possible to have a upstart script that runs the pre-script as root but the rest as normal_user . 是否可以使用以root身份运行预脚本的upstart脚本,但其余部分为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? 是否有必要放弃setuid?

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"

In general, you will have to remove the setuid stanza so that your job runs as root. 通常,您必须删除setuid节,以便您的作业以root身份运行。 You can then drop privileges in the exec / script stanza. 然后,您可以删除exec / script节中的权限。

From the Upstart Cookbook's Changing User section: 从Upstart Cookbook的更改用户部分:

The recommended method for Debian and Ubuntu systems is to use the helper utility start-stop-daemon(8) like this: Debian和Ubuntu系统的推荐方法是使用helper实用程序start-stop-daemon(8),如下所示:
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: 如果你想使用su(1)...为了避免由生成的shell引起的fork(2),你可以改为指定:
exec su -s /bin/sh -c 'exec "$0" "$@"' $user -- /path/to/command --arg1=foo -b wibble

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM