How to define PATH environment variable for sudo commands

Last updated on July 7, 2020 by Dan Nanni

Question: I built and installed a program in /usr/local/bin. The program requires root privilege to run. But when I try to run the program with sudo, I get the following error.
sudo: XXXXX: command not found
For some reason /usr/local/bin is not included in the PATH environment variable. How can I fix this problem?

When you run a program with sudo, the program is executed with a new, minimum environment for security reasons. That is, not all the environment variables you define are inherited to sudo commands. In case of PATH environment variable, it is reset to a new "default" PATH variable when sudo is used. So if the new default PATH variable does not include the folder where your program is, you will get "command not found" error with sudo.

To customize the default PATH variable for sudo session, open /etc/sudoers file with a text editor, and look for secure_path. The value defined in secure_path will be used as the default PATH variable when you execute sudo commands.

So add any necessary path (e.g., /usr/local/bin) to secure_path, and it will be passed to sudo commands.

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

This change will be effective immediately.

Support Xmodulo

This website is made possible by minimal ads and your gracious donation via PayPal (Credit Card) or Bitcoin (1M161JGAkz3oaHNvTiPFjNYkeABox8rb4g).

Please note that this article is published by under a Creative Commons Attribution-ShareAlike 3.0 Unported License. If you would like to use the whole or any part of this article, you need to cite this web page at as the original source.

Xmodulo © 2021 ‒ AboutWrite for UsFeed ‒ Powered by DigitalOcean