root 是否可以以非 root 身份执行命令?

adn*_*ili 10 linux user su

我是 root 用户,假设我想以另一个用户身份运行任何应用程序。这可能吗,而无需切换到另一个用户?

就像是

# google-chrome user=abc
Run Code Online (Sandbox Code Playgroud)

我实际上以非 root 用户身份执行 CLI 程序。我已经设置了粘性位并且我正在使用 setuid,所以程序以 root 权限运行。现在我system()在程序中使用来调用 GUI 应用程序。但我不想以 root 身份运行它,所以我只想暂时放弃该调用的 root 权限。

Ser*_*rge 10

简短回答:“是的,这是可能的”。

如果您想执行非 X 应用程序,则只需使用以下命令:

sudo -u abc命令

如果您想以其他用户身份运行某个 X 应用程序,但首先使用您自己的桌面,您需要创建一个帮助脚本,这将使您的生活更简单

  • 在您的主目录下创建一个 bin 文件夹:

mkdir -p ~/bin

并使用您最喜欢的文本编辑器创建一个文件~/bin/xsudo,如下所示:

#!/bin/bash
# (C) serge 2012
# The script is licensed to all users of StackExchange family free of charge
# Fixes/Enhancements to the script are greatly appreciated. 
# 
# SUDO_ASKPASS has to be set to the path of ssh-askpass
# fix the following two lines if your distribution does not match this autodetection
. /etc/profile.d/gnome-ssh-askpass.sh
export SUDO_ASKPASS="${SSH_ASKPASS}"

SUDOUSERNAME="$1"
shift
xauth nlist "${DISPLAY}"|sudo -HA -u $SUDOUSERNAME env --unset=XAUTHORITY \
bash -c "xauth nmerge - ; $*"
Run Code Online (Sandbox Code Playgroud)

然后使其可执行:

chmod +x ~/bin/xsudo

并以相同的方式使用它,sudo但没有任何开关:

xsudo用户应用程序

享受。

PS强烈不鼓励xsessionroot账号入手!


jll*_*gre 9

便携式解决方案是:

su abc -c google-chrome
Run Code Online (Sandbox Code Playgroud)

但是,由于 google-chrome 需要 X11 访问权限,因此除非您不对其进行保护,否则这可能会失败,这将是一个非常糟糕的主意,尤其是在以 root 身份运行时。

如果允许 X11 隧道/转发,更好的方法是

ssh -X abc@localhost google-chrome
Run Code Online (Sandbox Code Playgroud)

或者

ssh -Y abc@localhost google-chrome
Run Code Online (Sandbox Code Playgroud)