I have a script in my $HOME/bin folder that must be executed with sudo.

If I run

sudo <nameofthescript>

I get

sudo: <nameofthescript>: command not found

What is the most preferable way to add this script to the $PATH of sudo?

Should I move it in /usr/local/sbin?

Should I create a /root/bin folder?


sudo sets up a limited PATH, so it will not find your local script. You can configure it to not env_reset for a given user and so on, but perhaps the simplest thing is to write your own one-line mysudo script that does:

exec sudo -s PATH="$PATH" exec "$@"

and then say mysudo somecommand ... instead of sudo somecommand .... It will ask sudo to run a shell, set the PATH back to what it was, then run your command found in that original path.


PATH is an environment variable on operating systems like Linux, Windows which has specific set of directories where executable programs are located. According to your question, your executable script is placed under $HOME/bin directory. So if you want to run a script which is not in PATH, you need to give full file structural path to let OS reach the file to execute it. For example,

sudo $HOME/bin/<nameofthescript>

If you do not want to give full file path all the time, you can put that file path in environment veritable using below command. Consedering, you have kept your script in $HOME/bin directory.


Above command will add your bin directory to the environmental path and then you can use <nameofscript> from anywhere on the system. Also, this will only work till the time your bash is running, if you try the same in new terminal, your PATH will change to default. To make it permanent, you need to add below line either in $HOME/.profile or $HOME/.bashrc file.

export PATH=$PATH:$HOME/bin

To allow only root user to execute the <nameofscript> from anywhere, you can either put in under /sbin or /usr/sbin directory.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.