1

I'm encountering a confusing behavior with sudo and the which command. An executable is found when running which as a regular user, but not when using sudo which, even though it's accessible when switching to an interactive root shell.

Environment:

  • Linux system (CentOS/RHEL based)
  • Custom application installed in non-standard directory

Code to Reproduce:

# As regular user - command is found
[user@server ~]$ which myapp
/opt/custom/apps/bin/myapp

# Using sudo with which - command NOT found
[user@server ~]$ sudo which myapp
which: no myapp in (/sbin:/bin:/usr/sbin:/usr/bin)

# But switching to interactive root shell - command IS found
[user@server ~]$ sudo -u root -i
[root@server ~]# which myapp
/opt/custom/apps/bin/myapp

Why does sudo which use a different PATH than the interactive root shell?

1

2 Answers 2

4
  1. As root, there are more things that should be in your path. (like /sbin and /usr/sbin)
  2. As root, there are things that should not be in your path. (like .)

Therefore, the path is normally reset.

The options for this are in /etc/sudoers

Better would be to tell it just which program you want to run.

1

See the section Command Environment in man sudoers.

By default, env_reset is enabled and PATH is reset to a secure value - either secure_path value or a compiled-in value if the secure_path is not set.

sudo -i keeps PATH value from the user environment.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.