在初始化脚本中切换用户?

发布于 2024-12-27 22:37:37 字数 286 浏览 3 评论 0原文

这是我的 Ubuntu 工作站上的初始化脚本。我需要以除 root 之外的其他用户身份运行命令,但我就是不知道应该如何完成它。 sudo -usu newuser 似乎都不起作用。

脚本:

respawn
console none

start on runlevel [2345]
stop on runlevel [06]

script
  su "anotherUser" -c ./myCommand
end script

Here's my Init script which I have at my Ubuntu workstation. I need to run a command as another user than root, but I just can't get my head around how it should be done. Neither sudo -u or su newuser seems to work.

The script:

respawn
console none

start on runlevel [2345]
stop on runlevel [06]

script
  su "anotherUser" -c ./myCommand
end script

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

指尖微凉心微凉 2025-01-03 22:37:37

我使用这个:

su -l $MUSER -c "myCommand args..."

更新:由于有人对此答案感兴趣,我在这里解释一下我使用它的方式。

我们以普通 Linux 用户身份运行服务器,而不是 root。用户名包含三部分:

service、customer、stage

这样我们就可以在一个 Linux 操作系统中为多个客户运行多个服务。

示例: foo_bar_p 客户“bar”的服务“foo”,“p”表示生产

以下是初始化脚本的部分。初始化脚本可以作为 root 或 foo_bar_p 用户执行:

# /etc/init.d/foo_bar_p-celeryd
# scriptname contains linux username  
SCRIPT_NAME=`basename "$0"`
SYSTEM=${SCRIPT_NAME%*-celeryd}

U=`id -nu`

if [ ! $U == $SYSTEM ]; then
    if [ $U == "root" ]; then
        # use "-l (login)" to delete the environment variables of the calling shell.
        exec su -l $SYSTEM -c "$0 $@"
    fi
    echo "Script must be run from $SYSTEM or root. You are '$U'"
    rc_exit 1
fi

# OK, now I am foo_bar_p
cd
. $HOME/.bashrc
....

I use this:

su -l $MUSER -c "myCommand args..."

Update: Since there is interest in this answer, I explain the way I use it here.

We run servers as normal linux users, not root. The username contains three parts:

service, customer, stage

This way we can run several services for several customers in one linux OS.

Example: foo_bar_p Service "foo" of customer "bar" and "p" means production

Here is the part of the init script. The init script can be executed as root or as foo_bar_p user:

# /etc/init.d/foo_bar_p-celeryd
# scriptname contains linux username  
SCRIPT_NAME=`basename "$0"`
SYSTEM=${SCRIPT_NAME%*-celeryd}

U=`id -nu`

if [ ! $U == $SYSTEM ]; then
    if [ $U == "root" ]; then
        # use "-l (login)" to delete the environment variables of the calling shell.
        exec su -l $SYSTEM -c "$0 $@"
    fi
    echo "Script must be run from $SYSTEM or root. You are '$U'"
    rc_exit 1
fi

# OK, now I am foo_bar_p
cd
. $HOME/.bashrc
....
在你怀里撒娇 2025-01-03 22:37:37

对于新贵,请使用:

setuid myuser
exec command args

For upstart, use:

setuid myuser
exec command args
梦幻的心爱 2025-01-03 22:37:37

su 可能是一种更通用的方法,但这在一些带有 sudo 的常见发行版上也是可能的:(

sudo -u $MUSER $COMMAND $ARGS

只需重读您的问题,并没有意识到这对您不起作用,但它在初始化脚本中对我有用)

su is probably a more universal approach, but this is also possible on some common distributions with sudo:

sudo -u $MUSER $COMMAND $ARGS

(just reread your question and didn't realize that doesn't work for you, but it has worked for me in init scripts)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文