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?
secure_pathon this page, which is the manual page of sudo: sudo.ws/docs/man/1.8.18/sudoers.man