What do you think? Discuss, post comments, or ask questions at the end of this article. [More about me]

Skip to end of metadata
Go to start of metadata

Problem

Running a command that is known to exist in a folder that is in a $PATH but fails when attempting to execute with sudo.

For example, I added a executable script to /usr/local/sbin (which is in the local and system path on my system) - however this fails with command not found when running with sudo.

Solution

sudo acccess is controlled by /etc/sudoers.  It does not use the $PATH defined for users (including root user).  You'll need to add said path to the secure_path default in /etc/sudoers.

For example, say /usr/local/sbin was not in the secure_path default, e.g. running:

sudo visudo

shows

Defaults        env_reset
Defaults        mail_badpass
Defaults        secure_path="/usr/sbin:/usr/bin"

# User privilege specification
root    ALL=(ALL:ALL) ALL

# Members of the admin group may gain root privileges
%admin ALL=(ALL) ALL

# Allow members of group sudo to execute any command
%sudo   ALL=(ALL:ALL) ALL

# See sudoers(5) for more information on "#include" directives:

#includedir /etc/sudoers.d

To add /usr/local/sbin to the default path for sudo, we would modify line 3 to:

Defaults        secure_path="/usr/sbin:/usr/local/sbin:/usr/bin"


References

  1. https://askubuntu.com/questions/118263/some-programs-not-found-when-used-with-sudo