如何运行命令

如何运行命令

我需要使用 sudo 运行,这在commanduserx通常是不可用的,但PATH由于 ,Command在 下的 bash 中可用。userxHOME/.bashrc

只需在 bash 中运行userx

echo $PATH
/opt/sw-python/extra/pythonbrew/bin:/opt/sw-python/extra/pythonbrew/pythons/Python-2.7.2/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games

我已获得权利PATHcommand可以行动。

但如果我使用 sudo 运行:

sudo -u userx echo $PATH
/home/curuser/.pythonbrew/bin:/home/curuser/.pythonbrew/pythons/Python-2.7.2/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games

sudo -H -u userx echo $PATH
/home/curuser/.pythonbrew/bin:/home/curuser/.pythonbrew/pythons/Python-2.7.2/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games

sudo su python -c 'echo $PATH'
/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games

sudo su python -c 'bash -c "echo $PATH"'
/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games

userx未使用's设置 PATH 变量.bashrc

如何使用 sudo 运行以使其在 下PATH正常?PATHuserx

答案1

运行 sudo 以-i启动一个新的交互式 shell(并加载所有用户配置)。

  -i [command]
               The -i (simulate initial login) option runs the shell specified by the password database entry of the target user as a
               login shell.  This means that login-specific resource files such as .profile or .login will be read by the shell.  If
               a command is specified, it is passed to the shell for execution via the shell's -c option.  If no command is
               specified, an interactive shell is executed.  sudo attempts to change to that user's home directory before running the
               shell.  The security policy shall initialize the environment to a minimal set of variables, similar to what is present
               when a user logs in.  The Command Environment section in the sudoers(5) manual documents how the -i option affects the
               environment in which a command is run when the sudoers policy is in use.

相关内容