为什么sudo无法获取root用户的路径

为什么sudo无法获取root用户的路径

我已经更改了root用户的PATH环境变量。以下命令也显示路径。

 su
 env | grep ^PATH

PATH=/root/.nix-profile/bin:/nix/var/nix/profiles/default/bin:/opt/distrod/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin :/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/pierre/.nix-profile/bin

 sudo env | grep ^PATH

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin


我错过了什么?

我知道如果写

sudo echo $PATH

它不起作用,因为在更改用户之前已经评估了 PATH。我不认为这是一个这样的问题,但我没有其他想法

答案1

我错过了什么?

该 sudo 会清理环境变量,仅传递那些你明确允许的变量

答案2

我认为 sudo 使用调用用户 .profile 中的值,而 su 将运行 .profile 等内容作为切换到新 ID 的一部分。我认为 sudo -i 会获得正确的 PATH 变量。

-i--登录

 Run the shell specified by the target user's password database entry 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 command is run with an environment similar to the one a user would receive at log 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.

相关内容