-1

I've changed the PATH environment variable of the root user. The following command are there too show the 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

But

 sudo env | grep ^PATH

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


What have I missed?

I know that if write

sudo echo $PATH

it doesn't work because PATH is already evaluated before changing user. I don't think that it is a problem like this but I have no others idees

2 Answers2

0

What have I missed?

That sudo cleans the environment variables and only passes those that you explicitly allowed

Hvisage
  • 470
0

I think sudo uses values from the invoking user .profile while su will run things like .profile as part of switching to new ID. I think sudo -i will get the correct PATH variable.

-i --login

 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.